Milinda Pathirage’s Blog

Computers are fascinating machines, but they’re mostly a reflection of the people using them

Python 2.5 C API doesn’t support convertion of PyFloatObject, PyIntObject and PyLongObject to PyStringObject

August 4th, 2008 · No Comments · python

These days I am developing WSF/Python which is a Python extension for consuming and providing Web Services in Python. This extension is develop based WSF/C which is a standards compliant, enterprise grade, open source, C library for providing and consuming Web services in C. While implementing WSDL support for WSF/Python, I have to convert Python Dictionries to our own C structure using Python C API. While converting PyDictObject to our structure, I have to convert PyFloatObjects, PyIntObjects and PyLongObjects to strings to use with WSDL mode C API. But these Python Objects doesn't have a *_AsString or direct way to convert it them to strings. Python C API doesn't provide a way to get C int from PyIntObject and we can only get C long from PyInt. I think this is because different sizes of C data types and Python Integers. To convert PyIntObjects and PyLongObjects to string, we first need to convert them C long using Py<type_name>_AsLong(obj) function and use PyString_FromFormat method to create PyStringObject from them. Or otherwise we have to sprintf like function to directly convert those longs in to string.
1
2
3
4
5
6
7
8
9
10
PyObject *val_str_object = NULL;
long int_val;
int_val = PyInt_AsLong(value);
if(PyErr_Occurred() != NULL)
{
     wsdl_data = NULL;
     return;
}
val_str_object = PyString_FromFormat("%ld", int_val);
val_str = axutil_strdup(env, PyString_AsString(val_str_object));
The funny thing is we can only onvert PyFloatObject to C double and PyString_FromFormat doesn't provide support for doubles. I have to use sprintf to convert duoble in to C string(char *).
1
2
3
4
5
6
double from_float = 0;
char double_str[32];
type_str = axutil_strdup(env, "float");
from_float = PyFloat_AsDouble(value);
sprintf(double_str, "%f", from_float);
val_str = axutil_strdup(env, double_str);
I saw that most of the developers had report this issue with PyFloats and I think this was fixed in Python 3000. But because of most of the users are using stable version Python 2.5, we can't develope WSF/Python for Python 3000. Sphere: Related Content

Related posts brought to you by Yet Another Related Posts Plugin.

Tags:

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment