Questions tagged [python-c-api]

API used by C and C++ programmers who want to write extension modules or embed Python.

The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use. The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

Writing an extension module is a relatively well-understood process, where a “cookbook” approach works well. There are several tools that automate the process to some extent. While people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

Many API functions are useful independent of whether you’re embedding or extending Python; moreover, most applications that embed Python will need to provide a custom extension as well, so it’s probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Reference: http://docs.python.org/c-api/intro.html

1096 questions
1
vote
2 answers

Memory Leaks and Reference Counting in Python/C API

I am pretty new to Python and its C API. I still do not understand how reference counting works. I have written a module for particle tracking that exposes to python a number of C++ thread tracking functions that I had written and tested in the…
1
vote
1 answer

Python-C API: Trouble packing C strings into a tuple

In the following code, I am trying to pack two null-terminated C strings (char pointers) into a Python tuple. printf("word1 = '%s', word2 = '%s'\n", words1->wordArray[i], words2->wordArray[i]); cmpArgs = Py_BuildValue("ss", words1->wordArray[i],…
dpitch40
  • 2,621
  • 7
  • 31
  • 44
1
vote
1 answer

Creating a dynamic Array in numpy Capi

I have a dynamic 2 dimensional C array, for example an array that created by this code: double…
1
vote
1 answer

Implementing nb_inplace_add results in returning a read-only buffer object

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…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
1
vote
1 answer

Integrating C and Python: ValueError: module functions cannot set METH_CLASS or METH_STATIC

I am making my first venture into integrating C and Python 2.7.3. For starters, I'm just trying to write a C module for Python that can do basic addition. (It is called npfind because once I figure this out, I want to write a find method for…
dpitch40
  • 2,621
  • 7
  • 31
  • 44
1
vote
2 answers

doubles in embedded python

So I'm writing a set of C++ classes that run Python scripts. I've gotten code working to run any script I want from a specified directory, and I can pass and return values just fine. But the one issue I have is I can't seem to find out how to set…
Mister R2
  • 861
  • 5
  • 12
  • 22
1
vote
3 answers

It looks like C code in Python C-API returns ptr to stack variable. What am I missing?

I was reading through the file methodobject.c, because I'm trying to learn about making C extensions for Python, when I saw the following code snippet: PyObject * PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) { PyCFunctionObject*…
user1245262
  • 6,968
  • 8
  • 50
  • 77
1
vote
1 answer

"overloading" doesn't work with stderr

/*---------------stdcallbk_module.h---------------------*/ #ifndef STDCALLBK_MODULE_H #include #define STDCALLBK_MODULE_H // Type definition for the callback function. typedef void __stdcall(CALLBK_FUNC_T)(const char* p); #ifdef…
user1373893
  • 175
  • 7
1
vote
2 answers

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

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…
Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
1
vote
1 answer

Convert Python long integer to C char array

I am using the Python/C API and would like a C function to take a Python long integer argument, nPyLong, and convert it to a char array, nString, in base 10. Here is a snippet of what I would like to do. PyObject *nPyLong; PyArg_ParseTuple(args,…
Vortico
  • 2,610
  • 2
  • 32
  • 49
0
votes
0 answers

threading.Condition C API

Is there a threading.Condition-like object exposed directly in the Python C API? If not, what are my options?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
0
votes
1 answer

Coerce a numpy array scalar to a particular C type using numpy C API

I have a PyObject* representing a numpy scalar array and I would like to check whether I can coerce the value to a float and then, if so, pull out a C float. I've been through the numpy C api docs in some detail but have not managed to do this.
Alex Flint
  • 6,040
  • 8
  • 41
  • 80
0
votes
1 answer

convert cstring to python string object

I have a function wrapper for c and python. This wrapper will receive 2 string arguments and convert these two into a tuple and pass it to python script. fileName and className are the c string arguments. pFunc is the python function name. PyObject…
PeterG
  • 519
  • 2
  • 7
  • 15
0
votes
1 answer

convert PyInt to C Int

i need to convert PyInt to C int. In my code count=PyInt_FromSsize_t(PyList_Size(pValue)) pValue is a PyObject, PyList. the problem i was having is the PyList_Size is not returning me the correct list size (count is supposed to be 5, but it gave me…
PeterG
  • 519
  • 2
  • 7
  • 15
0
votes
1 answer

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

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
mehmet.ali.anil
  • 505
  • 6
  • 17