python-c-api

How does Python 3 know how to pickle extension types, especially Numpy arrays?

梦想与她 提交于 2019-12-04 13:02:41
Numpy arrays, being extension types (aka defined using in extensions the C API), declare additional fields outside the scope of the Python interpreter (for example the data attribute, which is a Buffer Structure , as documented in Numpy's array interface . To be able to serialize it, Python 2 used to use the __reduce__ function as part of the pickle protocol, as stated in the doc , and explained here . But, even if __reduce__ still exists in Python 3, the Pickle protocol section (and Pickling and unpickling extension types a fortiori) was removed from the doc, so it is unclear what does what.

Passing arguments to tp_new and tp_init from subtypes in Python C API

梦想与她 提交于 2019-12-04 10:42:31
问题 I originally asked this question on the Python capi-sig list: How to pass arguments to tp_new and tp_init from subtypes? I'm reading the Python PEP-253 on subtyping and there are plenty of good recommendations on how to structure the types, call tp_new and tp_init slots, etc. But, it lacks an important note on passing arguments from sub to super type. It seems the PEP-253 is unfinished as per the note: (XXX There should be a paragraph or two about argument passing here.) So, I'm trying to

Pass array of structs from Python to C

℡╲_俬逩灬. 提交于 2019-12-04 10:42:26
问题 [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: class Vertex(Structure): _fields_ = [ ('x', c_float), ('y', c_float), ('z', c_float), ('u', c_float), ('v', c_float), ('color', c_int) ] verts = (Vertex * 3)()

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

Import and use standard Python module from inside Python C extension

独自空忆成欢 提交于 2019-12-04 07:31:17
I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os or shutil . How is best to do this? PyObject* os = PyImport_ImportModuleNoBlock("os"); if (os == NULL) return NULL; someattr = PyObject_GetAttrString(os, "someattr"); Py_DECREF(os); If you import the module only once e.g., in init_yourmodule() function then use PyImport_ImportModule("os") . Don't. Instead, change your extension module so that it provides a service to Python, and then write Python code which calls os , shutil and your module. In fact, for a lot of the

typecasting PyArrayObject data to a C array

核能气质少年 提交于 2019-12-04 06:08:32
I want to work with my Numpy arrays in a C extension. Many examples in this case uses the structure of PyArrayObject, array->data , array->strides[0] , array->strides[1] , ... pointers in order to reach the data, if I wanted to reach my array in a more familiar (or tidier) way to me, with indices like array[i][j] how should I proceed so? Should I typecast (bool *) array->data and work with the C array I created? (my elements are bools) My function declaration for now is (not finished, of course) static PyObject * xor_masking(PyObject *self, PyObject *args) { PyObject *input; PyObject *mask;

PyDict_SetItemString segfaults

感情迁移 提交于 2019-12-04 04:53:36
问题 I am trying to write a simple C extension for Python3, and it segfaults when I try to add a string to a dictionary. Here is my code: #include <stdio.h> #include <Python.h> int main() { PyObject* dict_p = PyDict_New(); char *val = "idjewijjd"; PyObject* val_p = PyUnicode_FromString(val); const char *key = "dhsjdshj"; for (int j=0; j<8; j++) { printf("%d\n", PyUnicode_READ_CHAR(val_p,j)); } int r = PyDict_SetItemString(dict_p, key, val_p); return 0; } I compile it like this gcc t.c $(python3

Python C API: how to get string representation of exception?

荒凉一梦 提交于 2019-12-04 02:58:52
If I do (e.g.) open("/snafu/fnord") in Python (and the file does not exist), I get a traceback and the message IOError: [Errno 2] No such file or directory: '/snafu/fnord' I would like to get the above string with Python's C API (i.e., a Python interpreter embedded in a C program). I need it as a string, not output to the console. With PyErr_Fetch() I can get the type object of the exception and the value. For the above example, the value is a tuple: (2, 'No such file or directory', '/snafu/fnord') Is there an easy way from the information I get from PyErr_Fetch() to the string the Python

PyObject segfault on function call

半腔热情 提交于 2019-12-04 02:13:07
问题 I'm trying to use Python to open a dialog to accept input into my C++ application. Here is a very minimal representation of what I am trying to do: #include <iostream> #include <Python.h> int main() { /* Begin Python Ititialization - only needs to be done once. */ PyObject *ip_module_name = NULL; PyObject *ip_module = NULL; PyObject *ip_module_contents = NULL; PyObject *ip_module_getip_func = NULL; Py_Initialize(); PyEval_InitThreads(); ip_module_name = PyString_FromString( "get_ip" ); ip

Build a PyObject* from a C function?

耗尽温柔 提交于 2019-12-04 01:37:29
I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*); so that I can use those functions in the embedded Python. So I have a function pointer and I know that it is possible to put this function as a module's method using a PyMethodDef struct and passing it to Py_InitModule("module", ModMethods); and thus obtaining a PyObject* module which I can easily grab functions from. But, what I would really appreciate is to be able to create this function on the fly without