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

Returning list from python method called from C++

I am trying to call a Python method from C++ code and the method I am calling takes a file name as an argument returns a list. My sample code currently converts PyObject* result of the method call to a double but I want to convert this to a…
Paul
  • 94
  • 1
  • 9
1
vote
1 answer

PYCFunctionWithKeywords being called incorrectly from python

I am trying to write a C-extension module for python3, say foo and I am trying to define method that can take keyword arguments. static PyObject* fooImpl(PyObject*, PyObject*, PyObject*); static PyObject* fooImpl2(PyObject, PyObject*); static…
1
vote
1 answer

Numpy C-Api array_equal

I've tried to find function comparing two PyArrayObject - something like numpy array_equal But I haven't found anything. Do you know function like this? If not - How to import this numpy array_equal to my C code?
Darkowic
  • 661
  • 8
  • 17
1
vote
1 answer

How to get a PyObject corresponding to a builtin?

If I want to get a PyObject for something like, say, sys.exc_info, I can write PyObject *sys_module = PyImport_Import("sys"); PyObject *sys_exc_info = PyObject_GetAttr(sys_module, "exc_info"); What if I want to get a PyObject corresponding to a…
Zachary Turner
  • 738
  • 4
  • 24
1
vote
1 answer

Loading binary data from a file into a buffer in Python

I have a set of 640x480 images. I’m converting these images to a binary file format via Matlab… Now, I need to load each of the binary files into a buffer in Python, and then read data from that buffer. Can anyone help me out with how I might do…
user3397145
  • 787
  • 1
  • 5
  • 16
1
vote
0 answers

Python/C API: Statically-Linked Extensions?

I've been writing a Python extension use the Python/C API to read data out of a .ROOT file and store it in a list of custom objects. The extension itself works just fine, however when I tried to use it on a different machine I ran into some…
1
vote
2 answers

Where is TimeZoneType?

In the C API structure in datetime.h, I see the following type objects: PyTypeObject *DateType; PyTypeObject *DateTimeType; PyTypeObject *TimeType; PyTypeObject *DeltaType; PyTypeObject *TZInfoType; But there is no: TimeZoneType I need to…
MMM
  • 910
  • 1
  • 9
  • 25
1
vote
1 answer

Passing a List and numpy.matrix to a python function from a C++ application

I have a bunch of functions written in python (for rapid prototyping). My main project is in C++ and I wanna call these functions from my C++ program.These functions use some specialized python modules like numpy, pyside etc. To start with, I have…
Jim
  • 317
  • 1
  • 13
1
vote
1 answer

Python C API: Passing two functions of many parameters with special types as module

I am attempting to create a Python module using C. This module has two callable functions - one with mpi support and one without. int run_mymodule(char *file1, char *file2, char *file3, PyObject *tmpp) {
kilojoules
  • 9,768
  • 18
  • 77
  • 149
1
vote
1 answer

Adding new python type : TypeError: can't set attributes of built-in/extension type

Below python-c code which compiles properly #include #include struct rangerr { long min; long max; }; //Python type to represent rangerr struct py_rangerr { PyObject_HEAD struct rangerr…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
1
vote
1 answer

Py_InitModule with multiple functions - invalid conversion from int to PyCFunction

I am transitioning to removing the boost-python dependencies in my code, and I have made to to the "final step" of this transition (I removed all other boost dependencies, and when I comment out the boost code below I get ImportError: dynamic module…
kilojoules
  • 9,768
  • 18
  • 77
  • 149
1
vote
2 answers

Return an array from python to C++

I'm writing a c++ code to call a python function and the returned array from the python function will be store in an array in c++. I am able to call the python function in c++ but I am able to return only one value from Python to C++ and what I want…
Sajal Jain
  • 263
  • 1
  • 3
  • 11
1
vote
2 answers

ValueError(u"Invalid mode, expected 'c' or 'fortran', got f\x00o\x00r\x00t",)

I am trying to import sklearn.neighbors in Python, and from there import KNeighborsClassifier. When I try to execute it in Python, I get a ValueError: ValueError(u"Invalid mode, expected 'c' or 'fortran', got f\x00o\x00r\x00t",) in
Sajal Jain
  • 263
  • 1
  • 3
  • 11
1
vote
2 answers

How to achieve polymorphism in Python C API?

I'm writing functools.partial object alternative, that accumulates arguments until their number become sufficient to make a call. I use C API and I have tp_call implementation which when its called, returns modified version of self or PyObject*. At…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
1
vote
1 answer

What is the point of defining new class as a struct in Python C API

The docs for Python C API describes the pattern of defining a new type: typedef struct { PyObject_HEAD PyObject *first; /* first name */ PyObject *last; /* last name */ int number; } Noddy; ... Then methods such as init could be…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138