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

How to pass an array from C to an embedded python script

I am running to some problems and would like some help. I have a piece code, which is used to embed a python script. This python script contains a function which will expect to receive an array as an argument (in this case I am using numpy array…
user1750948
  • 719
  • 2
  • 10
  • 27
13
votes
3 answers

What is the best way to deal with "_d" suffix for C extensions when using debug build?

I'm trying to debug my C extension for Python 2.7. I use python2.7 debug build. I build my project with setuptools and my setup.py has such lines: ext_modules=[Extension("my.extension", ["my/_extension.c"])] When I invoke…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
12
votes
2 answers

Method without return value in python c extension module

I'm trying to create a script in python that sends data through a parallel port. I'm creating my own module in C language. The problem is: when I try to execute my module, python crashes. No errors, no data, nothing. It simply closes. This is my…
markmb
  • 852
  • 4
  • 12
  • 32
12
votes
2 answers

Passing a C pointer around with the Python/C API

I'm new to the Python/C API ... I'm trying to add new functionality to my C program, wherein I can embed python into it and simultaneously extend functionality so that the embedded interpreter can execute a script that will interact with an…
user1088396
  • 121
  • 1
  • 1
  • 3
12
votes
1 answer

Embedding multiple Python sub-interpreters into a C program

I am writing a C program that spawns multiple C threads, with one Python sub-interpreter per thread. The sub-interpreters do not share any mutable Python variables, they are isolated from each other. (They do have a read-only access to a common…
Alec Matusis
  • 781
  • 1
  • 7
  • 16
12
votes
1 answer

How is __slots__ implemented in Python?

How is __slots__ implemented in Python? Is this exposed in the C interface? How do I get __slots__ behaviour when defining a Python class in C via PyTypeObject?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
12
votes
2 answers

Multithreading with Python and C api

I have a C++ program that uses the C api to use a Python library of mine. Both the Python library AND the C++ code are multithreaded. In particular, one thread of the C++ program instantiates a Python object that inherits from threading.Thread. I…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
12
votes
2 answers

Define Python class from C

I wrapped some C code for Python and it works. The C module creates a handle, which I pass to Python as PyCapsule. The API I would like to have can be made in Python like: import wrapped class Test(object): def __init__(self, loc ): …
Lars Hanke
  • 614
  • 5
  • 16
12
votes
1 answer

"AttributeError: 'module' object has no attribute 'argv'" when using Python.h

When messing around with Python.h I got this error: AttributeError: 'module' object has no attribute 'argv' C++ code: #include "stdafx.h" #include "C:/Python27/include/Python.h" #include using namespace std; int main() …
Chengy
  • 619
  • 1
  • 7
  • 17
12
votes
1 answer

How to specify docstring for __init__ in Python C extension

Perhaps a stupid question: how can one specify docstring for special functions like __init__ when writing a C extension? For ordinary methods, method table has provision for docstrings. The following autogenerated documentation is displayed when I…
subhacom
  • 868
  • 10
  • 24
12
votes
1 answer

Python C API: how to get string representation of exception?

If I do (e.g.) open("/snafu/fnord") in Python (and the file does not exist), I get a traceback and the message IOError: [Errno 2] No such file or directory: '/snafu/fnord' I would like to get the above string with Python's C API (i.e., a Python…
ashcatch
  • 2,327
  • 1
  • 18
  • 29
11
votes
2 answers

File I/O in the Python 3 C API

The C API in Python 3.0 has changed (deprecated) many of the functions for File Objects. Before, in 2.X, you could use PyObject* PyFile_FromString(char *filename, char *mode) to create a Python file object, e.g: PyObject *myFile =…
Vicent Marti
  • 7,203
  • 6
  • 30
  • 34
11
votes
1 answer

Build a PyObject* from a C function?

I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*); so that I can use those functions in the embedded Python. So I…
Manux
  • 3,643
  • 4
  • 30
  • 42
11
votes
2 answers

Limitations of PyTuple_SetItem

I have a Python extension module which creates a tuple as an attribute of another object, and sets items in the tuple. Whenever I execute this module in Python, I keep getting the error SystemError: bad argument to internal function After reading…
Channel72
  • 24,139
  • 32
  • 108
  • 180
11
votes
1 answer

How to build C extensions via poetry?

To build a python project managed with poetry I need to build C extensions first (an equivalent to python setup.py build). poetry is able to do this according to this github issue. But to me it's not clear what to include into pyproject.toml that…
thinwybk
  • 4,193
  • 2
  • 40
  • 76
1 2
3
73 74