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

How to create submodules with the Python C API?

I want to create the following module and object structure: main_module | `--sub_module | +--ObjectOne | `--sub_sub_module | `--ObjectTwo When I only had main_module.sub_module everything worked as it should, as I created an…
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
0
votes
2 answers

C and Python integration: name of callback function

I am working on a project which requires Python with C integration. I have got it mostly working except for one issue below: How do I get the string representation of the callback function object in the C code. (See my comment in the code) import…
PySandy
  • 23
  • 3
0
votes
1 answer

error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'

I am passing a python module to C as a PyObject. I want to check to see if this value is NONE in my C code, using this form: int func(PyObject tmp) { if(tmp) { // etc I am getting the following error. How can I convert from a PyObject…
kilojoules
  • 9,768
  • 18
  • 77
  • 149
0
votes
0 answers

Python C package source code header files are missing

I am working on modifying source code for python C extension module ceODBC. I set up the C source code of ceODBC in Visual studio as a C++ project and linked python source code (so including Python.h in ceODBC won't cause any issue). However, there…
Joy3223
  • 1
  • 1
0
votes
0 answers

Import C module not working in Python 3

I tried to write a module for python using the c-api but the module wouldn't import. The problem persists for this minimal example: #include #include static PyMethodDef CAPMethods[] = { {NULL, NULL, 0, NULL} /*…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
0
votes
1 answer

Convert Python dictionary into C like structure

I am a newbie in Python and C and I would like to know how to put dictionary elements into a C like structure (struct). For example, here is my structure: typedef struct { int dim; float *Tab1; float *Tab2; } Tableaux; Here is my…
SOCKet
  • 191
  • 1
  • 2
  • 15
0
votes
2 answers

Check if return value is instance of Py_None

I have to use a c function (myAPI.readArr) that returns a scalar,numpy.ndarray or Py_None on failure. This works for a failure: data = myAPI.readArr(arg1, arg2) if not data: raise Exception("Problem!") but for valid arguments I get: ValueError:…
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
0
votes
1 answer

Strange behaviour when calling python functions from cython c

I have some C++ code, some python code and some cython code. In C++, I have an asynchronous callback that gets executed, and I want python code to be executed. Here's what I did. In python, I wrote a function that took 2 parameters: def fn(x,y): …
thejinx0r
  • 444
  • 7
  • 10
0
votes
1 answer

Only one character returned from PyUnicode_AsWideCharString

I'm trying to get in a Visual Studio 2012 C++ program the value of a string variable defined in a embedded Python environment using Python's C API. As embedded environment, I'm using Python 3.4.3 64-bit, which I downloaded from…
Pep
  • 1,957
  • 2
  • 24
  • 40
0
votes
1 answer

C++-Python Interop: Marshalling data faster

I'm working on an app that has a C++ and Python API, and am implementing an API call that needs to be used in both languages. Part of the data this API sends back is an array of structs representing color information: struct { float r; …
easythrees
  • 1,530
  • 3
  • 16
  • 43
0
votes
1 answer

To convert PyBytesObject type to PyUnicodeObject type in python3

How to convert pyunicodeobject type to pybytesobject type? Example: function(PyBytesObject* byteobj){ ....operation.. } PyUnicodeObject* Uniobj; function((PyBytesObject*) Uniobj); got a bus error as a result.
Thun
  • 51
  • 2
0
votes
1 answer

Synchronizing embedded Python interpreters

Is there a way to synchronize two or more python interpreters (either embedded in a single process, or running in separate processes) on the opcode level, other than hacking on the Python VM implementation(s)? Are there any alternative Python VM…
Petr Broz
  • 8,891
  • 2
  • 15
  • 24
0
votes
2 answers

Importing Class in Python Subpackage imports more than requested

Overview I'm running some scientific simulations and I want to process the resulting data in Python. The simulation produces a custom data type that is not used outside of the chain of programs that the authors of the simulation produced, so…
Joel
  • 2,065
  • 2
  • 19
  • 30
0
votes
1 answer

Searching "easy" approach for `tp_repr` and `tp_str`

I am creating an Python extension in C, for having a friendly structure and efficient usage on it. So there is a lot of source code for certain heavy operations which should be in C. And a lot of internal fields are referenced. Now, I was going to…
MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
0
votes
3 answers

convert PyFloatObject to float

I have a PyFloatObject that I want to convert to a C float. However, PyObjectFloat only provides a conversion to double. Can I safely cast a double to a float, or is there a function to convert PyObject to float i.e. PyObject_AsFloat.
ragingSloth
  • 1,094
  • 8
  • 22