python-c-api

fatal error: Python.h: No such file or directory

fatal error: Python.h: No such file or directory Question: I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below: gcc -Wall utilsmodule.c -o Utilc After executing the command, I get this error message: > utilsmodule.c:1:20: fatal error: Python.h: No …

Total answers: 35

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam) Question: Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are already many questions on stackoverflow regarding this topic, …

Total answers: 7

Python (and Python C API): __new__ versus __init__

Python (and Python C API): __new__ versus __init__ Question: The question I’m about to ask seems to be a duplicate of Python's use of __new__ and __init__?, but regardless, it’s still unclear to me exactly what the practical difference between __new__ and __init__ is. Before you rush to tell me that __new__ is for creating …

Total answers: 6

Py_INCREF/DECREF: When

Py_INCREF/DECREF: When Question: Is one correct in stating the following: If a Python object is created in a C function, but the function doesn’t return it, no INCREF is needed, but a DECREF is. [false]If the function does return it, you do need to INCREF, in the function that receives the return value.[/false] When assigning …

Total answers: 1

How To catch python stdout in c++ code

How To catch python stdout in c++ code Question: I have a program which during it’s run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function pythonCallBackFunc(const char* pythonInput) …

Total answers: 3

Create an object using Python's C API

Create an object using Python's C API Question: Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff… } pyfoo; …and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // … pyfoo_new, }; How do I create a new instance of pyfoo somewhere within my C extension? Asked By: …

Total answers: 1

Calling a python method from C/C++, and extracting its return value

Calling a python method from C/C++, and extracting its return value Question: I’d like to call a custom function that is defined in a Python module from C. I have some preliminary code to do that, but it just prints the output to stdout. mytest.py import math def myabs(x): return math.fabs(x) test.cpp #include <Python.h> int …

Total answers: 10

How to create a generator/iterator with the Python C API?

How to create a generator/iterator with the Python C API? Question: How do I replicate the following Python code with the Python C API? class Sequence(): def __init__(self, max): self.max = max def data(self): i = 0 while i < self.max: yield i i += 1 So far, I have this: #include <Python/Python.h> #include <Python/structmember.h> …

Total answers: 2