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

Creation of PyTuple in C++ module crashes

Having some trouble with this code. Trying to return a tuple of tuples (coordinates) from a C++ module Im writing. It looks right to me, the dirty list contains two Coords so len is 2, the x and y values of the items in the list are 0,0 and 0,1…
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
0
votes
1 answer

Where does Python gets its traceback information from?

Consider the following verysimple.py: if '__main__' == __name__: prnt('Line1') Now, if I execute it with > python verysimple.py, I am, of course, greeted by: Traceback (most recent call last): File "verysimple.py", line 2, in…
SebGR
  • 293
  • 2
  • 10
0
votes
1 answer

Embedding Python 3.3 in a C++ program while only able to read one line at a time from input

I am currently working on adding embedded Python support (and yes, extending is not an option) to a large program as part of my summer internship. Ideally, I can keep the Python support within a single .DLL, which currently contains the program's…
0
votes
1 answer

Passing python method's reference to C as callback

Can we pass a reference of python method as an argument to C. This method is a callback method which will be executed after the C code has been executed.
Chaitanya
  • 3,399
  • 6
  • 29
  • 47
0
votes
1 answer

Extending currently written Klatt C program to interface with script using numpy

Currently I am using Klatt to create vowels and it was written and compiled with C http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/areas/speech/systems/klatt/ . I automatically create the vowels using a Python program by invoking terminal…
J Spen
  • 2,614
  • 4
  • 26
  • 41
0
votes
0 answers

Retrieve return values from a Python script in C++

I would like to be able to retrieve some results from C++ when I call a Python script using the following command: int nResult = PyRun_SimpleFileEx(PyFile_AsFile(pyFileObject), strScriptPath.GetBuffer(), 1); The goal here is to be able to…
MasterMind
  • 379
  • 2
  • 17
0
votes
0 answers

Convert python object to c array

What is the simple and effective way to create c contiguous array from python object? Suppose I wish to create C++ Matrix class, that can be constructed using python object. template struct Matrix { Matrix(PyObject* obj) { …
DikobrAz
  • 3,557
  • 4
  • 35
  • 53
0
votes
1 answer

Python C Api and correct handling of memory in C++ classes

I'm wondering how to cope with the following problem. Inside my C++ class I have an auxiliary PyObject pointer. class Foo { public: // Should I new the dictionary here in constructor? Foo() { } // Must decrease the…
linello
  • 8,451
  • 18
  • 63
  • 109
0
votes
1 answer

Wrapping a C function that expect C dynamic callbacks

I am trying to write a wrapper around libedit (a BSD alternative to readline with a slightly different API) and there is a way to add a named function to the C API. For example in C: static unsigned char show_help(EditLine *e, int ch) { …
nemith
  • 927
  • 5
  • 5
0
votes
1 answer

Compiling Python Imaging Library with MinGW

I'm trying to compile Python Imaging Library on Windows 7 64-bit by running pip install pil I've set my compiler to MinGW. It looks like it's going fine until it's time to compile _imaging.c. It fails on this monster gcc call: …
Mark
  • 5,286
  • 5
  • 42
  • 73
0
votes
1 answer

what is the right approach to pass default values in python extesnions?

How can i pass the default values to c extensions like 10,20 ,the below code doesn't take the values for a =10 and b =20. In below code example.py is simple a python code which is calling the c function calculate . example.py def Multiply: …
jaysh
  • 41
  • 1
  • 6
0
votes
1 answer

calling third party c functions from python

I have a requirement of calling third party c functions from inside python. To do this I created a c api which has all the python specific c code ( using METH_VARARGS) to call the third party functions. I linked this code liba.so with the 3 party…
marvel
  • 33
  • 7
0
votes
1 answer

Python C-Extension segfaults when accessing through tp_getset

I'm trying to write a C-Extension for python. What I'd like to write is a ModPolynomial class which represents a polynomial on (Z/nZ)[x]/x^r-1[even though you may answer to my question without knowing anything of such polynomials]. I've written some…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
0
votes
1 answer

Python object extension which gets a list in constructor never passes the creation step (SIGSEV), why?

I've been fighting for a lot of time with an error and I've run short of ideas on what's happening and why it doesn't work. First of all, I'm trying to create a new object type for Python through a C extension. This object is created using a list,…
0
votes
1 answer

Passing Tuple Embedded Python

So I'm writing a C++ application that uses embedded calls to Python to run some computations. I have code working for running any script in a specified directory, as well as passing and returning one value between the C++ application and the Python…
Mister R2
  • 861
  • 5
  • 12
  • 22