[capi-sig] Beginner Question PyObject* to int
Chris Miles
miles.chris at gmail.com
Wed Nov 12 13:05:28 CET 2008
On 12/11/2008, at 1:55 AM, Philipp Heuser wrote:
> Hi all,
>
> I am just starting to use the C API for Python. What I want to do is
> to use an external funtion availabele in C and therfore I want to
> 'convert' a list of floats from Python to an array of floats in C
> which I can forward to the external function. So first step is tp
> parse a list to C and let the C code print the variables! The
> obviously too simplistic approach is below... When I call this
> function from python, e.g. cpp([1,2,3]) the output is:
> 25180552
> 25180540
> 25180528
>
> Could you be so kind to help me here to get started with using C-
> functions from Python? Where is the error?
>
> Thank you very much and kind regards,
>
> Philipp
>
>
>
> static PyObject *
> cpp_foo(PyObject *self, PyObject* args)
> {
> PyObject* obj;
> PyObject* seq;
> int i, len;
> if(!PyArg_ParseTuple(args, "O", &obj))
> return NULL;
> seq = PySequence_Fast(obj, "expected a sequence");
> len = PySequence_Size(obj);
> PyObject* item;
> int a;
>
> for (i=0; i<len; i++)
> {
> item=PySequence_Fast_GET_ITEM(seq, i);
> a=item; // ?????????????????????????????????????????
Perhaps something like:
long a;
a = PyInt_AsLong(item);
if (a == -1 && PyErr_Occurred())
return NULL; /* error */
printf("%i\n", a);
Cheers,
Chris Miles
More information about the capi-sig
mailing list