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

Shall strings pointed by paramaters to PySys_SetArgvEx() be kept in memory until Py_Finalize()?

i have the following piece of code int nArgs; if (LPWSTR * const szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs)) { PySys_SetArgvEx(nArgs, szArglist, false); LocalFree(szArglist); } I cannot find in Python documentation if memory…
2
votes
1 answer

Python 3: Python/C API String Problems

I am trying to use the Python/C API to run a Python function which returns a string. I want to store that returned string to a C++ variable, but I can't get it to work. I am using Python 3 and it seems like the PyString_FromString() method doesn't…
MarcioPorto
  • 555
  • 7
  • 22
2
votes
1 answer

How to import from __future__ in C extension module

I've got an extension module that needs to support Python 2 and Python 3. When it loads, I want to do something like from __future__ import print_function so that if anyone tries to do this: PyRun_SimpleString("print 'foo'"); it will fail, but if…
Zachary Turner
  • 738
  • 4
  • 24
2
votes
1 answer

segfault trying to print object in IPython but not in Python

I get a segmentation fault when trying to display an object of a class defined in a C extension. In [1]: import moose on node 0, numNodes = 1, numCores = 2 In [2]: a = moose.Neutral('a') In [3]: print a
subhacom
  • 868
  • 10
  • 24
2
votes
5 answers

C program with embedded Python: How to restrict process to not open files nor sockets?

I like to forbid my C program certain rights, permissions or capabilities, e.g. to open any files (other than stdin, stdout, stderr) or any sockets, ideally even if run as root. The reason is, that the program embeds a Python interpreter and might…
user923543
2
votes
1 answer

Using a Python 2.7 enum from C

I have an enum in Python (backported enum package to 2.7) that is meant to be of only integers: import enum class MyEnum(enum.Enum): val = 0 Let's say I receive a PyObject * in a C extension pointing to MyEnum.val. I want the integer value…
user
  • 4,920
  • 3
  • 25
  • 38
2
votes
1 answer

How do I best parse a date string in the Python C API?

Unfortunately there seems to be no PyDateTime_FromString() (equivalent to e.g. PyFloat_FromString()) in the Python C API. Has anybody figured out what would be the ideal workaround if I have to parse a date string into a datetime.datetime type?
Thomas F.
  • 381
  • 2
  • 8
2
votes
1 answer

Seemingly nonsensical runtime increases when switching from pure C to C with Numpy objects

Introduction I am trying to realise some number crunching on a one-dimensional array in C (herafter: standalone) and as a Numpy module written in C (herafter: module) simultaneously. Since all I need to do with the array is to compare selected…
Wrzlprmft
  • 4,234
  • 1
  • 28
  • 54
2
votes
1 answer

Allocating a dynamic array in a dynamically allocated struct (struct of arrays)

This question is really about how to use variable-length types in the Python/C API (PyObject_NewVar, PyObject_VAR_HEAD, PyTypeObject.tp_basicsize and .tp_itemsize , but I can ask this question without bothering with the details of the API. Just…
wkschwartz
  • 3,817
  • 2
  • 29
  • 33
2
votes
1 answer

Appropriate syntax for Initialising Python C-API container types (List Dict Tuple Set) in C++

I'm designing a C++ Python-wrapper. I have an Object class to wrap a generic PyObject*, and I am providing constructors and conversion operators so that I can do things like: // C++ type -> PyObject (of appropriate PyFoo_Type) Object i{42}; //…
P i
  • 29,020
  • 36
  • 159
  • 267
2
votes
0 answers

Why is PyObject_SetAttrString returning -1 (Python C-API)

I am working on bridging C++ and Python. When a new instance of my custom type gets created, I need to register certain C++ instance methods as attributes on the Python object being created. The relevant code flow goes like this: // during setup, we…
P i
  • 29,020
  • 36
  • 159
  • 267
2
votes
1 answer

Compiling C extension with anaconda on Travis-CI missing __log_finite symbol

A C extension module that compiles fine on Travis-CI without anaconda fails when installed with anaconda. It appears to install just fine, but when I try to import it, I get the following error: ImportError:…
Mike
  • 19,114
  • 12
  • 59
  • 91
2
votes
1 answer

How to return an C array to Python?

I wrote a Python/C extension function that was called by Python, How can return an 2d array int[][] to Python? static PyObject* inference_function(PyObject *self, PyObject *args) { PyObject* doc_lst; int K,V; double alpha,beta; int…
lessisawesome
  • 1,313
  • 1
  • 12
  • 14
2
votes
1 answer

PyErr_SetString's string argument: is it borrowed?

How does PyErr_SetString handle the passed in c-string? e.g. is it safe to do: { int age = 12; std::stringstream ostr; ostr << "I'm " << age << " years old and what is this?"; PyErr_SetString(PyExc_RuntimeError,…
user
  • 4,920
  • 3
  • 25
  • 38
2
votes
2 answers

reading a global variable of python in c

I am trying to learn how to use the Python/C API correctly - all I actually need to do is to read a global variable (in my case dictionary - but I'm starting with a simple integer variable). Using the discussion: How to access a Python global…
et_l
  • 1,868
  • 17
  • 29