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
1
vote
1 answer

Wrapping a piece of C++ code into Python

I have a piece of C++ code that generates the data. I want to expose the data to Python. But it is not as trivial as it sounds... Say this C++ compiles into binaries cppcode. Because of the complicated way of the framework setup, I can only run the…
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
1
vote
1 answer

Apparent memory leak when using PyTuple_SetItem

I'm building a nested set of tuples in a C++ Python extension. However, I'm having some problems with managing reference counts. The minimal code to re-create this memory leak: PyObject *outer = PyTuple_New(outer_size); for (size_t index = 0; index…
squidpickles
  • 1,737
  • 19
  • 27
1
vote
1 answer

Python Extension Returned Object Etiquette

I am writing a python extension to provide access to Solaris kstat data ( in the same spirit as the shipping perl library Sun::Solaris::Kstat ) and I have a question about conditionally returning a list or a single object. The python use case would…
Erik
  • 898
  • 8
  • 20
1
vote
1 answer

gcc error with Python C API code - "ISO C++ forbids casting between pointer-to-function and pointer-to-object"

The following code fragment does nothing, but illustrates the problem. It was extracted from some Boost Python code, which uses the Numpy C API. This was tested with the backport of a gcc 4.7 snapshot from Debian unstable to squeeze. #include…
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
1
vote
1 answer

Passing a SWIG proxy object as input to PIL.Image.frombuffer

I'm using the SWIG array_class macro defined in carrays.i to create an unsigned char buffer which can be sent to the c++-side of my project, which handles picture taking. This works fine – the buffer is filled width data after the camera has…
Paul
  • 257
  • 1
  • 3
  • 10
1
vote
2 answers

embedding python module error

So i have a c to python wrapper that takes input strings and pass them to a python function. the error im getting is that the python API is not recognizing my python file... PyObject *pName, *pModule, *pFunc; QString…
PeterG
  • 519
  • 2
  • 7
  • 15
1
vote
1 answer

Usage of python logging module in c-extension

I'm working on a python script and I'm using the python logging module for writing some information into a log file (see http://docs.python.org/howto/logging.html). Here is a small example (also from the mentioned page): import logging import…
firehead
  • 71
  • 7
1
vote
1 answer

PyArray_BOOL declaration & working with Numpy array of bools in C extension

I have a NumPy array of bools in my code that I wish to write a C extension to. When I try to get a contiguous array in order to use in my C routine, I use: arr_mask = (PyArrayObject *) PyArray_ContiguousFromObject(mask, PyArray_BOOL, 2,…
mehmet.ali.anil
  • 505
  • 6
  • 17
1
vote
0 answers

quickly check that Python3 integer fits into C long or int

In Python2's C API there was long PyInt_AS_LONG() function which allowed a very quick creation of a C long from a Python2 int. Is there a quick way to check, using Python3 C API, that an int (which is actually an arbitrary precision integer) fits…
Dima Pasechnik
  • 336
  • 4
  • 16
1
vote
1 answer

Python unittest C extension call to exit()

TLDR I can't get unittest to run a test where I am trying to check that my Python C extensions calls exit(1) from stdlib.h. The setup I have a Python unit test and C extension which looks like the following: The Python test import unittest import…
oliversm
  • 1,771
  • 4
  • 22
  • 44
1
vote
0 answers

Python C API importing a module with PyRun_String

I am trying to use PyRun_String(string, Py_eval_input, dict1, dict2); to import a module, but I am getting a syntax error. Is there a way to import built-in or third-party modules with PyRun_String?
1
vote
0 answers

Valgrind Memory error detection on just call to Py_Initialize() and nothing else

I am adding my python code into c++ for making an wrapper to c api that can be later used inside cpp as library and everything is working fine but for except one thing that is memory errors. See bellow code. #include "Python.h" #include…
1
vote
0 answers

Using Python C API and FastCGI, how can I get the Python C API to send output to NGINX instead of stdout?

I am using the Python C library and FastCGI to run code in a file and return the output to NGINX. The problem is PyRun_SimpleString outputs to stdout, which is fine for a CLI, but is ignored by FastCGI. The code I tried was this: #include…
Sidney
  • 13
  • 4
1
vote
0 answers

Module in a C-embedded Python script cannot import some other packages

My apologies for the confusing title. I've run into a conceptual problem in a codebase which I'm now trying to debug from an MRE. The code to run a function defined in a Python script is #define PY_SSIZE_T_CLEAN #include int main(int…
user169291
  • 21
  • 2
1
vote
1 answer

Cpython Extension forcing Memory Leak

I am trying to leak memory from my C-extension forcefully. The code for the below is Pyobect* myfunc(PyObject* self, PyObject* args) { static int i = 213123; PyObject* temp = PyLong_FromLong(i); // This is supposed to leak. i = i + 1 ; …
daniel
  • 35
  • 8