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
4
votes
2 answers

Numpy/CAPI error with import_array() when compiling multiple modules

I am trying to compile a C++ module to use in scipy.weave that is composed of several headers and source C++ files. These files contain classes and methods that extensively use the Numpy/C-API interface. But I am failing to figure out how to include…
maurizio
  • 745
  • 1
  • 7
  • 25
4
votes
1 answer

New python type created but PyGetSetDef core dumps

I have below code which tries to create a new type and use getter method to access value. #include #include struct rangerr { long min; long max; }; //Python type to represent rangerr struct…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
4
votes
1 answer

Creating new numpy scalar through C API and implementing a custom view

Short version Given a built-in quaternion data type, how can I view a numpy array of quaternions as a numpy array of floats with an extra dimension of size 4 (without copying memory)? Long version Numpy has built-in support for floats and complex…
Mike
  • 19,114
  • 12
  • 59
  • 91
4
votes
1 answer

How to execute PyObject_CallObject() in two different threads simultaneously?

I have two functions running on two threads simultaneously. Both functions call this method PyObject_CallObject(pFunc,pArgs) to execute two different python functions. But i am getting access violation reading location exception when i try to do so.…
Kumar
  • 616
  • 1
  • 18
  • 39
4
votes
1 answer

Python C extension decorator

I apologize in advance for the long-winded background info. I've been playing around with the Python/C-API recently (Python 3.4) and have gotten stumped. My goal is to have a C extension which I can use as a function/method decorator. I have a…
ptb
  • 2,138
  • 1
  • 11
  • 16
4
votes
1 answer

ImportError when using Python C API

I have a binary which can load .so shared objects to extend functionality. These extensions are coded in C++ but I want to use some pre-coded python functions so I make use of Python C API. So far so good. Calls to Python functions work nicely, but…
Diego Herranz
  • 2,857
  • 2
  • 19
  • 34
4
votes
2 answers

Accessing the c pointer to structure in python

Is it possible to cast an int to a class type ? I have the following code in C: #include "Python.h" #define PYTHON_FILENAME "modelparam" void getmodelparam(long pModelParam) ; typedef struct { int seconds; int nanoseconds; } someTime; int…
Sagar Masuti
  • 1,271
  • 2
  • 11
  • 30
4
votes
1 answer

Python C-API module exit handler - an atexit equivalent?

I'm using Python ver 2.6.4 There is a function I have to call from a C library when my extension module exits/is unloaded. What would be the equivalent of atexit for a C extension module?
TheObserver
  • 2,973
  • 7
  • 33
  • 41
4
votes
2 answers

Python C-API Making len(...) work with extension class

When creating a class in Python, I can simply make a def __len__(self): method to make the len(InstanceOfMyClass) work, however I can't find out how to do this with an extension class via the C-API. I tried adding a __len__ method, but that appears…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
4
votes
1 answer

MIDIGetNumberOfDestinations returning 0 when using Python C API

I have the following midi wrapper for Python3: #include #include "structmember.h" #include #include "stdio.h" #define MESSAGESIZE 3 // CFStringRef to char* char * MYCFStringCopyUTF8String(CFStringRef aString) { …
Baz
  • 12,713
  • 38
  • 145
  • 268
4
votes
2 answers

How to redirect stderr in Python? Via Python C API?

This is a combination of my two recent questions: [1] Python instance method in C [2] How to redirect stderr in Python? I would like to log the output of both stdout and stderr from a python script. The thing I want to ask is, to create a new type…
EcirH
  • 473
  • 1
  • 5
  • 9
4
votes
3 answers

Python instance method in C

Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo() How to write the same functionality in C? I mean, how do I create an object with a method in C? And then create an instance from it? Edit: Oh,…
EcirH
  • 473
  • 1
  • 5
  • 9
4
votes
1 answer

Difference between PyInt_FromLong and Py_BuildValue

The Python/C API has a number of related functions that perform similar operations where one is usually for general use and another is somehow more efficient or convenient for a specific situation. For example, PyDict_SetItem and…
Jared
  • 25,627
  • 7
  • 56
  • 61
4
votes
2 answers

How to return a tuple containing a None value from the C API?

What is the appropriate way to return a tuple containing a None value from a C extension to Python? I know that a single None value can be created using result = Py_BuildValue(""); and that a tuple can be created using something like result =…
Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
4
votes
2 answers

PyDateTime_IMPORT macro not initializing PyDateTimeAPI variable

I'm using the Python C API on Windows using Visual Studio 2008. When I attempt to use the PyDate_Check macro, and other related macros, they cause an access violation because the static variable PyDateTimeAPI is null. This variable is initialized…
void
  • 99
  • 2
  • 6