python-c-api

Embedding Python into C - importing modules

只谈情不闲聊 提交于 2020-01-12 05:25:09
问题 I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an : Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004. The error occurs in the PyObject_GetAttrString() method and the documentation isn't much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation. The following is the example code from one of the tutorials

SegFault when trying to write to a Numpy array created within a C Extension

眉间皱痕 提交于 2020-01-06 12:26:55
问题 I have an if clause within a for loop in which I have defined state_out beforehand with: state_out = (PyArrayObject *) PyArray_FromDims(1,dims_new,NPY_BOOL); And the if conditions are like this: if (conn_ctr<sum*2){ *(state_out->data + i*state_out->strides[0]) = true; } else { *(state_out->data + i*state_out->strides[0]) = false; } When commenting these out, state_out returns as an all-False Numpy array. There is a problem with this assignment that I fail to see. As far as I know, all within

Implementing nb_inplace_add results in returning a read-only buffer object

隐身守侯 提交于 2020-01-05 12:13:50
问题 I'm writing an implementation of the in-place add operation. But, for some reason, I sometimes get a read-only buffer as result(while I'm adding a custom extension class and an integer...). The relevant code is: static PyObject * ModPoly_InPlaceAdd(PyObject *self, PyObject *other) { if (!ModPoly_Check(self)) { //Since it's in-place addition the control flow should never // enter here(I suppose) if (!ModPoly_Check(other)) { PyErr_SetString(PyExc_TypeError, "Neither argument is a ModPolynomial.

Is the python C API entirely compatible with C++?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 03:52:09
问题 As I understand the relationship between C and C++, the latter is essentially an extension of the former and retains a certain degree of backwards compatibility. Is it safe to assume that the python C API can be called with C++ code? More to the point, I notice that the official python documentation bundles C and C++ extensions together on the same page. Nowhere am I able to find a C++ API. This leads me to believe that the same API is safe to use in both languages. Can someone confirm or

Adding symbolic constants with hex values to Python extension module

筅森魡賤 提交于 2020-01-04 17:21:39
问题 I have a few values defined as symbolic constants in my header file: #define NONE 0x00 #define SYM 0x11 #define SEG 0x43 ... The names of these values represent a certain type of data. Now in my current implementation of my module I put all these symbolic links into an array static unsigned char TYPES[] = { NONE, SYM, SEG, ...} And add the positions of the types in the array as int constants in the module. PyMODINIT_FUNC initShell(void) { PyObject *m; m= Py_InitModule3("Sample", sample

Adding symbolic constants with hex values to Python extension module

你。 提交于 2020-01-04 17:21:05
问题 I have a few values defined as symbolic constants in my header file: #define NONE 0x00 #define SYM 0x11 #define SEG 0x43 ... The names of these values represent a certain type of data. Now in my current implementation of my module I put all these symbolic links into an array static unsigned char TYPES[] = { NONE, SYM, SEG, ...} And add the positions of the types in the array as int constants in the module. PyMODINIT_FUNC initShell(void) { PyObject *m; m= Py_InitModule3("Sample", sample

Is it possible to cast dtype of scipy CSR matrix to NPY_FLOAT?

回眸只為那壹抹淺笑 提交于 2020-01-04 06:07:17
问题 I have a scipy CSR matrix that was constructed from a COO matrix as follows: coord_mat = coo_matrix((data, (row, col)), dtype=np.float64) It is being used as an input to a library with an underlying C implementation, and I believe that the dtype of my matrix is double(np.float64) . However, I'm encountering the following error: ValueError: Buffer dtype mismatch, expected 'flt' but got 'double' I went to do some research and found the scipy C-api, which informed me that the NPY_FLOAT data type

Creating a numpy array of custom-class objects with C API

一曲冷凌霜 提交于 2020-01-03 14:15:19
问题 Using the C API, I would like to create a numpy array containing objects of type Quaternion , which is a class I've written in C++. I already have an array of these (actually a std::vector ), and I want to make a copy -- or use the same memory if possible. Since this isn't a basic type, I need to use Py_Object types, and can't use PyArray_SimpleNew or anything easy like that. I'm guessing I might want to use PyArray_NewFromDescr or even PyArray_SimpleNewFromDescr , but I am completely and

NumPy C-API: convert type object to type number

强颜欢笑 提交于 2020-01-02 04:00:07
问题 The function PyObject* PyArray_TypeObjectFromType(int); converts the type number for a NumPy scalar type (NPY_BOOL, NPY_BYTE, ...) to the corresponding type object. How do you do the opposite conversion, from the type object for a NumPy scalar type to the corresponding type number? Edit: The following code is based on kwatford's answer. It accepts both type objects such as int and numpy.int16, and strings such as "int", u"int" and "int16". int numpyScalarTypeNumber(PyObject* obj) { PyArray

Limitations of PyTuple_SetItem

蹲街弑〆低调 提交于 2020-01-02 01:20:50
问题 I have a Python extension module which creates a tuple as an attribute of another object, and sets items in the tuple. Whenever I execute this module in Python, I keep getting the error SystemError: bad argument to internal function After reading over the docs for PyTuple , and debugging my program for a few hours, I still couldn't figure out what the hell was going on. Running my program through a debugger indicated the problem was occurring within a library call inside the Python