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

Numpy C-Api example gives a SegFault

I'm trying to understand how the Python C- Api works, and I want to exchange numpy arrays between Python and a C Extension. So, I started this tutorial: http://dsnra.jpl.nasa.gov/software/Python/numpydoc/numpy-13.html Tried to do the first example…
mehmet.ali.anil
  • 505
  • 6
  • 17
9
votes
1 answer

c++0x std::shared_ptr vs. boost::shared_ptr

I have a c++ code which heavily uses shared_ptr and STL. A common header says #include using boost::shared_ptr; // for shared_ptr using namespace std; // for STL I wanted to switch to c++0x now to make use of the…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
9
votes
1 answer

Returning objects to Python from C

I've read the documentation for the Python C-API, and even written a few extension modules. However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function. The limited examples in the Python docs…
Channel72
  • 24,139
  • 32
  • 108
  • 180
9
votes
1 answer

Use the Eigen library with cppyy

I've been successfully using cppyy for automatic python bindings for a C++ project I'm working on. I recently included the Eigen library, but I'm having trouble using this together with cppyy. Does anyone have any experience doing this, or know how…
Bendik
  • 1,097
  • 1
  • 8
  • 27
9
votes
1 answer

Bind to pgcrypto from python

I'd like to call some pgcrypto functions from python. Namely px_crypt. I can't seem to figure out the right object files to link it seems. Here's my code: #include #include "postgres.h" #include "pgcrypto/px-crypt.h" static…
Oin
  • 6,951
  • 2
  • 31
  • 55
9
votes
3 answers

Returning a tuple of multipe objects in Python C API

I am writing a native function that will return multiple Python objects PyObject *V = PyList_New(0); PyObject *E = PyList_New(0); PyObject *F = PyList_New(0); return Py_BuildValue("ooo", V, E, F); This compiles fine, however, when I call it from a…
D R
  • 21,936
  • 38
  • 112
  • 149
9
votes
4 answers

Tracing code execution in embedded Python interpreter

I'd like to create an application with embedded python interpreter and basic debugging capabilities. Now I'm searching the API for functions which I could use to run code step-by-step and get the number of the current line of code which is being (or…
Luke
  • 1,369
  • 1
  • 13
  • 37
9
votes
1 answer

Static Variables in Python C API

How would one expose "static" variables like this class MyClass: X = 1 Y = 2 via the C API? The only variable on the PyTypeObject that looks like it would work is tp_members, but I see no flag in the PyMemberDef to indicate that the member…
Toji
  • 33,927
  • 22
  • 105
  • 115
9
votes
2 answers

Pass array of structs from Python to C

[Update: Problem solved! See bottom of the post] I need to allow python developers to pass an array of packed data (in this case vertices) into my API, which is a series of C++ interfaces exposed manually through the Python C API. My initial…
Toji
  • 33,927
  • 22
  • 105
  • 115
9
votes
1 answer

How to create lambda's from Python/C

We're working on some Python/C-API code, and we've encountered a method that would like to be passed a callback. The method will ship periodic updates to the callback as a form a feedback. As it turns out, we're not that interested in periodic…
user590028
  • 11,364
  • 3
  • 40
  • 57
9
votes
1 answer

Definition of PyBufferProcs in Python 2.7 when class implements PEP 3118

I am in the process of extending the classes in our library (which supports Python 2.7) to support PEP 3118, which has been back-ported to 2.7. From the documentation, I need to initialize the tp_as_buffer field to point to a PyBufferProcs. From…
James Kanze
  • 150,581
  • 18
  • 184
  • 329
9
votes
2 answers

PyObject_CallMethod with keyword arguments

I'm trying to embed a Python (2.7) library in my C application and I'm using the Python/C API to call Python code from C. I need to call a Python method that takes keyword arguments. Semantically, I'm trying to achieve the equivalent of the…
Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
9
votes
2 answers

Python: GIL context - switching

So, I generally have a pretty good understanding of how the Global Interpreter Lock (GIL) in Python works. Essentially, while the interpreter is running, one thread holds the GIL for N ticks (where N can be set using sys.setcheckinterval), at which…
Channel72
  • 24,139
  • 32
  • 108
  • 180
9
votes
6 answers

Python interpreter as a c++ class

I am working on embedding python in to c++. In some peculiar case I require two separate instances of the interpreter in same thread. Can I wrap Python interpreter in to a c++ class and get services from two or more class instances?
Amol Gawai
  • 3,220
  • 4
  • 23
  • 25
9
votes
1 answer

Create instance of a python class , declared in python, with C API

I want to create an instance of a Python class defined in the __main__ scope with the C API. For example, the class is called MyClass and is defined as follows: class MyClass: def __init__(self): pass The class type lives under __main__…
Paul
  • 2,474
  • 7
  • 33
  • 48