[Cython] Most efficient way to create an object with Python C-API
Daniele Nicolodi
daniele at grinta.net
Tue Jun 16 16:32:47 EDT 2020
Hello,
this is not a question directly related to cython, but I think the
readers of this list may have the right expertise to answer it.
I am working on a Python extension where I need to instantiate a lot of
Decimal objects. I would like to make it as efficient as possible.
Unfortunately the C-API of the _decimal module is not exposed (as is for
example done for the datetime module).
The straightforward approach is something on the lines of:
PyObject* decimal = PyImport_ImportModule("decimal");
PyObject* constructor = PyObject_GetAttrString(m, "Decimal");
and then instantiate the objects with something like:
PyObject* obj = PyObject_CallFunction(PyDec_Type, "s#", str, len);
Is there a better way?
I was thinking about getting to the PyTypeObject for Decimal, like
PyTypeObject* type = Py_TYPE(obj);
but then I don't see an easy way to use it to instantiate an object.
Am I missing something?
Thank you.
Cheers,
Dan
More information about the cython-devel
mailing list