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
4
votes
2 answers

How do you convert a C++ object to a PyObject?

I have a two c++ objects int queueIndex Timer timer_obj and here is the definition of the timer class class Timer { private: struct timeval tStart; /**< @brief Stores the system time when the timer is started */ struct timeval…
Tyranicangel
  • 189
  • 2
  • 16
4
votes
1 answer

Compile file .c with embedded Python/C functions

I'm starting the study of Python/C API and I make the first code to test some functions, I write this: file: test.c #include "Python.h" int main() { PyObject* none = Py_BuildValue(""); } I compile with command: gcc -I/usr/include/python2.7…
Figus
  • 681
  • 1
  • 7
  • 13
4
votes
1 answer

Implementing numeric methods always return NotImplemented

I'm writing a new extension type, but I have a problem setting the numeric operations(such as addition/subtraction/multiplication). I have managed to set the some in-place operations, while the normal operations are not called. For example, I have…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
3
votes
1 answer

Reading many values from numpy C API

I'm trying to read many values (in sequence) from a large numpy array using the C API. I'd like a more efficient way than seperately using boost::python::extract(...) on each value. Something like getting a pointer to the first value and then just…
Alex Flint
  • 6,040
  • 8
  • 41
  • 80
3
votes
2 answers

Evaluating Python Code From the CAPI and getting Output

I'm trying to emulate code.InteractiveInterpreter from the embedded Python C API. I'm using PyEval_Evalcode to evaluate the user input. I am trying to evaluate user input in the interpreter and return the output as a string (just like the…
emist
  • 137
  • 1
  • 2
  • 8
3
votes
1 answer

Transferring a Pointer From C++ To Python Compatible with Host and Device Memory

I have a Python function (named apply_filter), whose execution may involve either the CPU (using NumPy) and GPU (using CuPy). The function takes an input-buffer object, represting a pointer to data either in system memory or on the GPU's global…
Amit
  • 117
  • 7
3
votes
1 answer

Python C API doesn't load module

I'm trying to load a python module that contains a math and numpy import in C, using the C API. I can load and run the module but, if I import the math module it doesn't work. I'm using Arch Linux, Python 2.7.2 and gcc. Here the codes: #include…
Janil
  • 81
  • 1
  • 7
3
votes
1 answer

A memory leak in a simple Python C-extension

I have some code similar to the one below. That code leaks, and I don't know why. The thing that leaks is a simple creation of a Python class' instance inside a C code. The function I use to check the leak is create_n_times that's defined below and…
EZLearner
  • 1,614
  • 16
  • 25
3
votes
2 answers

Why do Python functions get garbage collected?

I have a C++ library which uses Python callbacks. The callback, i.e. PyObject*, is stored in an object of class UnaryFunction, and the constructor Py_INCREFs it. The destuctor Py_XDECREFs it. That's the problem. The interpreter segfaults on that…
Adam
  • 16,808
  • 7
  • 52
  • 98
3
votes
1 answer

Reversing axis in Numpy array using C-API

I am using the Python C-API to wrap some C++ code as a Python package. In the end, I have to reverse an axis in a numpy array, i.e. doing x = x[:, ::-1] Is there some way of doing this using the Numpy C-API? I know there are routines for transposing…
Andreas Mueller
  • 27,470
  • 8
  • 62
  • 74
3
votes
1 answer

python bytearray to C++ object

I'm wondering if I could get some help. For context, I'm using some C++ libraries to generate some large (think hundreds of Mb) objects that I want to send over a network from a server to a client. On the server, I've got the following: PyObject* …
IanQ
  • 1,831
  • 5
  • 20
  • 29
3
votes
0 answers

No objects of __all__ are defined in numpy.random subpackage, but no errors are thrown upon importing

Why all the functions listed in __all__ (of the init file) of numpy.random subpackage work even though they aren't defined there? I can imagine that they are written in C, but still when I do from numpy.random import * why doesn't Python throw an…
ankit
  • 3
  • 1
  • 11
3
votes
0 answers

.so-type plugin with embedded Python interpreter: How to link/load libpython?

I have got the following setup: ---------------------------- | C++ Application | ---------------------------- | dlopen()s | v ---------------------------- ------------------- |…
Stehsegler
  • 41
  • 3
3
votes
1 answer

Python gives error when importing simple C extension module

On windows I have built a very simple "hello world" C extension (the file hello.c from this site https://gist.github.com/physacco/2e1b52415f3a964ad2a542a99bebed8f). Using VS2015 I successfully obtain hello.dll. The problem is that I can't figure out…
cosas
  • 43
  • 5
3
votes
1 answer

Python C API: problems with calling python c method in c

In my module there is a method that takes python list and divides it by double static PyObject *st_div_r(PyObject *self, PyObject *args){ PyObject *pList; Py_ssize_t n; double x; int i; if (!PyArg_ParseTuple(args, "O!d", &PyList_Type,…
alehak
  • 47
  • 6