FAQ Database Discussion Community


Is there a PyMOD(DEINIT)_FUNC?

python,python-3.4,python-c-extension
I want to know whether there is a PyMOD(DEINIT)_FUNC?, I know that resources are released when the python script exits, but I would like to make my code as responsible for the memory it uses as possible. I have searched the documentaion of course, and until now I think there...

How to wrap a C pointer and length in a new-style buffer object in Cython?

python,python-2.7,cython,python-c-extension,memoryview
I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of memory is just a string of bytes, not a structure or multidimensional array....

Using a Python 2.7 enum from C

python,python-2.7,python-c-api,python-c-extension
I have an enum in Python (backported enum package to 2.7) that is meant to be of only integers: import enum class MyEnum(enum.Enum): val = 0 Let's say I receive a PyObject * in a C extension pointing to MyEnum.val. I want the integer value associated with the PyObject *....

How to link python's static library for C extensions?

python,c++,linker,python-c-extension
I am trying to write an C++ extension for python which involves Python C API. According to my previous experience on doing this with Visual Studio, I just need to include python's header files and link python's static libraries and everything would work fine. However I couldn't do this on...

How to use Cython typed memoryviews to accept strings from Python?

python,python-2.7,cython,python-c-extension,memoryview
How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview? According to the Unicode and Passing Strings Cython tutorial page, the following should work: cpdef object printbuf(unsigned char[:] buf): chars...

Function in C extension randomly stops python program execution

python,c,memory-management,random,python-c-extension
I am relatively new to C extensions for python. I wrote an extension that shows a behavior that seems weird to me. When I run a python script using this extension the script stops randomly after the routine in the extensions has been successfully executed. That is, I have a...

How to handle the `key' argument in PyObject_GetItem() function

python,c,python-c-extension
I am writing a python module from a c library of my own, I need to know how to handle the PyObject_GetItem() function, specifically the key argument, the documentation is not very helpful and I have seen an example where the solution is Py_hash_t hash; hash = PyObject_Hash(key); but the...

Python extension creates invalid pointers when manipulating large lists

python,c++,python-3.x,python-c-api,python-c-extension
I managed to implement a Fisher–Yates shuffle function for python lists as an exercise for getting used to extending python. It works perfectly for relatively small lists, unless I run the function several times. Whenever the list size goes over about 100, I get all kinds of memory problems: >>>import...

Python C Extension Keyword arguments

python,c,python-c-extension
Description of problem: When I call method defined like this: static PyMethodDef Parser_methods[] = { {"read", (PyCFunction)Parser_read, METH_KEYWORDS, "read from input source"}, {NULL, NULL, 0, NULL} }; static PyObject * Parser_read(Parser * const self, PyObject * unused0, PyObject * unused1) { ... } I got: SystemError: Bad call flags in...