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
1
vote
2 answers

Python C API, How to call base type method from subtype?

I created a subtype from one existing Python type with C API by following this guide Python C API Defining New Types I overridden one base type's method by registering the new method thanks to tp_methods attribute of the new PyTypeObject. This is…
1
vote
2 answers

Tried to embed python in a visual studio 2010 c++ file, exits with code 1

I am trying to embed some python code in a c++ application i am developing with ms visual studio c++ 2010. But when i run the program, it exits with code 0x01 when i call Py_initialize(). I dont know how to find out what went wrong. the help file…
1
vote
1 answer

How to use PyDict_SetDefault with regard to the refence count of default

I'm wondering about how to use PyDict_SetDefault without creating a reference count catastrophe. The documentation doesn't say anything about a stolen reference and some testing shows that it doesn't "steal the reference" of default. But it increfs…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
0 answers

Conditional importing in "from extern" in cython based on python version

I found that naive conditions in def extern from ... blocks are not allowed. For example if I use something like this: from cpython.object cimport PyObject, PyObject_Hash from cpython.version cimport PY_MAJOR_VERSION, PY_MINOR_VERSION cdef extern…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
1 answer

C/C++ Python exception traceback not being generated

I recently upgraded a C++ project I'm working on, which embedds python, from Python 3.4.3 to Python 3.5.2 (it is an executable that builds as 32 or 64 bits, and has the same behavior in both versions). I am creating my own exception: static…
teeks99
  • 3,585
  • 2
  • 30
  • 38
1
vote
0 answers

How to get finer control over sequence attributes in python C API?

I am trying to add a new class/type to python via C/C++. This particular class has a vector like attribute called x, it is a list but it is made only from positive doubles. I used the tp_getset to have finer control over setting x when defined like:…
Lawless
  • 75
  • 1
  • 5
1
vote
1 answer

"No module named '_'" when importing a SWIG module with embedded Python

I'm trying to use SWIG with embedded Python 3.5.2. The following is built as a Windows console app. It fails initializing the Python side SWIG module "arpy.py" when it tries to import the C++ side SWIG module "_arpy". My (probably incorrect)…
tukra
  • 921
  • 8
  • 13
1
vote
2 answers

What can you do with a PyDictValues_Type?

While trying to convert some Python/C API code to work in both 2 & 3, I found that, given the following Python DICT = { … } class Example(object): ITEMS = DICT.values() and then calling PyObject_GetAttrString(an_example, "ITEMS") would yield a…
Nathan Herring
  • 977
  • 8
  • 16
1
vote
1 answer

Difference iterating with tp_iternext or PyIter_Next

If I write a C function that does something with an iterable then I create an Iterator first and then loop over it. iterator = PyObject_GetIter(sequence); if (iterator == NULL) { return NULL; } while (( item = PyIter_Next(iterator) )) { …
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
1 answer

Stop embedded Python prompt from C++

I'm running Python embedded in a C++ application. The program consists of a Qt GUI and a work QThread where computations happen. The user can chose to run a Python script from a file or to start a Python prompt. Both run in the QThread. The program…
Jose
  • 46
  • 7
1
vote
1 answer

C-numpy: Setting the data type for fixed-width strings?

I'm working with some data that is represented in C as strings. I'd like to return a numpy array based on this data. However, I'd like the array to have dtype='SX' where X is a number determined at runtime. So far I am copying the data in C like…
Max
  • 301
  • 1
  • 9
1
vote
1 answer

Converting a numpy array into a c++ native vector

I currently have a python C extension which takes a PyObject list and I can parse is using 'PySequence_Fast'. Is there an equivalent command which would allow my to parse a one dimensional numpy array? Cheers, Jack
JMzance
  • 1,704
  • 4
  • 30
  • 49
1
vote
1 answer

How to work around missing PyModule_Create2 in AMD64 Win Python35_d.lib?

I'm trying to debug an extension module that worked fine in 32 bit Python 2.7, but not so fine in 64 bit Python 3.5. I've used the AMD64 Web installer from Python.org, and yet in the link I get __imp_PyModule_Create2 (referenced in…
Peter Du
  • 548
  • 1
  • 10
  • 20
1
vote
1 answer

Embedding Python in C: Passing a two dimensional array and retrieving back a list

I want to pass a list of arrays (or a 2D array) such as [[1,2,3],[4,5,6]] from C to a Python script which computes and returns a list. What possible changes would be required to the embedding code in order to achieve this? The python script to be…
suzaku
  • 11
  • 4
1
vote
1 answer

Passing a C++ pointer between C and Python

I have a python extension module written in C++, which contains multiple functions. One of these generates an instance of a custom structure, which I then want to use with other functions of my module in Python as follows import MyModule var =…
metm
  • 43
  • 6