[capi-sig] Beginner Question PyObject* to int

Philipp Heuser philipp at achtziger.de
Tue Nov 11 15:55:58 CET 2008


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;  // ?????????????????????????????????????????
        printf("%i\n", a);
    }
    
    Py_DECREF(seq);    
    return None;
}



More information about the capi-sig mailing list