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

Cannot import C++ extension if in the package root directory

Note: Since asking this question, I discovered later on that python -m pip install -e . will install the extension to cmod with .venv/lib/python3.8/site-packages/hello-c-extension.egg-link pointing to the project in the current directory. I've also…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
5
votes
1 answer

GCOV Version mismatch - expected 700e got 408R

On one server with GCC 4.4.7/GCOV 4.4.7, I'm able to run the tests successfully. However on a different server with GCC 4.8.5/GCOV 4.8.5, running the tests results in this error: profiling:/path/to/foo.gcda:Version mismatch - expected 700e got…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
5
votes
1 answer

Embedding Python in C/C++

For my senior project in Computer Science, I am making a puzzle video game that will teach people how to code in Python. The largest portion of the design involves creating engaging puzzles that the user can sort through. The puzzle design isn't the…
5
votes
1 answer

Python c-api and unicode strings

I need to convert between python objects and c strings of various encodings. Going from a c string to a unicode object was fairly simple using PyUnicode_Decode, however Im not sure how to go the other way //char* can be a wchar_t or any other…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
5
votes
2 answers

How to `pop` element from PyListObject?

Lets say I have a PyListObject, and I want to append a PyObject then I can use PyList_Append API which is documented in List Objects C-API. But for my use case I want to pop an element from the PyListObject(i.e. my_list.pop() in python layer). But…
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
5
votes
1 answer

Modifying or Reraising Python error in C API

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…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
5
votes
1 answer

How to return a list of ints in Python C API extension with PyList?

I am building a Python extension (.pyd) using Visual Studio 2015 C++ project and Python 2.7 32bit. This is my .cpp file: #include static PyObject* GetTwoInts(PyObject* self, PyObject* args) { srand(time(NULL)); int random1 =…
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
5
votes
1 answer

How to pip install package without building its' C extensions?

Is it possible to pip install package which provides optional C-extension, but specifically disable process of compiling those extensions? Sample examples of such packages are: _speedups in markupsafe _posixsubprocess32 in subprocess32
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
5
votes
2 answers

Python C API: PyEval_CallFunction?

I've discovered a function in the Python C API named PyEval_CallFunction which seems to be useful. It allows you to invoke a Python callable by saying something like: PyEval_CallFunction(obj, "OOO", a, b, c); However, I can't find any official…
Channel72
  • 24,139
  • 32
  • 108
  • 180
5
votes
1 answer

Python interpreter takes ~12 seconds to start up, all of which is spent in `import pyexpat`

I am using a Homebrew-installed Python on my Mac (running OS X 10.13.1), and of late, I’ve noticed that the interpreter takes a frustratingly long time to start up. In setting out to try to solve this problem, I did a simple check with…
fish2000
  • 4,289
  • 2
  • 37
  • 76
5
votes
1 answer

Using Python3 C API to add to builtins

I'm looking to use the Python3 C API to add a builtin function. I'm doing this merely as an exercise to help me familiarize myself with the Python C API. The answer to this question does a pretty good job of explaining why one might not want to do…
Dean Shaff
  • 125
  • 2
  • 7
5
votes
1 answer

Can two threads use the same embedded python interpreter simultaneously?

The title has it, but here are some elaborations. Suppose the main thread spawns another thread, where some code is loaded into python interpreter and then another thread is called which executes some more code through the same python interface…
user3496846
  • 1,627
  • 3
  • 16
  • 28
5
votes
2 answers

Method ignored when subclassing a type defined in a C module

I am subclassing a type defined in a C module to alias some attributes and methods so that my script works in different contexts. How is it that to get this to work, I have to tweak the dictionary of my class manually ? If I don't add a reference…
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
5
votes
2 answers

using a C extension in python, without installing it as a module

I am writing C extensions for python. I am just experimenting for the time being and I have written a hello world extension that looks like this : #include static PyObject* helloworld(PyObject* self) { return…
ironstein
  • 416
  • 8
  • 23
5
votes
1 answer

How to iterate through all python objects from a c extension?

I'm working on a python memory profiler where I collect the size of python objects with the following method: sum(map(sys.getsizeof, gc.get_objects())) This is significantly the slowest part of the code - especially gc.get_objects - so I decided to…
asciimoo
  • 631
  • 5
  • 9