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
0 answers

Define constants amongst Python and C (and C extension)

I have a project in which there are many names that, at the moment are hardcoded. The project is 2/5th C, 2/5th Python and 1/5th Python C-extension code. I would like to "define" a lot of names for the project. I was thinking in two general…
MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
0
votes
1 answer

Unicode key for Python dictionary in Python C API

I am using Python C API to connect to my python v2.7.2 As the title suggests, I am looking to use unicode string as key in my dictionary. I am aware that we can use unicode string as key in python dictionary. But how is that possible through Python…
D3XT3R
  • 181
  • 2
  • 15
0
votes
1 answer

How to construct a Complex from a String using Python's C-API?

How to use the Python C-API for the Complex class (documented here) to: convert a general PyObject (which might be a String, Long, Float, Complex) into a Complex PyObject? convert a Complex PyObject into String PyObject? Python has a complex()…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
1 answer

How does PyNumber_Float handle an argument that is already a float?

Documentation for PyNumber_Float (here) doesn't specify what happens if you pass in a PyObject* that points to another float. e.g. PyObject* l = PyLong_FromLong( 101 ); PyObject* outA = PyNumber_Float(l); outA will point to a newly created…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
1 answer

Force type conversions using Python C-API

I'm wondering whether it is possible to use Python's C-API to convert e.g. Float -> String, or Bool -> Int, or Int -> Bool etc Not every conversion makes sense, for example Dict -> List -- it would be hard to imagine what this would look…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
1 answer

Querying Python runtime for all objects in existence

I'm working on a C++ Python wrapper the attempts to encapsulate the awkwardness of reference counting, retaining, releasing. It has a set of unit tests. However I want to ensure that after each test, everything has been cleared away properly. i.e.…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
2 answers

C-API: Allocating "PyTypeObject-extension"

I have found some code in PyCXX that may be buggy. Is it indeed a bug, and if so, what is the right way to fix it? Here is the problem: struct PythonClassInstance { PyObject_HEAD ExtObjBase* m_pycxx_object; } : { : table->tp_new =…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
1 answer

Python SyntaxError when embedding in C

I have a python file which runs fine when I execute it against my python interpreter. I'm trying to call the same file from a C program using the python C API: #include #include int main(int argc, char* argv[]){ FILE* fp; …
jramm
  • 6,415
  • 4
  • 34
  • 73
0
votes
1 answer

Embedding Python in C++. Passing vector of strings receving a list of lists

I have a Windows application that is written in C++. I have a vector of strings that I need to pass to a python Script for processing. I know how to embed python in C++ with simple type but I am looking at how to create a python object equivalent to…
Eric
  • 97
  • 1
  • 9
0
votes
0 answers

Arbitrary builtin error on UUID call

Sorry, this was not a good question [edited, revised, summarized and diagnosed]. I have a Python C-API that works with UUID. I will omit error checking, but it is done for all Python and internal functions. [edit: ok, sorry about that, my bad... see…
MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
0
votes
1 answer

Passing line number to embedded Python interpreter

So I have a C program which uses an embedded CPython interpreter to execute Python code. The problem is that if the Python code has an error, the line number information provided by the interpreter is sort of useless, because each call to…
Siler
  • 8,976
  • 11
  • 64
  • 124
0
votes
1 answer

Using the python C API, is it possible to shrink a PyUnicode object?

I can't easily find out the exact size of the string I will produce. I only know the upper bound which should be within 1-2 characters of the final size. How do I shrink the string after filling it?
Hristo Venev
  • 972
  • 5
  • 17
0
votes
0 answers

Unable to import .so file present in a different directory using embedded Python

I am trying to import a .so file present in a different directory as a module using embedded Python, using the code: #include int main(int argc, char** argv){ PyObject *pimport, *pimport_sys; pimport=PyString_FromString("moddy"); …
Nihal Harish
  • 980
  • 5
  • 13
  • 31
0
votes
1 answer

populate Python list passed as argument to C function

I am writing a Python extension using C. How can I populate an empty list inside C when the list is passed as an argument and the C function does not return the list but a different PyObject*. b=[] a = c.populate(data=b) print b Output: [1, 2,…
zitro
  • 3
  • 1
0
votes
1 answer

How can I query the data of a complex return type using PyObject_CallObject?

I have a small python programm that computes a 2d positon def getPosition(lerpParameter): A = getClothoidConstant() ... The python script can be found in my project folder here. The function getPosition is included in the file clothoid.py.…
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90