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

Why does PyLong_AsUnsignedLongLong function fail to convert a numpy.uint64 element, whereas PyLong_AsLongLong succeeds?

I'm working on a C-extension for Python which implements a method that converts a list of numpy elements (numpy.uint64 in this case) to the unsigned long long C-type (using the PyLong_AsUnsignedLongLong function). The elements of the list are then…
Gavriil Pascalau
  • 151
  • 1
  • 12
0
votes
0 answers

Writing the result of a converted code to a plain text file

I am embedding Python in a Larger C++ application using the Python C API. My goal is to make a game that can teach programming using Python, and the player actually types in Python Code, which is why I'm embedding an interpreter. I am aware that…
0
votes
2 answers

Build a PyObject* with a C Object pointer

Say I have this struct: typedef struct { PyObject_HEAD Foo* myFoo; } PyFoo; Let's just say that Foo is: class Foo { public: hello() { std::cout << "Hello\n"; } }; I don't want to remake class Foo as a python module because it…
A student
  • 170
  • 11
0
votes
1 answer

PyFunctionObject crash the programm in Py_Finalize if it has being set item in PyTuple

I need get list of function name and signatures by python file. Python function inspect.signature can get me function signature. But inspect.signature need a function object (PyFunctionObject). inspect.getmembers can return such object. But if I set…
Nukopol
  • 11
  • 1
0
votes
2 answers

Files form .py not reading into C

Lets say I have float x from main import progC def main(): x = 3 y=progC(x) print y if __name__ == __main__ main() #include static PyObject* py_bracket(PyObject* self, float x){ x = x+5; return…
TT12
  • 97
  • 1
  • 1
  • 5
0
votes
1 answer

A problem in manually reproducing dict underlying code:the value can't be object

I tried to manually reproduce python dict underlying code(it can only implement setitem and getitem), but I met a problem: The hashmap I wrote can only work well when the value type is basic data types(like int,str etc).If the value's type is…
0
votes
0 answers

linking a dll does not link in .lib files and expects other the .dll form

I'm trying to build a python c extension on windows with msvc (using distutils) which links in some other static libraries. Basically I have test1.lib, test2.lib and test3.obj and want to build test4.dll which has test1.lib, test2.lib and test3.obj…
mitch
  • 37
  • 6
0
votes
1 answer

Numpy with PyPy problems

I'm trying to install numpy and other packages with PyPy. I get the error: "error: Microsoft Visual C++ 14.1 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/" I used "pypy3 -m pip install…
bkvstar
  • 11
0
votes
1 answer

Do Python C Extensions release the GIL for CPU tasks as they do for IO tasks?

Python C Extensions always release the GIL via Py_BEGIN_ALLOW_THREADS when making IO system calls. I have read mixed opinions on whether C Extensions release the GIL for relatively long running CPU-bound tasks. I've read that CPU inside the python…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
0
votes
1 answer

How to use python "include" and "libs" path in a windows makefile to compile all python embedded C++ program in a folder?

Makefile specified in this question, compiling all the cpp programs in a folder but not with python embedded cpp programs. all: myUB sourcesC := $(wildcard ../src/*.cpp) objectsC := $(patsubst…
Alex
  • 35
  • 6
0
votes
1 answer

Memory Leak in C-extension for Python

This is the first time I am writing a C-extension for python and you can see my ugly and probably super inefficient C++ implementation of a convolution. I have a problem with the memory management. Each time I call this function in python it…
kleka
  • 364
  • 3
  • 14
0
votes
1 answer

Why Python IDLE crash with error StackHash_0a9e?

My code compares 2 lists of tuples and displays "True" if the all elements of the first tuple are in the range of the second tuple. The range is set by the argument "ar". #include PyObject *compare( PyObject *self, PyObject *args ) { …
0
votes
0 answers

How to use basic string operations with PyObject* strings?

I would not like to keep converting every Python String Object from PyObject* to std::string or char* with PyUnicode_DecodeUTF8 and PyUnicode_AsUTF8 because it is an expensive operation. On my last question How to extend/reuse Python C…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
0
votes
1 answer

How to extend/reuse Python C Extensions/API implementation?

The problem is that now, I have to use the Posix C getline function to get the line from the file, only then convert it to a Python Unicode Object using PyUnicode_DecodeUTF8 and cache it using my caching policy algorithm. This process is losing 23%…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
0
votes
1 answer

How to return a cached PyObject* value several times without memory leaking or double/triple frees?

On my last question How to improve Python C Extensions file line reading? was brought up some memory problems I had. Then, I wrote this simple and ridiculous usage of and Python C Extension, just with the purpose of trying to understanding better…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144