python-c-api

A memory leak in a simple Python C-extension

A memory leak in a simple Python C-extension Question: I have some code similar to the one below. That code leaks, and I don’t know why. The thing that leaks is a simple creation of a Python class’ instance inside a C code. The function I use to check the leak is create_n_times that’s defined …

Total answers: 1

Python C API sys.getsizeof()

Python C API sys.getsizeof() Question: I’m trying to call a method getsizeof from a C library for Python. Here’s my code: //Returns in bytes the size of obj in memory //sys.getsizeof(obj) size_t getSizeOf(PyObject* obj){ //obj could be dict or list Py_INCREF(obj); PyObject* sys = PyModule_New("sys"); //import sys PyObject* string = Py_BuildValue("s", "getsizeof"); PyObject* res = …

Total answers: 1

Python C API, send a python function pointer to c and execute it

Python C API, send a python function pointer to c and execute it Question: I want to create a function in python, pass it’s function pointer to c and execute it there. So my python file: import ctypes import example def tester_print(): print("Hello") my_function_ptr = ctypes.CFUNCTYPE(None)(tester_print) example.pass_func(my_function_ptr) And here is what my function in c …

Total answers: 1

PyUnicode_FromStringAndSize: Very terse documentation

PyUnicode_FromStringAndSize: Very terse documentation Question: Apologies if this is a stupid question which I suspect it may well be. I’m a Python user with little experience in C. According to the official Python docs (v3.10.6): PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) Return value: New reference. Part of the Stable ABI. Create a Unicode object from …

Total answers: 1

PyArg_Parse for multiple returns of python on c++

PyArg_Parse for multiple returns of python on c++ Question: I am calling python from c++ using PyObject_CallObject as the python return only a floating point number, i can get it by: float output_of_python; PyObject *pValue,*pArgs; pValue = PyObject_CallObject(pFunc, pArgs); PyArg_Parse(pValue, "f", &output_of_python); however, if python’s returns 2 (return first,second), which format of PyArg_Parse(pValue… should i …

Total answers: 1

How to `pop` element from PyListObject?

How to `pop` element from PyListObject? Question: Lets say I have a PyListObject, and I want to append a PyObject. I can use PyList_Append API which is documented in the List Objects C-API. But for my use case I want to pop an element from the PyListObject(i.e. my_list.pop() in python layer). But the List Objects …

Total answers: 2

Python multi-thread multi-interpreter C API

Python multi-thread multi-interpreter C API Question: I’m playing around with the C API for Python, but it is quite difficult to understand some corner cases. I could test it, but it seems a bug-prone and time consuming. So I come here to see if somebody has already done this. The question is, which is the …

Total answers: 2

ImportError: dynamic module does not define init function (initfizzbuzz)

ImportError: dynamic module does not define init function (initfizzbuzz) Question: I tried to compile fizzbuzz.c, in order to import it by python. For building fizzbuzz.c,I used python setup.py build_ext -i. After building it, I tried to import fizzbuzz.c but the error below occurred. How can I solve this problem ? Error >>> import fizzbuzz Traceback …

Total answers: 6