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

What type of reference does PyModule_GetFilenameObject return?

PyModule_GetFilename was apparently deprecated in version 3.2, with PyModule_GetFilenameObject being the suggested replacement. PyModule_GetFilenameObject returns a PyObject*, but the Python 3 C API documentation doesn't specify whether the returned…
shay
  • 755
  • 10
  • 22
0
votes
1 answer

How to get real string from python c-api to python script?

I try send a string from c++ to python string using: PyObject* pyString = PyUnicode_FromString("/abc/def.html/a%22.php?abc=&def=%22;%00s%01"); .... PyObject* pyArgs = Py_BuildValue("(z)", pyString); ... PyObject_CallObject(pFunc, pyArgs); But in…
e-info128
  • 3,727
  • 10
  • 40
  • 57
0
votes
1 answer

How to write auto-populate the parameter list of a class member method into a Variadic arguments?

I recently wrote an extension to Python 3 in C++, but I encountered some trouble when I called C++ in python. I don't know how to encapsulate the code below, without the need to write callback function repeated every time? I am considering binding…
Jerry
  • 45
  • 5
0
votes
0 answers

python/Pandas - you may need to run... to build the C extensions first - without cmd

I downloaded pandas from PyPI and I installed it (I use Python 2.7). When I try to run a little python script with pandas, I have that error : ImportError: C extension: DLL load failed: %1 is not a Win32 application. not built. If you want to import…
Q.L.
  • 57
  • 1
  • 6
0
votes
0 answers

Linking C module with Python 3.7. CMake in Windows

I'm trying to create a C library that can be called from Python, so far I've created the proxy C file that exposes the module information and the method table (For simplicity just one method is added get_cycle_count and its implementation…
Santanor
  • 566
  • 1
  • 7
  • 20
0
votes
0 answers

How gevent implemented cooperative multitasking before coroutines where introduced into Python?

Since introduction of coroutines required quite a few changes in the implementation of CPython itself, I wonder how gevent (greenlets actually, as it turned out) manage to do cooperative multitasking without modifying the interpreter, as for example…
Ben Usman
  • 7,969
  • 6
  • 46
  • 66
0
votes
2 answers

Python C API - How to assign a value to eval expression?

Is it possible to assign a value to an "eval expression" without manipulating the evaluation string? Example: The user writes the expression "globalPythonArray[10]" which would evaluate to the current value of item 10 of globalPythonArray. But the…
Joe M.
  • 311
  • 3
  • 5
0
votes
1 answer

How to clear a PyList object that contains objects?

I am trying to clear a Python list I pass to a C function using ctypes. The calls I found at this link seem to work sometimes, but when the list I pass in contains other lists or classes as elements I get a segfault. I have a file foo.py that looks…
cjconte
  • 3
  • 2
0
votes
1 answer

python embedding in C++ with functions returning shared_ptr(pybind11/boost_python)

I am currently working on a usecase where I have implemented APIs in python and they need to return struct, vector and map to C++ caller functions. Could you please help me with an example how to do that. All the usecases I see in pybind/boost…
0
votes
1 answer

How to create an object of type numpy.int8 in a C numpy extension?

I have created my own class in a C Python extension. I want it to behave like a numpy array. Let's say my class is a myarray. I can index it using the slice notation. This means that my implementation of the mp_subscript function of the…
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
0
votes
1 answer

Portable Python C-API build with CMake

Let's consider the following trivial code for connecting C to python as follows // main.cpp #include int main() { Py_Initialize(); return 0; } The following CMake code works fine for…
ar2015
  • 5,558
  • 8
  • 53
  • 110
0
votes
1 answer

Returning a function python-c-api

I am creating Python bindings for a C library. In C the code to use the functions would look like this: Ihandle *foo; foo = MethFunc(); SetArribute(foo, 's'); I am trying to get this into Python. Where I have MethFunc() and SetAttribute()…
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
2 answers

Calling custom C subroutines in a Python application

I have two custom-written C routines that I would like to use as a part of a large Python application. I would prefer not to rewrite the C code in pure Python (or Cython, etc.), especially to maintain speed. What is the cleanest, easiest way that I…
bearplane
  • 700
  • 1
  • 9
  • 22
0
votes
1 answer

Python crashes on returning an C++ map value

i am writing a Test to keep Data in a C++ map. I am using the Python C-Api. Adding int values and getting the length of the map is no problem. But when i get a value from the map and want to return it to Python than the Interpreter crashes. This is…
0
votes
1 answer

IPython behaves differently than CPython when interacting with c extension

I have developed a small python c-extension, with a flex scanner, that works perfectly fine in CPython. All my tests pass. But in IPython the output differs or IPython dies with a malloc error, whenever I try to interact with my c-extension. When I…
Gellweiler
  • 751
  • 1
  • 12
  • 25