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
2
votes
1 answer

'PyThreadState_SetAsyncExc' causes 'SystemError: exception Exception() not a BaseException subclass'

Im writing Python extension module with threads and hookers. I need to throw exception from one of my threads to main Python thread. For this Im using int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc). In the documentation sayd: exc is…
Woodoo
  • 129
  • 6
2
votes
0 answers

Python C-Extension comparing capsules

Let's say I have two capsules returned from my C-extension that both point to the same address. How could I compare these two capsule objects within python to check if they both point to the same address? I've tries just simply comparing them (using…
Ian Rehwinkel
  • 2,486
  • 5
  • 22
  • 56
2
votes
1 answer

Returning an array from python3 C extension

I wish to generate arrays in a C extension module and pass them back to python. The following code works for python2: C_generate_array.c: #include "Python.h" #include "arrayobject.h" #include "C_generate_array.h" #include static…
Ginger
  • 21
  • 2
2
votes
1 answer

python works in c++ in debug mode, but not in exe file

I am working on a Python code embedding in c++ helloworld program, necessary additional include/library directories are properly set up. When I use Local Windows Debugger, it shows "Hello World" correctly. But if I double click project.exe, it…
Tiger Hu
  • 47
  • 7
2
votes
1 answer

Is there a way to resize python list using Python-C API?

My C-application is loading the python interpreter dll and calling the function list = PyList_New(len) to create a new python list of 'len' size. Two questions: 1. What happens if I call PyList_Append(list, item) more than len times? I assume it…
Rakesh Agarwal
  • 3,009
  • 9
  • 33
  • 40
2
votes
0 answers

Python C API: Import module with absolute file path

I'm prototyping a Python C extension which should be able to import a large number of python modules very quickly with minimum overhead. My extension will know the module names and their absolute paths based on it's own logic. I'd like to avoid…
Alex
  • 93
  • 1
  • 5
2
votes
0 answers

ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)

I am new in C, I am writing a Python C extension to improve speed. here is my setup tool: # file: setup.py from distutils.core import setup, Extension extra_objects = ["/Users/rrg/Documents/test/aliyun-log-c-…
ruiruige1991
  • 615
  • 1
  • 10
  • 21
2
votes
1 answer

How to get format-string for data of a ctypes-pointer

Given a ctypes-pointer, for example double**: import ctypes data=(ctypes.POINTER(ctypes.c_double)*4)() # results in [NULL, NULL, NULL, NULL] is it possible to obtain a format string, which describes the memory layout of the data? Right now, I…
ead
  • 32,758
  • 6
  • 90
  • 153
2
votes
0 answers

Python C extension importer how to use import lock

Background I'm building an importer to encrypt my code. When compiling, I encrypt the code object in pyc files. When loading, I use this customized importer to decrypt the code object before execution. Since my code is bundled in zip format, I…
CtheSky
  • 2,484
  • 14
  • 16
2
votes
1 answer

Updating elements of an array using the Python3/C API

I have a module method which takes in a python list, and then outputs the same list with all items multiplied by 100. I've attemped to follow the C intro here as close as possible but still running into issues. static PyObject…
Jamie C.
  • 25
  • 5
2
votes
0 answers

Python C extension: How to DECREF a C array of PyArrayObjects

I am creating some C extension code for python (example below). Part of the function requires me to iterate arrays passed in as arguments, preform a calculation on each element, and return an array of the calculated values. I am using…
Haydon
  • 588
  • 1
  • 4
  • 13
2
votes
1 answer

Python - Convert PyInt into C int

i'm currently facing a problem when trying to retrieve the value returned by my python code. A draft of my code is : # I call a function that returns a PyObject PyObject* pyObj_val = call_binding(...); # This PyObject seems to be a PyInt as…
Sebastien M
  • 61
  • 1
  • 8
2
votes
1 answer

PyDict_SetItemString segfaults

I am trying to write a simple C extension for Python3, and it segfaults when I try to add a string to a dictionary. Here is my code: #include #include int main() { PyObject* dict_p = PyDict_New(); char *val =…
Alec Matusis
  • 781
  • 1
  • 7
  • 16
2
votes
0 answers

Any way of calling c/c++ code from python without altering the c/c++ code?

Edit done: I am wrapping a custom python dll with the purpose of removing the custom code eventually and using a recent python instead of the ancient version that is being used now. One of the functions initializes a python extension, and…
i30817
  • 1,356
  • 2
  • 13
  • 26
2
votes
3 answers

Can't kill multiprocessing pool in a multithreaded C application embedding Python

OS: linux Python version: 3.6 I'm trying to extend a C application with Python runtime. The C application uses pthread and I tried to use multiprocessing forkserver in Python runtime but faced a problem. When I try to kill the program with SIGINT…
user5538922