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

Wrap a c library with python only

I was provided with a c library wave.so, with a function interfaced defined, I follow the guide here https://stackoverflow.com/a/5868051/2789784 and it works. However, when I made the script to be a file MyModule.py, and try to import by import…
user40129
  • 763
  • 1
  • 5
  • 15
1
vote
1 answer

Embedded Python (C API): How to run .py file repeatedly without reinitializing

I'm having a heck of a hard time with embedded python. I'm running a DLL, and each time a certain function is called I want to run a Python script. I want to avoid calling Py_Finalize() at the end of the function because the Initialize/Finalize…
gkimsey
  • 517
  • 6
  • 13
1
vote
1 answer

Python C API: when are we at the end of a Python instruction?

My understanding is that in PyEval_FrameEx(), the main loop is on each opcode instruction. Has PyEval_FrameEx the knowledge of being at the end of a Python instruction? Maybe there's an opcode for that?
SebGR
  • 293
  • 2
  • 10
1
vote
1 answer

Getting return values in Python C API

I am developing a specific IDE for python in C++, using Python C API. I would like embed a python terminal in it. I tried using PyRun_SimpleString, its works but it don't behave like a python terminal (for example, 1 + 1 don't show the answer). I am…
1
vote
1 answer

python c api not able to import any module into the newly created module

Here is the code : python_script[] = "try:\n\timport sys\nexcept:\n\tprint\"cannot import sys\""; pNewMod = PyModule_New("mymod"); Py_Initialize(); pGlobal = PyDict_New(); pLocal = PyModule_GetDict(pNewMod); PyRun_String(python_script,…
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
1
vote
1 answer

Dynamic arg types for a python function when embedding

I am adding to Exim an embedded python interpreter. I have copied the embedded perl interface and expect python to work the same as the long-since-coded embedded perl interpreter. The goal is to allow the sysadmin to do complex functions in a…
Todd Lyons
  • 998
  • 12
  • 19
1
vote
1 answer

Use C++-Iterators on Python-List with the Python/C API?

Is it possible to use the tools only needing Iterators and a function-pointer from the module on PyObjects ? The concrete problem I want to solve (it is constructed to learn from it): I have a huge list of IDs stored in a python-list …
zvyn
  • 706
  • 8
  • 17
1
vote
1 answer

PyObject segfault on function call

I'm trying to use Python to open a dialog to accept input into my C++ application. Here is a very minimal representation of what I am trying to do: #include #include int main() { /* Begin Python Ititialization - only…
Neil
  • 466
  • 1
  • 6
  • 15
1
vote
1 answer

Can't get the function from python module when call at the second time

Below is the c++ code, it will call my python script 'rule.py' in the same directory. #include #include using namespace std; int dou(int a) { PyObject *pModule, *pDict, *pFunc, *pArgs, *pRetVal; if( !Py_IsInitialized()…
luxiaolu
  • 111
  • 5
1
vote
1 answer

Purpose and usage of PyModule_New

At face value, the C-API function PyModule_New and PyModule_NewObject obviously creates a new module object. The official Python Documentation provides the following explanation for PyModule_NewObject: Return a new module object with the name…
Channel72
  • 24,139
  • 32
  • 108
  • 180
1
vote
1 answer

Pause/Resume embedded python interpreter

Is there any possibility to pause/resume the work of embedded python interpreter in place, where I need? For example: C++ pseudo-code part: main() { script = "python_script.py"; ... RunScript(script); //-- python script runs till the…
1
vote
1 answer

Can't install IMDbPY on windows

I'm having trouble installing IMDbPY on my windows computer. I think there is something wrong with my c compiler, because when I try to install it with easy_install or pip it gives me an error along these lines: C:>easy_install IMDbPY Searching for…
Alex S
  • 4,726
  • 7
  • 39
  • 67
1
vote
1 answer

Using a structure defined in C header as a Python data type

How do I use a C struct foo, defined in a header file as a datatype in my Python code? (This document does not seem to address the issue.) typedef struct { PyObject_HEAD /* Type-specific fields go here. */ struct api_query query; /*…
qwrty
  • 331
  • 3
  • 15
1
vote
0 answers

Easiest and Fastest way to Embed a Python Script into C?

I have been looking at the Python/C API for embedding my python script into C. I want to know if there is an easy (or simpler) way to embed my python script without having to get too involved in the python/c API. I should mention (cause I am not…
user1750948
  • 719
  • 2
  • 10
  • 27
1
vote
1 answer

finding out how many arguments a PyObject method needs

We can extract a PyObject pointing to a python method using PyObject *method = PyDict_GetItemString(methodsDictionary,methodName.c_str()); I want to know how many arguments the method takes. So if the function is def f(x,y): return x+y how do…
jkcl
  • 2,141
  • 3
  • 16
  • 19