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

Python C API performance gains?

Is there any data that visualizes just how much performance can be gained by using the Python C API when writing functions directly in C to be used as python modules? Besides the obvious fact that "C is faster"; is there any data that compares…
user1529891
2
votes
1 answer

None value PyObject to NULL in PyArg_ParseTupleAndKeywords()

I am passing an extension type object to a Python function, which needs to pass a variable in this type to a C function. My extension type looks like this: typedef struct { PyObject_HEAD rec_rset_t rst; //need to pass this to C function }…
Edaro
  • 43
  • 1
  • 6
2
votes
1 answer

Embedding Python 3.3: How do I access _PyParser_Grammar?

I am attempting to emulate the Python/C API's PyRun_InteractiveLoop() function, but from a different input system used by my employer. The Python FAQ (http://docs.python.org/3/faq/extending.html#how-do-i-tell-incomplete-input-from-invalid-input) has…
J.T. Davies
  • 401
  • 3
  • 9
2
votes
0 answers

How can I call python code from the C++ extension (a .pyd file)

Suppose I have a python module compiled from C++ called Foo (Foo.pyd). Can I call python from the .pyd module? Is it going to use the same interpreter that is running Foo.pyd, or it runs another interpreter? Assuming I do not use boost python or…
szli
  • 36,893
  • 11
  • 32
  • 40
2
votes
1 answer

Loop Over Python Dictionary (using C API) Twice?

I'm currently writing a Python module in C. In it, I have a dictionary that I iterate over like so: PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(index, &pos, &key, &value)) /* do interesting things here */ Later in the same…
Matt Patenaude
  • 4,497
  • 1
  • 22
  • 21
2
votes
0 answers

Parsing python arguments in c++ extension

Is there a function similar to PyArg_ParseTuple and PyArg_ParseTupleAndKeywords, without using a variable argument list? I have a c++ extension that takes a long list of input arguments. Parsing them in the format of PyArg_ParseTuple and…
2
votes
1 answer

No module named ctypes using Python/C API

I have PythonCode.py: import os, ctypes print "ctypes are imported" And CCode.c: ... PySys_SetPath(path_to_PythonCode); PyObject *pModule = PyImport_ImportModule("PythonCode"); if (!pModule) { PyErr_Print(); return; } ... PyErr_Print()…
AlexP
  • 1,416
  • 1
  • 19
  • 26
2
votes
1 answer

Python C extension: return PyFloat_FromDouble(double) segfault

I recently converted some slow python code into a C extension. It works beautifully, except that it generates a segfault at the 162nd call into it, right at the return statement. Here's how it works. Once, prior to all of the calls to the function I…
2
votes
1 answer

'PyCObject_Import("cairo", "CAPI")' C++ call yields segfault, 'import cairo' on python works

I am running OSX Lion and trying to import the python module for goocanvas, using python2.7. I managed to successfully compile pygoocanvas-0.14.1, but when I try to import goocanvas through the python2.7 console, I get a segfault. After some…
FernandoH
  • 855
  • 1
  • 9
  • 17
2
votes
1 answer

Running Python from Visual C++ code (Embedded Python)

I have this Visual C++ code, plus Python embedded in it, when I try to run external Python code using the below code, I get the error in the Debug mode: Unhandled exception at 0x77cf15de in CMLAir.exe (my code): access violation writing location…
2
votes
1 answer

Accessing Python Objects in a Core Dump

Is there anyway to discover the python value of a PyObject* from a corefile in gdb
Ed L
  • 1,947
  • 2
  • 17
  • 30
2
votes
2 answers

Python / C-Api: Add class to module

Im currently trying to embed the python interpreter into my application. Because my application uses the Poco API for logging, I want to make it accessable through the logging module in python too. The most easiest way for me to do this, is to…
1
vote
1 answer

Passing a string from C to Python to multiprocessing without making an extra copy

I have a C application that embeds the Python 2.7 interpreter. At some point in my program, a potentially large string (char*) is generated and needs to be processed by some Python code. I use PyObject_CallFunction to call the Python function and…
flashk
  • 2,451
  • 2
  • 20
  • 31
1
vote
1 answer

Python C module function argument reference counting

When I have a PyObject * obtained from PyArg_ParseTuple, do I need to make sure to Py_DECREF it before I return from the function? Example: static PyObject * modulefunc(PyObject * self, PyObject * args) { PyObject * obj; if…
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
1
vote
1 answer

Using higher-level Python constructs from C

Are there any best practices for the use of higher-level Python constructs such as threading.Condition, and collections.deque from modules written in C? In particular: Avoiding dict lookup costs, for methods and members Accessing parts of these…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526