python-c-api

Passing a C struct to a Python function

左心房为你撑大大i 提交于 2019-12-10 17:35:47
问题 I need a simple way to pass a C struct to a Python function. I have embedded Python into a game server, and I intend to write game logic in Python. I've scoured Google and mailing lists and found nothing useful. I have a complex structure in C (with pointers to other relatively complex structures) and have found no reasonable way of doing this. I have this struct: struct client { int state; int sockfd; struct sockaddr_in *addr; struct epoll_event *epollev; struct buffer *in_buffer; struct

Const correctness of Python's C API

这一生的挚爱 提交于 2019-12-10 13:05:20
问题 It seems that the Python C API is not consistent with the const correctness of character arrays. For example, PyImport_ImportFrozenModule accepts a char* , whereas PyImport_ImportModule accepts a const char* . The implication of all this is that in my C++ application that I am writing with an embedded Python interpreter, I sometimes have to cast the string literal that I pass to a Python API call as just a char* (as opposed to const char* ), and sometimes I don't. For example: PyObject *os =

Numpy C-Api array_equal

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:41:51
问题 I've tried to find function comparing two PyArrayObject - something like numpy array_equal But I haven't found anything. Do you know function like this? If not - How to import this numpy array_equal to my C code? 回答1: Here's the code for array_equal : def array_equal(a1, a2): try: a1, a2 = asarray(a1), asarray(a2) except: return False if a1.shape != a2.shape: return False return bool(asarray(a1 == a2).all()) As you can see it is not a c-api level function. After making sure both inputs are

typecasting PyArrayObject data to a C array

时间秒杀一切 提交于 2019-12-09 18:08:28
问题 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

c++0x std::shared_ptr vs. boost::shared_ptr

扶醉桌前 提交于 2019-12-09 16:14:09
问题 I have a c++ code which heavily uses shared_ptr and STL. A common header says #include<boost/shared_ptr.hpp> using boost::shared_ptr; // for shared_ptr using namespace std; // for STL I wanted to switch to c++0x now to make use of the language features, using gcc 4.6 with -std=c++0x . There is however std::shared_ptr now as well, leading to ambiguity for unspecified shared_ptr ( boost::shared_ptr vs std::shared_ptr ). When switching to std::shared_ptr instead, like this: #include<memory>

Modifying or Reraising Python error in C API

给你一囗甜甜゛ 提交于 2019-12-09 04:54:26
I have a bit of code that tries to parse an object as an integer: long val = PyLong_AsLong(obj); if(val == -1 && PyErr_Occurred()) { return -1; } Here obj is a vanilla PyObject * , and PyLong_AsLong raises a very generic TypeError if obj is not an integer. I would like to transform the error message into something a bit more informative, so I would like to either modify the existing error object, or to reraise it. My current solution is to do this: long val = PyLong_AsLong(obj); if(val == -1 && PyErr_Occurred()) { PyErr_Clear(); PyErr_Format(PyExc_TypeError, "Parameter must be an integer type,

How does PyArg_ParseTupleAndKeywords work?

◇◆丶佛笑我妖孽 提交于 2019-12-09 02:20:09
问题 I've been trying to learn how to write C-extensions for Python and want to be sure I understand how PyArg_ParseTupleAndKeywords works. I believe that the first argument is a PyObject pointer that points to an array of the arguments being passed into the C-extension function in the order they were passed. The second argument is a list of keywords that were passed, the positions at which they were passed and, very likely, some sort of indicator flag telling at which position the keywords begin

Python instance method in C

夙愿已清 提交于 2019-12-09 01:06:07
问题 Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo() How to write the same functionality in C? I mean, how do I create an object with a method in C? And then create an instance from it? Edit : Oh, sorry! I meant the same functionality via Python C API. How to create a Python method via its C API? Something like: PyObject *Foo = ?????; PyMethod??? *bar = ????; 回答1: Here's a simple class (adapted from http://nedbatchelder.com/text/whirlext.html for 3.x):

Python Callback from SWIG PyObject_Call Segfault

风流意气都作罢 提交于 2019-12-08 17:26:31
问题 I have a wx.py.Shell.shell widget which lets the user execute python code that interacts with my program. I want to be able to pass a function that the user defines in this space to my C++ code (Through the wxswig generated wrapper around my custom widget)and execute it. In my C++ code I'm using a std::function <> class to invoke bound functions (C++ or Python) So I created a simple class to wrap the PyObject with the function call operator. However I get a segfault when I try to call the

C array to PyArray

左心房为你撑大大i 提交于 2019-12-08 16:37:20
问题 I'm writing a Python C-Extension without using Cython. I want to allocate a double array in C, use it in an internal function (that happens to be in Fortran) and return it. I point out that the C-Fortran interface works perfectly in C. static PyObject * Py_drecur(PyObject *self, PyObject *args) { // INPUT int n; int ipoly; double al; double be; if (!PyArg_ParseTuple(args, "iidd", &n, &ipoly, &al, &be)) return NULL; // OUTPUT int nd = 1; npy_intp dims[] = {n}; double a[n]; double b[n]; int