FAQ Database Discussion Community


In Python C API why is wrapper function static

python,c,python-c-api
So I have below sample code for extending python with C #include <python.h> static PyObject* sayhello(PyObject* self, PyObject *args) { const char* name; if (!PyArg_ParseTuple(arg, "s", &name)) return NULL; printf("Hello %s !\n", name); Py_RETURN_NONE; } static PyMethodDef HelloMethods[] = { {"say_hello", say_hello, METH_VARARGS, "Greet Somebody."}, {NULL, NULL, 0, NULL} };...

Python C API - Reload a module

python,c++,python-3.x,python-c-api
I use Python 3.4 and Visual 2010. I'm embedding Python using the C API to give the user some script capabilities in processing his data. I call python functions defined by the user from my C++ code. I call specific function like Apply() for example that the user has to...

Embedding Python in Linux program

python,c++,linux,python-3.4,python-c-api
I am currently trying to embed Python in my C++ application to give the user advanced scripting possibilities. My program is working great on Windows so far (it is fully working), and now I'm trying to do the same on GNU/Linux (Debian 7 for now) but this is giving me...

C++-Python Interop: Marshalling data faster

python,c++,interop,python-c-api
I'm working on an app that has a C++ and Python API, and am implementing an API call that needs to be used in both languages. Part of the data this API sends back is an array of structs representing color information: struct { float r; float g; float b;...

How do I best parse a date string in the Python C API?

python-c-api
Unfortunately there seems to be no PyDateTime_FromString() (equivalent to e.g. PyFloat_FromString()) in the Python C API. Has anybody figured out what would be the ideal workaround if I have to parse a date string into a datetime.datetime type?

Allocating a dynamic array in a dynamically allocated struct (struct of arrays)

c,arrays,struct,malloc,python-c-api
This question is really about how to use variable-length types in the Python/C API (PyObject_NewVar, PyObject_VAR_HEAD, PyTypeObject.tp_basicsize and .tp_itemsize , but I can ask this question without bothering with the details of the API. Just assume I need to use an array inside a struct. I can create a list...

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 *....

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...

Return an array from python to C++

python,c++,arrays,python-c-api
I'm writing a c++ code to call a python function and the returned array from the python function will be store in an array in c++. I am able to call the python function in c++ but I am able to return only one value from Python to C++ and...

Convert Python dictionary into C like structure

python,c,dictionary,python-c-api
I am a newbie in Python and C and I would like to know how to put dictionary elements into a C like structure (struct). For example, here is my structure: typedef struct { int dim; float *Tab1; float *Tab2; } Tableaux; Here is my dictionary in Python: Tableaux =...

New python type created but PyGetSetDef core dumps

python,python-c-api,cpython
I have below code which tries to create a new type and use getter method to access value. #include <Python.h> #include <structmember.h> struct rangerr { long min; long max; }; //Python type to represent rangerr struct py_rangerr { PyObject_HEAD struct rangerr range; }; // * get & set methods for...