python-c-api

Python C-API Making len(...) work with extension class

When creating a class in Python, I can simply make a def __len__(self): method to make the len(InstanceOfMyClass) work, however I can't find out how to do this with an extension class via the C-API. I tried adding a __len__ method, but that appears to not work {"__len__",(PyCFunction)&TestClass_GetLen,METH_NOARGS,""}, Python test cod...

Python C-API module exit handler - an atexit equivalent?

I'm using Python ver 2.6.4 There is a function I have to call from a C library when my extension module exits/is unloaded. What would be the equivalent of atexit for a C extension module? ...

Selecting An Embedded Language

I'm making an application that analyses one or more series of data using several different algorithms (agents). I came to the idea that each of these agents could be implemented as separate Python scripts which I run using either the Python C API or Boost.Python in my app. I'm a little worried about runtime overhead TBH, as I'm doing so...

Access contents of PyBuffer from C

I have created a buffer object in python like so: f = io.open('some_file', 'rb') byte_stream = buffer(f.read(4096)) I'm now passing byte_stream as a parameter to a C function, through SWIG. I have a typemap for converting the data which looks like this: %typemap(in) unsigned char * byte_stream { PyObject *buf = $input; //some...

PyEval_CallObject failing in loop occasionally

Hi. I am struggling a bit with the Python C API. I am calling a python method to do some game AI at about 60hz. It works most of the time but every second or so the call to PyEval_CallObject results in a NULL return value. If I correctly detect the error and continue looping, all is well for the next second or so, whereupon the error o...

Assignment into Python 3.x Buffers with itemsize > 1

I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so: int Image_get_buffer(PyObject* self, Py_buffer* view, int flags) { int img_len; void* img_bytes; // Do my image fetch magic get_ima...

Pass array of structs from Python to C

[Update: Problem solved! See bottom of the post] I need to allow python developers to pass an array of packed data (in this case vertices) into my API, which is a series of C++ interfaces exposed manually through the Python C API. My initial impression with this is to use the ctypes Structure class to allow for an interface like this: ...

Static Variables in Python C API

How would one expose "static" variables like this class MyClass: X = 1 Y = 2 via the C API? The only variable on the PyTypeObject that looks like it would work is tp_members, but I see no flag in the PyMemberDef to indicate that the member should be per-class, not per-instance. For a bit more clarification, since it may chang...

Getting traceback from Python C API

I have a Python C API extension module which occassionally falls over with an uninformative "MemoryError". It's clearly not an error that's catered for by the module's exception handlers. How do I get a more informative error traceback so I can figure out what's gone wrong in the extension module? Perhaps the question should be, given ...

Implementing PyMyType_Check methods with Python C API?

All the Python-provided types have a check method (i.e., PyList_Check) that allows you to check if an arbitrary PyObject* is actually a specific type. How can I implement this for my own types? I haven't found anything good online for this, though it seems like a pretty normal thing to want to do. Also, maybe I'm just terrible at look...

To convert PyBytesObject type to PyUnicodeObject type in python3

How to convert pyunicodeobject type to pybytesobject type? Example: function(PyBytesObject* byteobj){ ....operation.. } PyUnicodeObject* Uniobj; function((PyBytesObject*) Uniobj); got a bus error as a result. ...

Execution issue with PyModule_AddIntConstant function

I m learning python c api functions and keen to learn python 3.1 stable version. Found a same kind of issue recently and tried PyModule_AddIntConstant(PyObject *module, const char *name, long value) Runtime error occurred for this function call. Is there something wrong with the function in python 3.1? ...

Are there any Python reference counting/garbage collection gotchas when dealing with C code?

Just for the sheer heck of it, I've decided to create a Scheme binding to libpython so you can embed Python in Scheme programs. I'm already able to call into Python's C API, but I haven't really thought about memory management. The way mzscheme's FFI works is that I can call a function, and if that function returns a pointer to a PyObj...

Creating exception class with custom fields in Python/C API

Hello! I'm writing a wrapper for C-library. When something goes wrong in that library, i can get error details. And i want to assign them to fields of my own exception class. For this purpose, i looked throught Modules/_ctypes/_ctypes.c (in python source tree) and implemented same things. Briefly: define PyObject * Error in header fi...

Extension modules and common routines in Python/C API

Hello! I'm writing wrapper for C library. This library consist of several parts. And i want split my extension package into different extension modules. I think, this is the right way ;-) But, there are some common parts that exist in extension package, get_library_version, Error, and so on. I've decided to create additional module, nam...

Returning a tuple of multipe objects in python C api

I am writing a native function that will return multiple python objects PyObject *V = PyList_New(0); PyObject *E = PyList_New(0); PyObject *F = PyList_New(0); return Py_BuildValue("ooo", V, E, F); This compiles fine, however, when I call it from python, I get an error\ SystemError: bad format char passed to Py_BuildValue How can t...

How to use python 2.6 from Visual Basic 2005?

What's the best way to call python scripts from Visual Basic 2005? I've got an application written in visual basic 2005 that needs to call into a library written in python. The library requires python 2.6. I'm using the python C API to access the python library from the visual basic code (private declare function blah lib "python26.dl...