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

What is the correct way python memory management in case of exceptions?

I am writing my own Python module and need some advice. Let's consider an example function in a module: PyObject* my_func(PyObject *self, PyObject* args) { PyObject* returnObj; try { returnObj = my_create_output(); } …
Samvel Hovsepyan
  • 639
  • 2
  • 6
  • 16
2
votes
1 answer

How to add a built-in module to a C-Python API after Py_Initialize?

I have a module defined in my C code like so: static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, "the_module_name", /* m_name */ module_documentation, /* m_doc */ //.... }; and a function to initialize…
brita_
  • 487
  • 7
  • 13
2
votes
1 answer

Adding symbolic constants with hex values to Python extension module

I have a few values defined as symbolic constants in my header file: #define NONE 0x00 #define SYM 0x11 #define SEG 0x43 ... The names of these values represent a certain type of data. Now in my current implementation of my module I put all…
plover
  • 439
  • 5
  • 17
2
votes
2 answers

How to pass a tuple of slice objects to C via the python-C api

I have an 2d matrix class in c that I am using in python. I want to be able to call the matrix class by passing a tuple of slice objects, e.g., A[1:2,1:2]. So far what I have is the following. The relevant C code for the __getitem__ method looks…
Bluegreen17
  • 983
  • 1
  • 11
  • 25
2
votes
1 answer

Assignment into Python 3.x Buffers with itemsize > 1

I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so: int Image_get_buffer(PyObject* self, Py_buffer* view, int…
Toji
  • 33,927
  • 22
  • 105
  • 115
2
votes
1 answer

How to use PyArray_SearchSorted in Numpy C API

In a C extension, I am accessing two arrays passed to the function: PyObject *xw_array = PyArray_FROM_OTF(xw_obj, NPY_DOUBLE, NPY_IN_ARRAY); PyObject *x1_array = PyArray_FROM_OTF(x1_obj, NPY_DOUBLE, NPY_IN_ARRAY); and I then want to use…
astrofrog
  • 32,883
  • 32
  • 90
  • 131
2
votes
1 answer

How to embed IronPython in a non-.NET application?

Is it possible to call IronPython functions (and get access to their return values), from C/C++ code, without .NET? And if yes, how? Is an embedding approach as in the python-c-api possible (i.e. as described here:…
c_k
  • 1,746
  • 1
  • 20
  • 35
2
votes
2 answers

Access contents of PyBuffer from C

I have created a buffer object in python like so: f = io.open('some_file', 'rb') byte_stream = buffer(f.read(4096)) I'm now passing byte_stream as a parameter to a C function, through SWIG. I have a typemap for converting the data which looks like…
Pavel
  • 729
  • 2
  • 11
  • 22
2
votes
0 answers

Embedding Python: How to use custom type inside Python script?

I try to run some Python scripts from inside the C++ code. I reach the point, in which I need to use my custom type. I found article in Python doc about creating custom types and nice SOQ, explaining how to create instances of custom type on C++…
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
2
votes
1 answer

How convert and save Python module (from PyObject*) as binary data to use it later?

I am doing some tests with Python/C API to understand how does it work and how properly use it. My goal is to create C++ wrapper, that allows me to run Python scripts from C++ code. I cannot use external bindind libraries (like Boost.Python or…
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
2
votes
1 answer

Nested modules (packages) with python c api?

How can I create nested modules (packages?) with the python c api? I would like the client code (python) to be able do something like this: import MainModuleName import MainModuleName.SubModuleName Instead of currently: import MainModuleName import…
monoceres
  • 4,722
  • 4
  • 38
  • 63
2
votes
2 answers

Using Python instead of XML for loading resources in C++?

I am building a simple 2D game (for learning purposes) in c++ and am currently parsing XML files using TinyXML to load my textures and other resources. Recently, however, I have been intrigued by python and wish to use python instead of XML for…
2
votes
1 answer

Return list of new custom-class objects in python C API

I need to create a new list via the python C API containing new copies of objects of a Quaternion class I've written (in C++). [Actually, I'd really like a numpy array, but any sort of sequence would do.] But I'm getting seg faults with everything…
Mike
  • 19,114
  • 12
  • 59
  • 91
2
votes
0 answers

Using either version of Python

I have a program (written in C and C++) that embeds Python and links with the Python dynamic library. Currently it uses Python 2.6, but the requirement is to use whatever version is being installed on the machine (currently either 2.6 or 2.7; no…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
2
votes
1 answer

C code within python and copying arrays in C code

I am sorry if this question is incomplete or hard to understand, I am trying to figure it out myself ! I have the following C code, inside python-both languages that I am not very good at. The following C code copies numpy array I think. Its not…
Steve Grafton
  • 1,821
  • 4
  • 17
  • 18