python-c-extension

When is PyEval_InitThreads meant to be called? [duplicate]

。_饼干妹妹 提交于 2019-12-04 07:39:18
This question already has answers here : PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam) (7 answers) Closed 6 years ago . I'm a bit confused about when I'm supposed to call PyEval_InitThreads . In general, I understand that PyEval_InitThreads must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used. However, I'm confused if PyEval_InitThreads is for C programs which embed the Python interpreter, or Python programs which import C-extension modules, or both. So, if I write a C extension module that will

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

心不动则不痛 提交于 2019-12-03 16:32:31
问题 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 = [chr(x) for x in buf] print repr(''.join(chars)) It does work for bytearrays and other writable buffers: $ python -c 'import test; test.printbuf(bytearray("test\0ing"))' 'test

Returning objects to Python from C

廉价感情. 提交于 2019-12-03 14:02:05
I've read the documentation for the Python C-API, and even written a few extension modules. However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function. The limited examples in the Python docs usually show a C function which returns the result of Py_BuildValue . Now, Py_BuildValue returns a New Reference , and transfers ownership of this reference over to the interpreter. So, can I extrapolate from this that it is a general rule that any object returned to Python must be a new reference , and that returning an object from a C function is

AssertionError (3.X only) when calling Py_Finalize with threads

跟風遠走 提交于 2019-12-02 04:41:17
问题 I'm getting an error output when I call the C-API's Py_Finalize() from a different C-thread than I made a python call on. The error I'm seeing is: Exception ignored in: <module 'threading' from 'C:\\Python34-32\\Lib\\threading.py'> Traceback (most recent call last): File "C:\Python34-32\Lib\threading.py", line 1289, in _shutdown assert tlock.locked() AssertionError: This only happens in Python 3.X (tested with 3.4.2), in Python 2.7 the exact same code doesn't have any issues. Here's a minimal

Is there any reason to use malloc over PyMem_Malloc?

人走茶凉 提交于 2019-12-01 16:04:52
I'm reading the documentation for Memory Management in Python C extensions , and as far as I can tell, there doesn't really seem to be much reason to use malloc rather than PyMem_Malloc . Say I want to allocate an array that isn't to be exposed to Python source code and will be stored in an object that will be garbage collected. Is there any reason to use malloc ? EDIT : Mixed PyMem_Malloc and PyObject_Malloc corrections; they are two different calls. Without the PYMALLOC_DEBUG macro activated, PyMem_Malloc is an alias of libc's malloc() , having one special case: calling PyMem_Malloc to

Is there any reason to use malloc over PyMem_Malloc?

回眸只為那壹抹淺笑 提交于 2019-12-01 15:07:58
问题 I'm reading the documentation for Memory Management in Python C extensions, and as far as I can tell, there doesn't really seem to be much reason to use malloc rather than PyMem_Malloc . Say I want to allocate an array that isn't to be exposed to Python source code and will be stored in an object that will be garbage collected. Is there any reason to use malloc ? 回答1: EDIT : Mixed PyMem_Malloc and PyObject_Malloc corrections; they are two different calls. Without the PYMALLOC_DEBUG macro

Python C Extension - Why are methods that use keyword arguments cast to PyCFunction

你离开我真会死。 提交于 2019-12-01 04:11:55
I've am learning about Python-C extensions and am puzzled as to why methods that use keyword arguments must be cast to PyCFunctions. My understanding of a PyCFunction is that it takes two pointers to PyObjects and returns a single pointer to a PyObject - e.g. PyObject* myFunc(PyObject* self, PyObject* args) If I'm going to use a function that uses keyword arguments, then this function will take three pointers to PyObjects and returns a single pointer to a PyObject - e.g. PyObject* myFunc(PyObject* self, PyObject* args, PyObject* keywordArgs) However, when I create the module functions array

Python C Extension - Why are methods that use keyword arguments cast to PyCFunction

孤街浪徒 提交于 2019-12-01 01:29:54
问题 I've am learning about Python-C extensions and am puzzled as to why methods that use keyword arguments must be cast to PyCFunctions. My understanding of a PyCFunction is that it takes two pointers to PyObjects and returns a single pointer to a PyObject - e.g. PyObject* myFunc(PyObject* self, PyObject* args) If I'm going to use a function that uses keyword arguments, then this function will take three pointers to PyObjects and returns a single pointer to a PyObject - e.g. PyObject* myFunc

Difference between PyMODINIT_FUNC and PyModule_Create

坚强是说给别人听的谎言 提交于 2019-11-30 17:41:31
If I'm understanding correctly, PyMODINIT_FUNC in Python 2.X has been replaced by PyModule_Create in Python3.X Both return PyObject* , however, in Python 3.X, the module's initialization function MUST return the PyObject* to the module - i.e. PyMODINIT_FUNC PyInit_spam(void) { return PyModule_Create(&spammodule); } whereas in Python2.X, this is not necessary - i.e. PyMODINIT_FUNC initspam(void) { (void) Py_InitModule("spam", SpamMethods); } So, my sanity checking questions are: Is my understanding correct? Why was this change made? Right now, I'm only experimenting with very simple cases of C

How can I reference #defines in a C file from python?

爱⌒轻易说出口 提交于 2019-11-30 15:15:54
问题 I have a C file that has a bunch of #defines for bits that I'd like to reference from python. There's enough of them that I'd rather not copy them into my python code, instead is there an accepted method to reference them directly from python? Note: I know I can just open the header file and parse it, that would be simple, but if there's a more pythonic way, I'd like to use it. Edit: These are very simple #defines that define the meanings of bits in a mask, for example: #define FOO_A 0x3