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

williams.jasonscott at gmail.com williams.jasonscott at gmail.com
Wed Apr 19 12:45:42 EDT 2006


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




More information about the Python-list mailing list