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
0
votes
0 answers

How to eventually cleanup after PyImport_ImportModule?

The Python C API has the function PyImport_ImportModule that returns a new reference to the module. As a new reference, it must be disposed of at some point. Most objects created during module initialization are added to the module with…
Dominick Pastore
  • 4,177
  • 2
  • 17
  • 29
0
votes
0 answers

Python C API : Convert PyObject pointer to C data type

I need to convert a PyObject* to and int variable in C. I understand we can parse arguments with PyArg_ParseTuple and that we can build Python object using Py_BuildValue but what is the 'inverse' of Py_BuildValue? For instance say I have a Python…
MFranc
  • 366
  • 1
  • 2
  • 14
0
votes
1 answer

Extending python with C: trying to swap list elements

I am to build a C module for Python 3.7, that swaps two list elements. Here is my code, where indexes of two elements and list are read: static PyObject *st_change(PyObject *self, PyObject *args){ PyObject *pList; PyObject *tmp1; PyObject…
alehak
  • 47
  • 6
0
votes
0 answers

Getting undefined module error for third party module

I am trying to make an interface between c and python. And by suing interface i want to call python user defined methods from c for some specific operations. I am using Cython for interface. Given a python module, i am compiling using cython and…
Biswajit
  • 59
  • 6
0
votes
0 answers

Python C API: import error when using C API

I'm working on integrating Python (via the C API) with an existing C++ application. Things have been going well until I ran into a strange case where I get an import error when running a code snippet through the C API. When I run the same snippet in…
sbell
  • 457
  • 6
  • 13
0
votes
1 answer

Proper way to call a different method from the same C-extension module?

I'm converting a pure-Python module to a C-extension to familiarize myself with the C API. The Python implementation is as follows: _CRC_TABLE_ = [0] * 256 def initialize_crc_table(): if _CRC_TABLE_[1] != 0: # Safeguard against…
Kamikaze Rusher
  • 271
  • 2
  • 10
0
votes
0 answers

How can stdout of a C extension to a python 3 app with no console be redirected to a file?

This is on windows with python 3.6. The goal is to have all stdout from python and c extensions logged to the same file. Because this is a windows application with no console sys.stdout.fileno() is -2 so the normal method of file descriptor…
ayeayeron
  • 1
  • 1
0
votes
1 answer

Python C API/ctypes function segfaults when parsing arguments

I've written a c++ method that enumerates the number of subgraphs of a specified size in a given adjacency matrix. I'm trying to make this method callable in python. The basic method signature takes a list and a number and (should) return a list of…
0
votes
0 answers

Python C API Unicode arguments to std::wstring

following https://docs.python.org/3/extending/extending.html#keyword-parameters-for-extension-functions I declared the following function: PyObject* myFunction(PyObject *self, PyObject *args, PyObject *keywds) { const wchar_t *query; …
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
0
votes
1 answer

Deriving from arbitrary Python class with C API and `tp_basicsize` of `object`?

I am trying to define a function, that would create a Python class using C API, that derives from arbitrary Python type base, and has an extra field void* my_ptr in its raw C-like object layout. I want it to reuse Python's __dict__ functionality. I…
LOST
  • 2,956
  • 3
  • 25
  • 40
0
votes
1 answer

Problem using opencv in a c-extension for python?

I'm trying to write a simple python c-extension which includes some opencv code. Here is my c++ code: #include "Python.h" #include "numpy/arrayobject.h" #include #include /* .... C matrix…
whoAmI
  • 358
  • 4
  • 16
0
votes
1 answer

How to set the name argument while using ctypes.pythonapi.PyCapsule_New

How do I pass in the name of a capsule into the second argument of PyCapsule_New via ctypes? I've tried the following, but it seems that something is off: capsule = ctypes.pythonapi.PyCapsule_New(b'data', b'abcdef.ghijkl', None) capsule =…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
0
votes
1 answer

PyRun_String returns a NoneType object

I'm writing a base64 function in c++.I want to use python to do the encode and returns the result to my c++ program.Here is my code: string EncodeBase64(string str) { Py_Initialize(); PyObject* ret1 = NULL; PyObject* ret2 = NULL; …
0
votes
0 answers

Calling Python from C: ./a.out: /lib/libgcc_s.so.1: version `GCC_7.0.0' not found (required by ./a.out)

I'm able to compile and link my program successfully, but running the a.out file results in: ./a.out: /lib/libgcc_s.so.1: version `GCC_7.0.0' not found (required by ./a.out) The output of ls /lib | grep libgcc…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
0
votes
1 answer

CPython's PyLong_FromLong(0L) never fails?

This piece of code is from the Python C-API reference: item = PyLong_FromLong(0L); if (item == NULL) goto error; Assuming the interpreter is CPython, the memory for the Python 0 object is already allocated, so what could go wrong there? Reading…
fonini
  • 2,989
  • 3
  • 21
  • 39