jbrock...@gmail.com schrieb am 24.01.21 um 21:45:
> Looking at the standard library decimal module
> <
https://github.com/python/cpython/blob/3.9/Lib/decimal.py>, its (default)
> implementation
> <
https://github.com/python/cpython/blob/63298930fb531ba2bb4f23bc3b915dbf1e17e9e1/Modules/_decimal/_decimal.c>
> is in C. AFAICt there is no corresponding .h file. Still, it would be
> nice to be able to cimport from there for e.g. fast isinstance checks.
The decimal module's C-API that you were referring to was taken back out
before the release of CPython 3.10 in
https://bugs.python.org/issue43422
Thus, there is no "official" way to access the internals any more.
But if you can live with potential unanticipated breakage in the future,
you can choose to exploit the internals.
Write a, say, "cdecimal.pxd" file like this:
cdef extern from *:
"""
typedef struct {
PyObject_HEAD
/* remaining fields intentionally left out */
} PyDecObject;
/* copy more declarations from _decimal.c here */
"""
ctypedef class _decimal.Decimal [object PyDecObject,
check_size ignore]
and so on. Note that I left out the mpd_t etc. typed fields to avoid having
to compile against libmpdec. If you want to include them, you'll have to
add those declarations, too, or compile against the header files of the
library, which are not included in a CPython installation. That might
become a bit fiddly.
Anyway, the above .pxd file should give Cython and the C compiler
everything they need to make use of the _decimal module's internals that
you declared to them.
Note that the above is untested, but something like this should work.
Good luck! And if you can get this .pxd file to work, it's probably worth
its own package on PyPI.
Stefan