-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternatives to using private API for conversion to/from CPython int's #467
Comments
@casevh, unfortunately it seems we aren't going to have the public API on CPython side. See https://discuss.python.org/t/pep-757-c-api-to-import-export-python-integers/63895/66 Is it does make sense for you if the |
skirpichev
added a commit
to skirpichev/gmpy
that referenced
this issue
Dec 9, 2024
skirpichev
added a commit
to skirpichev/gmpy
that referenced
this issue
Dec 11, 2024
casevh
pushed a commit
that referenced
this issue
Dec 23, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently, we are using a bunch of private CPython functions to provide fast mpz <-> int conversion: see GMPy_MPZ_From_PyLong, GMPy_PyLong_From_MPZ and
gmpy/src/gmpy2_convert.h
Lines 135 to 155 in 141eb88
There should be a better way!
I see several variants to deal with this problem.
The CPython 3.13 got PyLong_AsNativeBytes() and PyLong_FromNativeBytes() function to import/export arbitrary int's. We can use these functions and handle small integers case separately with PyLong_AsLong()/PyLong_FromLong(). In general, this should be much slower than the current solution. On another hand, I think that for real world scenarios - small integers case should be most important.
There is a proposal to add
mpz_import/export
-like functions for PyLong's C API, but it seems that CPython core developers aren't very enthusiastic on this idea (see C API: Consider adding public PyLong_AsByteArray() and PyLong_FromByteArray() functions python/cpython#111140 (comment)).Another possibility is the
mpz_limbs_write()
-like API to access unsigned bigint as an array of "digits", see this. With that kind of API on the CPython side - the gmpy2 could do fast conversion to/from int's using mpz_import/export functions.I'm planning to open a discussion thread on the d.p.o, that will propose the 3rd option, but first I would appreciate any feedback here. CC @oscarbenjamin: perhaps this does make sense for the python-flint.
The text was updated successfully, but these errors were encountered: