2008-07-15: Selected articles now have experimental "comment" links in the left column and at the bottom. You're welcome to use them for commenting and voting on articles. For a bit more on this, see this page. /F

How do I extract C values from a Python object?

That depends on the object’s type. If it’s a tuple, PyTuple_Size returns its length and PyTuple_GetItem returns a given item. Lists have similar functions, PyList_Size and PyList_GetItem.

For strings, PyString_Size returns its length and PyString_AsString a pointer to its value. Note that Python strings may contain null bytes so C’s strlen should not be used.

To test the type of an object, first make sure it isn’t NULL, and then use PyString_Check, PyTuple_Check, PyList_Check, etc.

There is also a high-level API to Python objects which is provided by the so-called ‘abstract’ interface — read Include/abstract.h for further details. It allows interfacing with any kind of Python sequence using calls like PySequence_Length, PySequence_GetItem, etc.) as well as many other useful protocols.

CATEGORY: extending

CATEGORY: cpython

 

A Django site. this page was rendered by a django application in 0.01s 2008-07-25 10:35:17.808302. hosted by webfaction.