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

(Numpy C API) Iterating over a single array: NpyIter vs for loop (with PyArray_DATA)

I am writing some C extension code for a python module. The function I want to write is (in python) output = 1./(1. + input) where input is a numpy array of any shape. Originally I was using NpyIter_MultiNew: static PyObject * helper_calc1(PyObject…
Haydon
  • 588
  • 1
  • 4
  • 13
3
votes
0 answers

Composition of native / C code callbacks in Python

I make heavy use of numerical analysis libraries in Python such as Scipy's optimization and integration routines, and many of these routines take a callback which evaluates some sort of objective function as their main argument. For performance…
JaredL
  • 1,372
  • 2
  • 11
  • 27
3
votes
1 answer

Python C API: Problems trying to build a dictionary

I'm trying to build a Python dictionary using the C API but it seems it's not possible (Py_BuildValue returns a NULL object) to use a PyObject* as value. I have a situation like the following: #include ... PyObject *myList =…
ggagliano
  • 1,004
  • 1
  • 11
  • 27
3
votes
1 answer

Is PyBuffer_Release required after Py_BuildValue("y#", ...)?

If it makes a difference, I am interested in an answer regarding Python 3. The docs state (here and here) that PyBuffer_Release() should be called after PyArg_Parse*() with s*, y*. Nothing of the sort is written about Py_BuildValue(). Is it an…
Zoltan K.
  • 1,036
  • 9
  • 19
3
votes
0 answers

SWIG: passing bytes between Python and C

I have searched online for a while, but what is the correct way to pass bytes to and fro between Python and C? I have leveraged the example from SWIG documentation: C Code: char* foo(char *buff, int size) { return buff; } Wrapper…
nikk
  • 2,627
  • 5
  • 30
  • 51
3
votes
1 answer

Checking if an object is an int or int-like in Python C API

Is there a an operation similar to PyInt_Check/PyLong_Check that takes into account whether or not the type has an __int__ method? The closest workaround I have been able to find so far is int check_int(PyObject *obj) { long lng; int over; …
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3
votes
1 answer

Freeing a PyTuple object

What is the proper way to free a PyTuple object using the Python C-API? I know that tuples are somewhat special when it comes to the reference counting semantics, since PyTuple_SetItem "steals" the reference to the inserted element. I also know…
Channel72
  • 24,139
  • 32
  • 108
  • 180
3
votes
1 answer

Enable auto-printing of evaluated statements in Python?

When running a Python REPL, top-level statements sent to the console get auto-printed. For example, with: >>> x = 1 >>> x 1 >>> | The second line evaluating x caused the value to be 'auto-printed' However, we don't get the same behavior when x is…
Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
3
votes
1 answer

How to include (script-built) libraries with package installation?

I am making a Python package that has a C++-extension module and someone else's shared library that it requires. I want everything installable via pip. My current setup.py file works when I use pip install -e . but when I don't use develop mode…
user5915738
  • 450
  • 5
  • 12
3
votes
0 answers

Calling scipy from C extension

I would like to load one of the scipy modules (scipy.linalg) inside a function which is exported from a python extension written in C++, so that I can then call a function from it. It would be best if the user to does not have to load the module in…
JeremyR
  • 401
  • 4
  • 8
3
votes
2 answers

Python 2.6 write string in C-API using the system encoding

I have a project in Python 2.6 and I'd like to write a utf-8 message to stdout using the system encoding. However it appears that such a function does not exist until Python 3.2: PySys_FormatStdout http://docs.python.org/dev/c-api/sys.html Is there…
Juan
  • 3,667
  • 3
  • 28
  • 32
3
votes
1 answer

How to redirect python interpreter output and catch it in a string in C++ program?

i am using python C++ API to run python commands from C++ program. I want to catch all the python output to a string, I've managed by the following redirection, to catch pythons stdout and stderr output: #python script ,…
alexpov
  • 1,158
  • 2
  • 16
  • 29
3
votes
1 answer

Python C API: Assigning PyObjects to a dictionary causes memory leak

I am writing a C++ wrapper for Python using the Python C API. In my case I have to make bigger amounts of byte oriented data accessible for the Python script. For this purpose I use the PyByteArray_FromStringAndSize method to produce a Python…
Zwackelmann
  • 557
  • 1
  • 5
  • 19
3
votes
1 answer

Is it possible to get the PyObject reference from the name of an existing variable?

Is there a way to get object reference of an existing object, in an embedded Python code? In other words: If an object called 'obj' already exist (created by a script), and I need to "convert" it to a PyObject* reference, is there a function like:…
FedFranz
  • 529
  • 1
  • 5
  • 15
3
votes
1 answer

Why doesn't the NumPy-C api warn me about failed allocations?

I've been writing a Python extension that writes into a NumPy array from C. During testing, I noticed that certain very large arrays would generate a segfault when I tried to access some of their elements. Specifically, the last line of the…
Max
  • 301
  • 1
  • 9