473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C API - Conversions from PyInt or PyFloat to a char *

Hi,

I'm looking at the C API and wanting to know a good way to convert
Python numeric types to native c types. For strings I've been using
PyString_AsString(v) and thats great, but I would like to do the same
for ints and floats. I have found PyFloat_AsString in the source but
it seems to be undocumented and some of the comments make me think that
I shouldn't use it but I've given it a go anyhow. It is defined in
floatobject.h as:

PyAPI_FUNC(void) PyFloat_AsString(char*, PyFloatObject *v);

and also:

PyAPI_FUNC(void) PyFloat_AsReprString(char*, PyFloatObject *v);

And I have a few questions:

Should I use these calls? And the comment that I am concerned about
from floatobject.c is:

315 /* XXX PyFloat_AsString and PyFloat_AsReprString should be
deprecated:
316 XXX they pass a char buffer without passing a length.
317 */

OK so what the caller can manage the buf len, but is there something
else I should be concerned about?

If these types of calls are the way to go, is there a call for int?
And how should I convert a PyObject to a PyFloatObject?

If this isn't a good approach, what is?

thanks,

~jason

Apr 19 '06 #1
2 3743
wi*****************@gmail.com wrote:
I'm looking at the C API and wanting to know a good way to convert
Python numeric types to native c types. For strings I've been using
PyString_AsString(v) and thats great, but I would like to do the same
for ints and floats.


double d = PyFloat_AsDouble(v);
long i = PyInt_AsLong(v);

double real = PyComplex_RealAsDouble(v);
double imag = PyComplex_ImagAsDouble(v);
Py_complex c = PyComplex_AsCComplex(v);

long i = PyLong_AsLong(v);
unsigned long y = PyLong_AsUnsignedLong(v);
double d = PyLong_AsDouble(v);
PY_LONG_LONG l = PyLong_AsLongLong(v);
// and others; see include/longobject.h for details

the float and int versions also available as "unsafe" macro versions

double d = PyFloat_AS_DOUBLE(v);
int i = PyInt_AS_LONG(op);

(the macro versions assume that that v points to a python object of the
right type; it's up to you to do the type check)

</F>

Apr 19 '06 #2
Thanks for the reply, This is the approach I have taken. Convert to a
c numeric and take it from there.

Is there movment to add functions like PyInt_AsString ? I noticed it
mentioned in response to a PEP...

thanks,
~jason

Apr 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: wongjoekmeu | last post by:
Hello All, I am trying to poort a code which is written in Visual C++ 6.0 to Visual C++ in VIsual Studio .NET I receive a problem which I simply do not understand. I was wondering if anyone could...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
5
by: Daz | last post by:
Hi everyone! I am very new to casting and converting, so much in fact then when I think I have it figured out, something proves me wrong. I was wondering if there is any logic in 'what types can...
3
by: James Aguilar | last post by:
Quick question. Why, in the following code sample, can I not convert a char ** to a const char **? void test(const char **p) { cout << str(*p) << '\n'; } int main() {
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
2
by: Matthias Neubauer | last post by:
Hi! I am a C++ newbie and I'd like to trace the evaluation of some expressions of my programs. I tried to use the comma operator for that, but failed. I used a simple code substitution for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.