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

Python C extension - Receiving a dict as argument

I'm writing a C extension and I'm quite lost on how to receive a dict as an argument. As the docs don't have any specifics on how to achieve this I tried to parse the argument as a Python Object and then manipulate it as a dict: PyTypeObject…
Raphael
  • 7,972
  • 14
  • 62
  • 83
5
votes
1 answer

How to use Py_AddPendingCall

I have an embedded Python program which runs in a thread in C. When the Python interpreter switches thread context (yielding control to another thread), I'd like to be notified so I can perform certain necessary operations. It seems that…
Channel72
  • 24,139
  • 32
  • 108
  • 180
5
votes
1 answer

Why is this 'from-import' failing with PyRun_SimpleString?

I am working on a simple(?) embedded Python project. I have a custom package that has been installed into site-packages with 'setup.py install', e.g.: in C:\Python27\Lib\site-packages\: mypackage\ __init__.py subpackage\ …
djangodude
  • 5,362
  • 3
  • 27
  • 39
4
votes
2 answers

How to get a char* from a PyObject which points to a string

How can I get a char* from a PyObject which points to a string. For example, this is the python script, Test.Connect("272.22.20.65", 1234) and this is the C++ code, static PyObject* Connect(PyObject *self, PyObject *args) { PyObject* pIP; …
Morpheus
  • 1,722
  • 5
  • 27
  • 38
4
votes
1 answer

Printing a variable in an embedded Python interpreter

I have written a small C program that embeds Python. I'm setting it up correctly using Py_Initialize() and Py_Finalize(), and am able to run scripts either using PyRun_SimpleString or PyRun_SimpleFile. However, I don't know how mimic the behavior of…
FizzBuzz
  • 764
  • 6
  • 16
4
votes
4 answers

Passing a C struct to a Python function

I need a simple way to pass a C struct to a Python function. I have embedded Python into a game server, and I intend to write game logic in Python. I've scoured Google and mailing lists and found nothing useful. I have a complex structure in C (with…
4
votes
1 answer

Python C API unicode arguments

I have a simple python script import _tph str = u'Привет, мир!' # Some unicode string with a russian characters _tph.strip_tags(str) and C library, which is compiled into _tph.so. This is a strip_tags function from it: PyObject…
SvartalF
  • 163
  • 2
  • 8
4
votes
0 answers

Bad access when calling PyObject_CallObject for the second time

I am really new to this. In fact this is my first try at embedding a python file function call in C(C++) code. I am calling the following function which loads a file and a function in the file, which lies in some directory other that the one from…
Swati Kala
  • 51
  • 1
4
votes
1 answer

Linking to numpy's linear algebra libraries in C extension

I am writing a C extension and would like to take advantage of the linear algebra routines that the numpy libraries are linked to. Is there a cross platform method of getting the path for these? I've looked through most of numpy.distutils.* and…
vlovero
  • 190
  • 1
  • 11
4
votes
1 answer

How to call a python function from C++ with pybind11?

Please consider the following C++ pybind11 program: #include namespace py = pybind11; int main() { py::scoped_interpreter guard{}; py::dict locals; py::exec(R"( import sys def f(): …
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
4
votes
3 answers

How can I get a pointer to the data inside a generic PyObject*?

I'm trying to get the memory address of data held within a PyObject* (from Python.h 3.8.2 specifically) so I can do a memcpy to a buffer. I've only been able to figure out how to copy the data out of the object but nothing on just getting the…
Tyler Weiss
  • 139
  • 1
  • 15
4
votes
0 answers

Dymola Segfault in external C function with Python API when using time and numpy

We execute Python code from Dymola via the C/Python API. Therefore we used that tutorial. The tutorial works as shown, but when we combine Dymolas built-in time variable and Pythons Numpy library, we get a segfault. Function in Dymola to call…
4
votes
4 answers

How to get current function name in Python C-API?

I implemented a bunch of functions and they are dispatched from the same C function called by the Python interpreter: PyObject * CmdDispatch(PyObject *self, PyObject *args, PyObject *kwargs) Unexpectedly, self is NULL, and I need to get the…
Juan
  • 3,667
  • 3
  • 28
  • 32
4
votes
1 answer

Is Python C module extension version incompatible?

I compile a python c module extension in Python 3.6. It works well in Python 3.6 environment but does not work in Python 3.7 and 3.8 environments, it gets error cannot import name 'cygrpc'. I wondering is this the expected behavior? If yes, what is…
Lei Wang
  • 217
  • 1
  • 4
4
votes
1 answer

PyObject vs PyTypeObject

I am trying to build a Python-C interface, but when I looked at the source code, I am confused by PyObject and PyTypeObject. Can someone please explain the difference between them? It would be nice if someone could provide an example.
yin
  • 97
  • 1
  • 4