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

AttributeError: object ... has no attribute

I have this in my fb.h file: static fbClass *getInstance(); void clearFBblit(); int getFBdiff(int ret); void setFBdiff(int top, int right, int left, int bottom); and myfb.cpp content: void fbClass::clearFBblit() { //set real frambuffer…
0
votes
1 answer

Return string from python to C++

My python code has methods with returning String. import urllib.request import ssl import suds.transport.http from suds.client import Client class UnverifiedHttpsTransport(suds.transport.http.HttpTransport): def __init__(self, *args, **kwargs): …
batuman
  • 7,066
  • 26
  • 107
  • 229
0
votes
0 answers

Python C API: Include "if __name__ == '__main__':" call?

I am on Windows and am using the Python 3 C API to import a library that uses the Python multiprocessing library. Per the docs, on Windows I need put a main guard in place, like if __name__ == '__main__': rest of code making calls into classes…
0
votes
1 answer

How do I expose a C variable to Python as a variable?

Suppose in a custom python module written in C, I declare the variable: static int module_state; The functions in my module set the state of this variable. If I want to know the value of module_state, I can declare a getter function like…
gny-001f2
  • 29
  • 5
0
votes
1 answer

Python/C API project - compile to exe

I'm working on project where I am using Python/C API and C++. C++ is used for most part of application, mainly for gui (wxWidgets), while Python is used for calculations on large data sets, e.g. from files. I'm doing it in Visual Studio and when I…
0
votes
0 answers

Python C++ module causing a core dump when accessing the same PyObject* attribute more than once

So I've finally managed to import the C module into python. However, I'm getting a core dump when I try to access the attribute of an object more than once. Here's the code: node.h: #pragma once #include #include…
0
votes
0 answers

dynamic module does not define init function error when importing .so file compiled with boost python (Python 3.7)

This might have already been posted somewhere else but I couldn't find anything that worked. I have the following libraries / tools installed : boost: boost-python36.x86_64 boost-python36-devel.x86_64 boost-python36-static.x86_64 Python…
qwerty_99
  • 640
  • 5
  • 20
0
votes
2 answers

Is it possible to assign an C++ pointer to an attribute of a Python object, and retrieve it as a C++ pointer in another C++ function?

I'm making a C++ extension for Python, and I'm trying to do something like: // this function assigns a C++ pointer to as attribute of a python object void function1(PyObject* p){ // equivalent of p.attr = cpp_attr; MyClass* cpp_attr = new…
qwerty_99
  • 640
  • 5
  • 20
0
votes
0 answers

Python c-api, cant reuse NUMPY

I use the initialization of the following type: if (!Py_IsInitialized()) { Py_Initialize(); qInfo() << "INIT LIBs"; PyRun_SimpleString("import sys"); PyRun_SimpleString("import os"); PyRun_SimpleString("import…
Paul.S
  • 1
  • 2
0
votes
1 answer

Returning numpy array in c extension causes Segmentation fault: 11

I'm trying to write a c extension for python to speed up some number crunching I'm doing in my project with out having to port the whole thing to C. Unfortunately when I try to return numpy arrays by using my extension function in python it causes…
Peter Clark
  • 161
  • 1
  • 5
0
votes
1 answer

How to properly call PyIter_Next over a List or Iterable in Python C API?

I've been struggling to have some generic method of iterating over a sequence or iterable in the Cpython C API. The example code below compiles without errors, but fails when run as follows: it ok Segmentation fault: 11 I'm speculating that it's…
shakfu
  • 41
  • 1
  • 5
0
votes
0 answers

How to implement ufunc 'matmul' using Numpy-C API?

I am trying to implement a custom datatype with built-in support for numpy using Numpy-C API. My code is based on the quaterion implementation by https://github.com/moble/quaternion. Currently that project has supported functions like 'add',…
0
votes
0 answers

Python C Extensions, Visual Studio 2019 Gives Errors (nullptr, python native dev tools not installed)

I'm using visual studio 2019 and python 3.8. I'm trying to make a C extension for Python, and I was trying to figure things out and found this tutorial…
Romero
  • 1
  • 1
0
votes
1 answer

PyBytes_FromString different endianness

I have a python-wrapped C++ object whose underlying data is a container std::vector that represents bits. I have a function that writes these bits to a PyBytes object. If the endianness is the same, then there is no issue. However if I wish to…
Throckmorton
  • 564
  • 4
  • 17
0
votes
0 answers

Reference counting on PyErr_Fetch/PyErr_NormalizeException results

PyErr_Fetch doesn't always return a triplet where the second argument is a PyException so, to ease the parsing, you might want to use PyErr_NormalizeException to sort that part out (see the documentation) What the documentation doesn't say, however,…
Rick77
  • 3,121
  • 25
  • 43