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

Python C API: WindowsError after creating some number of PyObjects

I've been having an issue getting the Python C API to not give me errors. Background: I've been using ctypes to run native code (C++) for a while, but until now I had never actually done anything specific with the Python C API. I had mostly just…
Aaron
  • 385
  • 4
  • 13
1
vote
1 answer

How can I initialize a NumPy array from a multidimensional buffer?

The documentation for the numpy.frombuffer function specifically says that the generated array will be one dimensional: Interpret a buffer as a 1-dimensional array. I'm not sure about the consequences of this quote. The documentation just tells me…
Spiros
  • 2,156
  • 2
  • 23
  • 42
1
vote
1 answer

Should a C extension fail in module init if a PyModule_Add* function fails?

I was just reviewing some code that created a C extension module for Python which didn't contain enough error-checking. It was easy enough in most cases but when it comes to the module-init function I wasn't sure. Just for the sake of discussion,…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
1 answer

How to nest c-api extensions in a Python module and use them in that module?

I want to create a Python 3 package called "mypack" that can be installed with pip. It has some c-extensions and some python code that calls those extensions. Here is my directory…
user5915738
  • 450
  • 5
  • 12
1
vote
1 answer

Python 3 C-API IO and File Execution

I am having some serious trouble getting a Python 2 based C++ engine to work in Python3. I know the whole IO stack has changed, but everything I seem to try just ends up in failure. Below is the pre-code (Python2) and post code (Python3). I am…
Kyle C
  • 1,627
  • 12
  • 25
1
vote
1 answer

How to enable the pyd to call functions in external C++ dll?

I'm trying to design an external DLL for my python program. Now I could use the C++ and Visual Studio 2010 to produce a file with a postfix of ".pyd". If the .pyd is not attached with other .dll files produced by C++, this python library could work…
1
vote
1 answer

binary using both Python C API version 2 and 3

In an open source project1 we have Python/Cython and C/C++ modules mixed with one C++ library using the Python C API. The API changed only a few function's names from 2 to 3. Assume the library is written without those functions. Will it link to…
rwst
  • 2,515
  • 2
  • 30
  • 36
1
vote
1 answer

Python C API: Call a Python function with argument(s) passed by reference

I'm writing a python module in C and I need to have it call a Python function with one of the arguments passed by "reference". The end result should be that what the Python function does to the argument gets saved into the original C variable. int …
Caleb Hearon
  • 256
  • 4
  • 14
1
vote
0 answers

how to modify the shape returned from shape_predictor in dlib python

I want to modify the shape returned from shape_predictor in dlib python, code as follows: sp = dlib.shape_predictor(predictor_path) dets = detector(img, 1) for k, d in enumerate(dets): shape = sp(img, d) for par in…
Rhapsody
  • 77
  • 1
  • 5
1
vote
1 answer

Python/C API - The result is not displayed

I would like to integrate C modules in the Python, so my choice fell on the interface Python.h. Everything compiled without errors and warnings, so I can not understand what the problem is. C side: #include ... #define…
errfrom
  • 243
  • 2
  • 8
1
vote
1 answer

Linking errors when using Tensorflow/Bazel to call Python from C++

I am writing a C++ application (based on Tensorflow Serving) in which I need to call Python and Numpy functions (from Python.h and numpy.h). This application is built with Bazel. So I include the header: #include…
JoSauderGH
  • 337
  • 4
  • 15
1
vote
1 answer

Python packages path issues

Using Python 2.7, OS: Ubuntu 16.04 64 bit. I am writing one sample application using Python C API. I have created one virtual environment and installed dependent package in virtualenv to run this application. Below is my code. #include…
Neel
  • 451
  • 1
  • 9
  • 23
1
vote
1 answer

Pointer ownership when PyCapsule_New fails

PyCapsule_New accepts a destructor function, which is called when the capsule is destroyed: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor) I am trying to use this mechanism to pass ownership of an object…
L Song
  • 121
  • 4
1
vote
1 answer

PyUnicode_FromFormat with (not-unicode) strings

I try to create a representation function for a class and I want it to be python-2.x and python-3.x compatible. However I noticed that normal strings when passed to PyUnicode_FromFormat as %U will segfault. The only viable workaround that I found…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
1 answer

What is the free list (that PyDict_ClearFreeList refers to)?

There is a C API function for Python dictionaries called PyDict_ClearFreeList. However the docstring is rather sparse: int PyDict_ClearFreeList() Clear the free list. Return the total number of freed items. New in version 3.3. This function has no…
MSeifert
  • 145,886
  • 38
  • 333
  • 352