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

Python3 CTypes error loading dylib on Mac 10.14 (Mojave)

Background I have read countless GitHub project threads and everything I can find on StackOverflow about this problem, though so far no luck. I have a Mac 10.14 box running with the stock CommandLineTools and/or Xcode. I'm trying to "port" a Python…
mds
  • 129
  • 4
0
votes
0 answers

Why the PyObject is NULL after creation?

I have declared this function: PyObject * A::func(const void *data) const { const time_t *time = reinterpret_cast(data); std::tm *now = std::gmtime(time); PyObject *date_py =…
0
votes
0 answers

Python catching a wrapped C function error

When calling a wrapped c function as such: f = io.BytesIO() with stdout_redirector(f): r = _wrapped_c_function(in_file, out_file) the return code is returned in r, where return codes are given based on the errors raise inside the C function,…
Sam Palmer
  • 1,675
  • 1
  • 25
  • 45
0
votes
0 answers

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 6208: invalid start byte (Python C-Extension)

I am working with this library, which is a fork of pjsua with minor modifications from here, to work with Python 3. I have almost got everything working in the call and audio process, but I am running into an error related to the Python C-Extension…
Nicolai Prebensen
  • 301
  • 1
  • 4
  • 10
0
votes
0 answers

Call C++ code from Python, which in turn calls Python again (i.e. Python->C++->Python), is this possible?

I have a multithreaded C++ library that currently already has the ability of calling Python scripts from the inside via the Python C API. However, I would like to also add an outside Python wrapper to this C++ library to be able to use it as a…
Doktor Schrott
  • 616
  • 1
  • 7
  • 14
0
votes
1 answer

Embedding Python in GRPC Server

I am exploring GRPC (C++). Following their examples I am trying to create a server which accepts an image from the client returns the text in the image. I have a python code which accepts an image and a json file describing the bounding box of the…
Raki
  • 329
  • 3
  • 18
0
votes
0 answers

Use Logitech C library in python - define struct and callback

I want wrap/use Logitech C/C++ libraries in python code: http://gaming.logitech.com/sdk/LCDSDK_8.57.148.zip https://www.logitechg.com/sdk/LED_SDK_9.00.zip http://gaming.logitech.com/sdk/GkeySDK_8.57.148.zip With first two (LCD and LED) is…
emcek
  • 459
  • 1
  • 6
  • 17
0
votes
0 answers

ModuleNotFoundError python wrapper with c++

I am trying to reproduce the Boost.Python tutorial on how to do a wrap a C/C++ file for python. This is the cpp file which builded successfuly. #include "boost/python.hpp" class SaySomething { public: void set(std::string msg) { …
Hawoona
  • 31
  • 7
0
votes
1 answer

Python C API: Access Violation when trying example module with MSVC

I'm making a Python module in C++. Until now I used MingW to compile the module, which worked fine. But I want to switch to MSVC because an other library that I use is easier to use with MSVC. But, I cannot get it to work. Compiling and linking…
user42723
  • 467
  • 3
  • 8
0
votes
1 answer

Do the values of global variables in a C extension to Python persist on function calls?

Basically, say I have some global variable foo in my C extension, set to an initial value of 3 like so: int foo = 3; And say the value of foo is changed to 4 within function call foobar: int foobar() { foo = 4; return 0; } If I make…
0
votes
1 answer

Python C API crashes on 'import numpy' when initilizing multiple times

While working with the Python C API, I found that the python interpreter crashes when initializing it a second time and executing import numpy after each initilization. Any other command (e.g. import time) will do just fine. #include int…
0
votes
1 answer

CPython 'overloaded' functions

I am trying to overload a python extension function that would take either a object or a string. typedef struct { PyObject_HEAD } CustomObject; PyObject* customFunction(CustomObject* self, PyObject* args); PyMethodDef methods[] = { …
A student
  • 170
  • 11
0
votes
1 answer

Python C API - How to inherit from your own python class?

The newtypes tutorial shows you how to inherit from a base python class. Can you inherit from your own python class? Something like this? PyObject *mod = PyImport_AddModule("foomod"); PyObject *o = PyObject_GetAttrString(mod, "BaseClass"); …
Craftables
  • 236
  • 1
  • 8
0
votes
1 answer

f2py linking quadmath libraries? Use ctypes for fortran wrapper instead?

Update 11/23/2019: This started out as a question about why I could not get f2py to work for a simple fortran wrapper. My "answer" (below) is to use ctypes instead. Original post: I have spent the last three days trying to use f2py to interface…
L. Young
  • 113
  • 9
0
votes
1 answer

How to call release on a memoryview in Python C API

I have an existing PyMemoryViewObject that I want to "release" to invalidate the memoryview object. I am able to call the release function through the PyObject_CallMethod API: if (PyMemoryView_Check(obj)) { PyObject_CallMethod(obj, "release",…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49