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

Python C-API access String constants

I wanted to implement a library I have written for python in C using the C-API of python. In python I can declare "constants" in my module by just stating: RED = "red" # Not really a constant, I know BLUE = "blue" # but suitable, nevertheless def…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
5
votes
1 answer

Define a global in a Python module from a C API

I am developing a module for Python using a C API. How can I create a variable that is seen as global from Python? For example, if my module is module, I want to create a variable g that does this job: import module print module.g In particular, g…
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
5
votes
1 answer

Alternatives of fused type in cython

I am working on rewriting a python module originally written in C using python-C api to Cython.The module also uses NumPy. A major challenge of the project is to maintain the current speed of module and also it should work for all Numpy data types.…
bewithaman
  • 768
  • 8
  • 16
5
votes
1 answer

Python compare objects in C API

Given two PyObject*s, how can I compare them in C API? I thought of a == b at first, but it's clearly incorrect since it would compare the pointer and not the object. I'm looking for a == b (not a is b) Python equivalent in Python C API.
Nacib Neme
  • 859
  • 1
  • 17
  • 28
5
votes
1 answer

PyErr_CheckSignals not picking up signal

I have a C-extension which implements an LRU cache https://github.com/pbrady/fastcache . I've recently noticed that in an application (SymPy) which makes fairly heavy use of caching, a timeout signal gets lost and the application continues to run. …
ptb
  • 2,138
  • 1
  • 11
  • 16
5
votes
1 answer

How to deal with PyCapsule type inside Python

I'm trying to pass the object from QtGui.QWidget.effectiveWinId() to win32gui.SetWindowLong() effectiveWinId() is returning: and SetWindowLong() expects a PyHANDLE (doc says it "should"…
David
  • 692
  • 8
  • 21
5
votes
1 answer

Python Function Capsules

I found this code snippet in the help of python 2.7.5, which is a chapter about exposing a C-API to other modules in the Extending Python with C and C++ section: Providing a C API for an Extension Module /* C API functions */ #define…
Sebi2020
  • 1,966
  • 1
  • 23
  • 40
5
votes
1 answer

How to declare a python c-extension method as a classmethod?

I'm writing a python wrapper for a C++ class that provide several static methods for alternate "constructors". I'm wondering how to export these through the python c-api? Here is a stub of the relevant C++ code. PyObject *PyFoo_FromFoo(Foo foo); …
Dov Grobgeld
  • 4,783
  • 1
  • 25
  • 36
5
votes
1 answer

Creating a numpy array of custom-class objects with C API

Using the C API, I would like to create a numpy array containing objects of type Quaternion, which is a class I've written in C++. I already have an array of these (actually a std::vector), and I want to make a copy -- or use the same memory if…
Mike
  • 19,114
  • 12
  • 59
  • 91
5
votes
1 answer

Named parameters with Python C API?

How can I simulate the following Python function using the Python C API? def foo(bar, baz="something or other"): print bar, baz (i.e., so that it is possible to call it via: >>> foo("hello") hello something or other >>> foo("hello",…
Michael
  • 11,612
  • 10
  • 41
  • 43
5
votes
6 answers

How to import a file by its full path using C api?

PyObject* PyImport_ImportModule( const char *name) How to specify a full file path instead and a module name? Like PyImport_SomeFunction(const char *path_to_script, const char *name).
Elias Bachaalany
  • 1,180
  • 2
  • 13
  • 27
5
votes
1 answer

Explanation of PyAPI_DATA() macro?

I've searched all over the web and can't seem to find documentation or even a simple explanation of what PyAPI_DATA() does (even though it is used in the Python header files and cited on python.org). Could anyone care to explain what this is or…
Michael
  • 4,700
  • 9
  • 35
  • 42
5
votes
2 answers

C++ Python import class; call methods

I am using Python 2.7. It is not clear to me how to embed Python inside C++ as found here: http://docs.python.org/2.7/extending/embedding.html. I have a simple python example here in a file named test.py: class math: #def __init__(self): …
Doo Dah
  • 3,979
  • 13
  • 55
  • 74
5
votes
1 answer

Calling numpy function from C-code

I'm trying to move some MatLab code with Mex extensions into Python with numpy and scipy libraries. Using this wonderful tutorial http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays, I quite quickly adopt C-functions for calling from the Python.…
rth
  • 2,946
  • 1
  • 22
  • 27
5
votes
1 answer

When is PyEval_InitThreads meant to be called?

I'm a bit confused about when I'm supposed to call PyEval_InitThreads. In general, I understand that PyEval_InitThreads must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used. However, I'm…
Channel72
  • 24,139
  • 32
  • 108
  • 180