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
11
votes
7 answers

Is it possible to modify PYTHONPATH at runtime?

I have a C++ application dynamically linked to the Python interpreter. I want to be able to import python modules from a particular directory. I want to modify the PYTHONPATH for my process so that sys.path will include the paths that I added to…
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
11
votes
1 answer

Add a signature, with annotations, to extension methods

When embedding Python in my application, and writing an extension type, I can add a signature to the method by using a properly crafted .tp_doc string. static PyMethodDef Answer_methods[] = { { "ultimate", (PyCFunction)Answer_ultimate,…
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
11
votes
5 answers

In Python, why is a module implemented in C faster than a pure Python module, and how do I write one?

The python documentation states, that the reason cPickle is faster than Pickle is, that the former is implemented in C. What does that mean exactly? I am making a module for advanced mathematics in Python, and some calculations take a significant…
Pushpak Dagade
  • 6,280
  • 7
  • 28
  • 41
11
votes
2 answers

How can I convert Python boolean object to C int (or C++ boolean) (Python C API)

I have a variable PyObject that I know is a Python bool. It either is True or False (eg. Py_True or Py_False). Now I would like to convert it to C++ somehow. Doing this with strings isn't so hard, there is a helper function for that -…
Petr
  • 13,747
  • 20
  • 89
  • 144
11
votes
2 answers

Python extension - construct and inspect large integers efficiently

I have a native library for which a natural interface would involve passing potentially large numbers. I anticipate about half being < 32 bits; another quarter < 64 bits; the next eighth < 128 bits - and so on, without a fixed length…
aSteve
  • 1,956
  • 1
  • 20
  • 35
11
votes
1 answer

Returning numpy array from a C extension

For the sake of learning something new, I am currently trying to reimplement the numpy.mean() function in C. It should take a 3D array and return a 2D array with the mean of the elements along axis 0. I manage to calculate the mean of all values,…
karlson
  • 5,325
  • 3
  • 30
  • 62
11
votes
2 answers

C array to PyArray

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…
Daniele Bigoni
  • 213
  • 1
  • 3
  • 8
11
votes
5 answers

How can I reference #defines in a C file from python?

I have a C file that has a bunch of #defines for bits that I'd like to reference from python. There's enough of them that I'd rather not copy them into my python code, instead is there an accepted method to reference them directly from…
mfisch
  • 979
  • 9
  • 15
10
votes
1 answer

Python C API: Using PyEval_EvalCode

I'm trying to figure out how to use the Python interpreter from C, and I'm having trouble with PyEval_EvalCode. Basically, I'm writing a C function which takes in an arbitrary string of Python code, compiles it, executes it, and then prints out the…
Channel72
  • 24,139
  • 32
  • 108
  • 180
10
votes
1 answer

Self-built extension module slower than built-in c module

To learn how to create C-extensions I've decided to just copy a built-in .c-file (in this case itertoolsmodule.c) and placed it in my package. I only changed the names inside the module from itertools to mypkg. Then I compiled it (Windows 10, MSVC…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
10
votes
1 answer

How to pass const char* from python to c function

I am using ctypes in Python to open a file for writing in C++. My C++ code: extern "C" { void openfile(const char *filename) { cout<<"File to open for writing = " <
Mahesh Chaurasia
  • 133
  • 1
  • 1
  • 10
10
votes
1 answer

Python C API How to pass array of structs from C to Python

For a python module I'm creating, I want to pass to the python user an array of structs like this: struct tcpstat { inet_prefix local; inet_prefix remote; int lport; int rport; int state; int rq, wq; int …
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
10
votes
1 answer

Python Callback from SWIG PyObject_Call Segfault

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…
Tocs
  • 752
  • 4
  • 17
10
votes
2 answers

(Python C API) PyRun_StringFlags missing builtin functions?

I am trying to embed some python in my pet project. I have reduced my problem to the following code: #include #include "iostream" int main(int argc, char *argv[]) { Py_Initialize(); PyObject *globals = Py_BuildValue("{}"); …
Simon
  • 961
  • 2
  • 9
  • 14
9
votes
4 answers

typecasting PyArrayObject data to a C array

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