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

Generalized Universal Function in numpy

I'm trying make a generalized ufunc using numpy API. The inputs are one (n x m) matrix and a scalar, and outputs are two matrix ((n x p) and (p x m)). But I don't knowing how to do it. Someone could help me? In init function I'm using…
fabincarmo
  • 81
  • 9
6
votes
2 answers

How to clear a PyListObject?

I have a question that how to clear a list that's formed by PyList_Append()? Is there a document about Python/C extension API functions in detail?
sophistcxf
  • 111
  • 3
6
votes
1 answer

How do I get string representation of PyObject in Python3?

I am debugging Python source code (CPython 3.4). I'm receiving a PyObject*. I would like to printf it. For example (from Objects/floatobject.c of Python 3.4 source code): PyObject *o_ndigits = NULL; Py_ssize_t ndigits; x = PyFloat_AsDouble(v); if…
arjunaskykok
  • 946
  • 10
  • 17
6
votes
2 answers

Parsing User Defined Types Using PyArg_ParseTuple

How to parse userdefined types (or types from an existing non-standard library) using PyArg_ParseTuple?
dhanesh sr
  • 121
  • 6
6
votes
1 answer

Is there a NumPy C API function which will reset the layout flags?

I am manually modifying the shape and strides of NumPy arrays which may (or may not) invalidate the contiguity flags. Do I have to then manually check whether the strides match the values you would expect from the shape and NPY_C_CONTIGUOUS (or…
Alex Rubinsteyn
  • 420
  • 4
  • 8
6
votes
2 answers

Python C API, High reference count for new Object

I'm just trying to understand how to deal with the reference counts when using the Python C API. I want to call a Python function in C++, like this: PyObject* script; PyObject* scriptRun; PyObject* scriptResult; // import module script =…
user1774143
  • 192
  • 1
  • 7
6
votes
4 answers

Can I use C++ features while extending Python?

The Python manual says that you can create modules for Python in both C and C++. Can you take advantage of things like classes and templates when using C++? Wouldn't it create incompatibilities with the rest of the libraries and with the…
Javier
  • 4,552
  • 7
  • 36
  • 46
6
votes
1 answer

PyArg_ParseTuple default argument

If I have the following function and the optional argument myobj is not passed, does myobj remain NULL or is it set to Py_None? static PyObject * myfunc(PyObject * self, PyObject * args) { PyObject * myobj = NULL; if (!PyArg_ParseTuple(args,…
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
5
votes
2 answers

Import and use standard Python module from inside Python C extension

I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os or shutil. How is best to do this?
zaharpopov
  • 16,882
  • 23
  • 75
  • 93
5
votes
3 answers

Python-C api concurrency issue

We are developing a small c server application. The server application does some data processing and responds back to the client. To keep the data processing part configurable and flexible we decided to go for scripting and based on the availability…
Will
  • 482
  • 1
  • 8
  • 21
5
votes
1 answer

Does the Python 3 interpreter leak memory when embedded?

This bug report states that the Python interpreter, as of June 2007, will not clean up all allocated memory after calling Py_Finalize in a C/C++ application with an embedded Python interpreter. It was recommended to call Py_Finalize once at…
5
votes
1 answer

How to determine if a PyObject is a numpy PyArrayObject

I'm new to the Python C-API. Currently I get objects from the embedded Python module via PyObject* a = (PyObject*) PyObject_GetAttrString(pModule, "a"); std::cout << "a as long is " << PyLong_AsLong(a) << std::endl; I access numpy objects…
Fabian
  • 107
  • 1
  • 5
5
votes
1 answer

How do I find out why importing failed with PyImportModule?

I have this code in a C application that's embedding Python (2.7.1): { PyObject *user_dict; PyObject *user_func; PyObject *result; PyObject *header_tuple; PyObject *original_recipients; PyObject *working_recipients; if (!Py_IsInitialized()) { …
Tony Meyer
  • 10,079
  • 6
  • 41
  • 47
5
votes
1 answer

How do I use the correct dll files to enable 3rd party C libraries in a Cython C extension?

I have a C function that involves decompressing data using zstd. I am attempting to call that function using Cython. Using this page from the docs as a guide I can compile and run the code below with no problem. (I don't actually use the zstd lib…
CSStudent7782
  • 618
  • 1
  • 5
  • 21
5
votes
2 answers

Checking A PyObjects C Type

I am using Python 3.2 and C++. I need to extract what kind of C type is currently being stored in a PyObject. I have checked over the documentation and googled it as well and no one else seems to have needed to do this. So I have a PyObject and am…
DaneEC117
  • 227
  • 3
  • 10