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
2
votes
1 answer

Return CTypes pointer from C

I'm writing a Python C Extension that needs to return a CTypes pointer to a char array in memory (I need to interface with another Python library that expects a CTypes pointer). I cannot find any documentation on any kind of CTypes interface for C.…
Vicent Marti
  • 7,203
  • 6
  • 30
  • 34
2
votes
2 answers

Memory leak when embedding python into my application

The following program, when linked against python 2.7.13 and run on Windows 10 slowly but steadily leaks memory. #include #include int main() { std::cout << "Python version: " << PY_VERSION << std::endl; while (true) …
sigy
  • 2,408
  • 1
  • 24
  • 55
2
votes
2 answers

Extending Python with C: Convert PyStringObject to PyIntObject

With CPython 2.7, can a PyStringObject object be converted directly to a PyIntObject? Currently, I convert a Python string into a Python integer using two functions, PyString_AsString and PyInt_FromString: if (PyString_Check(pObj)) { /* Convert…
Matt P
  • 2,287
  • 1
  • 11
  • 26
2
votes
1 answer

Is the PyObject* returned by PyModule_Create 'borrowed' or 'acquired'?

The latest Python Doc only says this about PyModule_Create: The module initialization function may create and return the module object directly. This is referred to as “single-phase initialization”, and uses one of the following two module creation…
greatwolf
  • 20,287
  • 13
  • 71
  • 105
2
votes
1 answer

How to check if a PyObject is a String or Unicode for a Python C Extension

In Python(2), the idiomatic way to check if a variable is of str or unicode type is isinstance(var, basestr) In the Concrete Object Layer documentation, I did not see anything resembling basestr. Currently I am validating variables like the…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
2
votes
2 answers

Where can I find an instancemethod in the Python 3 standard library?

I'm trying to test for and fix a bug in pprint++ (edit: the correct link; original link left for posterity) which is coming up because the instancemethod type is not hashable: In [16]: import pandas as pd In [17]:…
David Wolever
  • 148,955
  • 89
  • 346
  • 502
2
votes
1 answer

How to terminate python script by C API

I want to run some python script in my application, but sometimes I want to terminate the script before it finishes and then run some other python script. I currently use PyRun_SimpleString(python_script) to run the script, but I don't know how to…
Fent
  • 31
  • 3
2
votes
1 answer

Python callbacks on different threads & the GIL

I am wrapping C functions that belong to a multi threaded C framework in a python module. In this framework there are callbacks that are executed when certain events are triggered. However, the callbacks are not always executed from the same thread.…
vriesdemichael
  • 914
  • 1
  • 7
  • 19
2
votes
2 answers

Python C/API: how to create a normal class

Using the Python C/API, how do I create a normal Python class using the normal Python class-creation mechanism (i.e.: not an extension type)? In other words, what is the Python C/API equivalent (in the sense that it does exactly the same in all…
2
votes
1 answer

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

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…
andre
  • 41
  • 6
2
votes
1 answer

Python C API PyObject_Repr Segmentation fault (core dumped)

I'm trying to use scikit learn in my C++ project. Here is the code I'm using: #include PyObject* loadModule(char* name)// { PyObject* pName = PyString_FromString(name); PyObject* pModule =…
nguyeh
  • 63
  • 1
  • 8
2
votes
2 answers

Distutils: Compiling an Objective-C++ source file as part of a C++ extension

I am writing a Python extension in C++. I compile it by defining a list of the constituent source files in my setup.py file, like so: extensions = { 'im': [ "im/src/buffer.cpp", "im/src/detail.cpp", "im/src/gil.cpp", …
fish2000
  • 4,289
  • 2
  • 37
  • 76
2
votes
1 answer

Segment fault when creating PyList_New in Python C extention

parser.cpp: #include #include #include #include #include using namespace std; extern "C" { PyObject* test(const char* filename) { size_t LIM = 1000; PyObject* result…
Aganov
  • 23
  • 3
2
votes
1 answer

Python C api PyImport_importmodule fail when the file has an import statement

I tried to use the Python C api to call a function from python in C++, the test was successful. But if I intend to import a module already importing other module, the Pymodule_findmodule will return Null even though it's there and created a compiled…
Hongjun Han
  • 55
  • 2
  • 6
2
votes
1 answer

Why am I getting this segfault when using the Python/C API?

I am getting a segmentation fault when decrefing a PyObject* in my C++ code using the Python/C API, and I can't figure out why. I am using C++ and Python 2.7. I am using new-style classes for future Python 3 compatibility. My goal is to create a…
Carrie D.
  • 188
  • 2
  • 12