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
1
vote
1 answer

Parsing Python Structure as PyObject

I'm returning object of following structure from a python function class Bus(Structure): _fields_ = [ ("a", c_int), ("b", c_char), ("c", c_float), ("d", c_double), ("e",…
P0W
  • 46,614
  • 9
  • 72
  • 119
1
vote
2 answers

Creating new types in Python using extensions

I've read the documentation and coded up my own example based on their Noddy example: https://docs.python.org/3.4/extending/newtypes.html This is a bit of a departure from usingPy_BuildValue()to build arbitrary objects dynamically. It appears that…
1
vote
1 answer

How to implement breakpoint functionality in a embedding of Python

I am using Python C Api to embed a python in our application. Currently when users execute their scripts, we call PyRun_SimpleString(). Which runs fine. I would like to extend this functionality to allow users to run scripts in "Debug" mode, where…
Nicko Po
  • 727
  • 5
  • 21
1
vote
2 answers

Python C API Boolean Objects

I am using Python C API 2.7.2 with my C++ console application. There is one doubt regarding Python C API Boolean Objects I am using: PyObject* myVariable = Py_True; Do I need to deference myVariable with Py_DECREF(myVariable)? The Python C API…
D3XT3R
  • 181
  • 2
  • 15
1
vote
1 answer

What's the correct name for a Python Extension-thingy?

Is it: Extension class Extension type Extension object ? Could somebody disambiguate? I'm asking because I have the following object structure in my C++ code, and I'm trying to figure out whether I should revise the names before I release my…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
1 answer

tp_dealloc not getting hit upon exit

I am embedding Python in C++. I have a working C++ Python extension object. The only thing wrong is that if I set tp_dealloc to a custom function it never gets called. I would have thought Py_Finalize() would trigger this, or maybe terminating the…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
2 answers

How to tidy/fix PyCXX's creation of new-style Python extension-class?

I've nearly finished rewriting a C++ Python wrapper (PyCXX). The original allows old and new style extension classes, but also allows one to derive from the new-style classes: import test // ok a = test.new_style_class(); // also ok class Derived(…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
1 answer

Why does PyCXX handle new-style classes in the way it does?

I'm picking apart some C++ Python wrapper code that allows the consumer to construct custom old style and new style Python classes from C++. The original code comes from PyCXX, with old and new style classes here and here. I have however rewritten…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
2 answers

Do PyObject_GetItem and PyObject_SetItem work on PyType_List and PyType_Dict types?

Documentation for PyObject_GetItem and PyObject_SetItem here states: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) Return value: New reference. Return element of o corresponding to the object key or NULL on failure. This is the…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
1 answer

What is the difference between PyBytes_Type and PyString_Type

It appears that Python 2 has PyString_Type whereas Python 3 has PyBytes_Type What is going on under the hood? I'm writing a C++ wrapper (currently wrapping Python3, but I hope to add Python2 support later), and I just got round to the task of…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
1 answer

Unicode-friendly architecture for bridging Python's String and Bytes types to C++

I'm writing a C++ Python wrapper. I am planning to have a generic Object class, class Object { private: PyObject* p; public: Object(int i) { /* construct PyLong */ } Object(double i) { /* construct PyFloat */ } : etc i.e. The…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
3 answers

How to break a direct reference cycle in CPython

In CPython I have two types of objects, which are close connected to each other. #include #include typedef struct pyt PYT; struct pyt { PyObject_HEAD PYT *other; }; static void dealloc (PYT *self) { …
tynn
  • 38,113
  • 8
  • 108
  • 143
1
vote
1 answer

Getting traceback from Python C API

I have a Python C API extension module which occassionally falls over with an uninformative "MemoryError". It's clearly not an error that's catered for by the module's exception handlers. How do I get a more informative error traceback so I can…
TheObserver
  • 2,973
  • 7
  • 33
  • 41
1
vote
1 answer

How to embed properly using Python for .NET

When I try to use PythonEngine.ImportModule(mymodulename) some of the optional modules in dependencies are attempted to be loaded (not required for module use without embedding). This results in return null from this method because some of these…
denfromufa
  • 5,610
  • 13
  • 81
  • 138
1
vote
1 answer

Setting different doc string for functions of the same custom type defined with Python-C-API

I'm embedding python in a C++ application and wrote a function wrapper (like boost::python does). To achieve this, I created a custom python type and defined its PyTypeObject structure and set a tp_call function pointer. This works fine, but now I…
neodelphi
  • 2,706
  • 1
  • 15
  • 22