All Questions
Tagged with python-c-api cpython
77 questions
0
votes
1
answer
62
views
Can a simple mutex pthread_mutex be a replacement for GIL in a multithreaded Python C extension?
Can a simple mutex pthread_mutex be a replacement for GIL in a multithreaded Python C extension? For eg: if I put mutex locks around Py_INCREF or PyMem_Alloc, would that suffice?
Tried using the GIL, ...
1
vote
1
answer
113
views
Create instance of a imported python class with C API
I want to create an instance of a Python class with the C API.
My specific use case is the PySerial Object. My Python code would be:
import serial
test = serial.Serial()
I couldn't find any clue or ...
0
votes
0
answers
55
views
Creating spamtuple instead of spamlist from CPython's xxsubtype.c example template
While in the process of learning the C API for CPython I wanted to modify the xxsubtype.c template example that contains spamlist and spamdict to create spamtuple. The does not work directly, I ...
3
votes
1
answer
86
views
CPython: Usage of `tp_finalize` in C-defined static types with no custom `tp_dealloc`
PEP 442 introduced the tp_finalize callback to Python type definitions (as a one-to-one equivalent of Pythons classes' __del__ function), and recommends using this for any non-trivial destruction.
The ...
0
votes
1
answer
70
views
Create a PyObject with a pointer to a C function
Let's say I have this Python function:
def foo(bar):
bar()
And this C function
void bar() {
}
How can I create a PyObject with Python C API that holds a pointer to bar function so I can call the ...
2
votes
1
answer
70
views
Why do attributes that are being set by a custom type initializer need to be protected?
The CPython Tutorial defines a custom initializer for a custom type which has the following lines :
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
...
0
votes
0
answers
126
views
Serialization of PyObjects in Cpython
I am trying to write a PyObject in a file(in bytes) and then access it via other instance of Python interpretor. Although my main goal is to somehow have some PyObjects in a shared memory so that ...
1
vote
1
answer
395
views
Cpython Extension forcing Memory Leak
I am trying to leak memory from my C-extension forcefully.
The code for the below is
Pyobect* myfunc(PyObject* self, PyObject* args)
{
static int i = 213123;
PyObject* temp = PyLong_FromLong(i)...
2
votes
2
answers
1k
views
Raising exception in init causes SystemError: returned a result with an error set in Python C API
I am using pytest to test my own Python C extension module.
I am trying to check if the TypeError occurs properly when an argument of invalid type is input to the __init__ method.
The method ...
3
votes
1
answer
771
views
How to solve Python-C-API error "This is an issue with the package mentioned above, not pip."?
I'm trying to implement an algorithm in the form of the C programming language into my system that runs using the python programming language. I'm trying to implement the Python C API with the ...
2
votes
0
answers
218
views
Some builtins modules crash python if the interpreter is embedded in a shared library
I'm on MacOs and I'm trying to embed python inside a c shared library. Disclaimer: there are two installations of python involved with this question. One is my "main" python, so to speak, ...
2
votes
2
answers
299
views
In the Python C API, if the second argument is not set how can I default to the value of the first?
I have an x, y parameters. Using the python C API, how can I default y to the value of x if y is None.
The python equivalent is:
def scale(x: float, y:Optional[float]=None):
if y is None:
...
0
votes
1
answer
456
views
Python C API: What does PyList_Append do on error?
This is not documented in the docs. It just says it "returns 0 for success and -1 for error". What does this mean?
If we're iterating over some data structure (say a linked list/array) and ...
7
votes
2
answers
372
views
Can you safely change a Python object's type in a C extension?
Question
Suppose that I have implemented two Python types using the C extension API and that the types are identical (same data layouts/C struct) with the exception of their names and a few methods. ...
2
votes
1
answer
629
views
What's the performance of PyImport_ImportModule(), and what are the best practices for using it?
I have an inner loop C function which (in my case) constructs a Python datetime object:
PyObject* my_inner_loop_fn(void* some_data) {
PyObject* datetime = PyImport_ImportModule("datetime"...