python-c-extension

Is it normal that running python under valgrind shows many errors with memory?

I've tried to debug memory crash in my Python C extension and tried to run script under valgrind. I found there is too much "noise" in the valgrind output, even if I've ran simple command as: valgrind python -c "" Valgrind output full of repeated info like this: ==12317== Invalid read of size 4 ==12317== at 0x409CF59: PyObject_Fre...

How to get the arch string that distutils uses for builds?

When I build a c extension using python setup.py build, the result is created under a directory named build/lib.linux-x86_64-2.6/ where the part after lib. changes by the OS, CPU and Python version. Is there a way I can access the appropriate string for my current architecture from python? Hopefully in a way that is guaranteed to mat...

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? ...

How to define initialized C-array in the Pyrex?

I want to define initialized C-array in Pyrex, e.g. equivalent of: unsigned char a[8] = {0,1,2,3,4,5,6,7}; What will be equivalent in Pyrex? Just array is cdef unsigned char a[8] But how can I made it initialized with my values? ...

python c extension, problems with dlopen on mac os

I've taken a library that is distributed as a binary lib (.a) and header, written some c++ code against it, and want to wrap the results up in a python module. I've done this here. The problem is that when importing this module on Mac OSX (I've tried 10.5 and 10.6), I get the following error: dlopen(/Library/Python/2.5/site-packages/...

Python C API: Switch on PyObject type

I have some code to interface Python to C++ which works fine but every time I look at it I think there must be a better way to do it. On the C++ side there is a 'variant' type that can deal with a fixed range of basic types - int, real, string, vector of variants, etc. I have some code using the Python API to convert from the equivalen...

PyArg_ParseTuple causing segmentation fault

I'm trying to call a c function from my extension and have narrowed the problem down to this test case. #import "Python.h" ... // Called from python with test_method(0, 0, 'TEST') static PyObject* test_method(PyObject *args) { int ok, x, y, size; const char *s; // this causes Segmentation fault //ok = PyArg_ParseTupl...

How to use C extensions in python to get around GIL

I want to run a cpu intensive program in Python across multiple cores and am trying to figure out how to write C extensions to do this. Are there any code samples or tutorials on this? ...

PyArg_ParseTuple and a callback function pointer

I have code like the following: PyObject *callback; PyObject *paths; // Process and convert arguments if (!PyArg_ParseTuple(args, "OO:schedule", &paths, &callback)) return NULL; What exactly happens inside PyArg_ParseTuple? My guess is that callback gets the function pointer I passed to args (also PyObject...

Python and OpenMP C Extensions

I have a C extension in which I'd like to use OpenMP. When I import my module, though, I get an import error: ImportError: /home/.../_entropysplit.so: undefined symbol: GOMP_parallel_end I've compiled the module with -fopenmp and -lgomp. Is this because my Python installation wasn't compiled with the -fopenmp flag? Will I have to bui...

Python C extension not threadsafe?

I made a c extension out of a python script that was fairly labour intensive. The code itself is well tested and simple. The c extension is called with a few large lists, and it then performs some clever arithmetic and returns a few new lists. The c extension is 100% self sufficient, it doesn't use any other c functions nor does it use a...

What is the easiest way to make an optional C extension for a python package?

I've created a C extension that I'd like to enable in my Python package (using setuptools) only if a command line option is passed in. What is the easiest way to do this? I can't seem to find any straightforward ways of going about this. ...

Why is this C method segfaulting?

I'm writing an immutable linked list class in C, but one method is mysteriously segfaulting. The code is intended to be roughly equivalent to this: class PList(object): def __init__(self, first, rest=None): self.first = first self.rest = rest def cons(self, item): return PList(item, self) Here is my c...