Skip to content

Commit 7ccefe1

Browse files
authored
PEP 757: reject more ideas (#4017)
* keep PyLong_GetNativeLayout() function (was: open question) * reject mpz_import/export-like API
1 parent 1490ed1 commit 7ccefe1

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

peps/pep-0757.rst

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,6 @@ There is no impact on the backward compatibility, only new APIs are
388388
added.
389389
390390
391-
Open Questions
392-
==============
393-
394-
* Should we add *digits_order* and *endianness* members to :data:`sys.int_info`
395-
and remove :c:func:`PyLong_GetNativeLayout()`? The
396-
:c:func:`PyLong_GetNativeLayout()` function returns a C structure
397-
which is more convenient to use in C than :data:`sys.int_info` which uses
398-
Python objects.
399-
* Currently, all required information for :class:`int` import/export is
400-
already available via :c:func:`PyLong_GetInfo()` or :data:`sys.int_info`.
401-
Native endianness of "digits" and current order of digits (least
402-
significant digit first) --- is a common denominator of all libraries
403-
for arbitrary precision integer arithmetic. So, shouldn't we just remove
404-
from API both :c:struct:`PyLongLayout` and :c:func:`PyLong_GetNativeLayout()` (which
405-
is actually just a minor convenience)?
406-
407-
408391
Rejected Ideas
409392
==============
410393
@@ -426,6 +409,46 @@ If later there are use cases for arbitrary layouts, new APIs can be
426409
added.
427410
428411
412+
Don't add :c:func:`PyLong_GetNativeLayout` function
413+
---------------------------------------------------
414+
415+
Currently, most required information for :class:`int` import/export is already
416+
available via :c:func:`PyLong_GetInfo()` (and :data:`sys.int_info`). We also
417+
can add more (like order of digits), this interface doesn't poses any
418+
constraints on future evolution of the :c:type:`PyLongObject`.
419+
420+
The problem is that the :c:func:`PyLong_GetInfo()` returns a Python object,
421+
:term:`named tuple`, not a convenient C structure and that might distract
422+
people from using it in favor e.g. of current semi-private macros like
423+
:c:macro:`!PyLong_SHIFT` and :c:macro:`!PyLong_BASE`.
424+
425+
426+
Provide mpz_import/export-like API instead
427+
------------------------------------------
428+
429+
The other approach to import/export data from :class:`int` objects might be
430+
following: expect, that C extensions provide contiguous buffers that CPython
431+
then exports (or imports) the *absolute* value of an integer.
432+
433+
API example::
434+
435+
struct PyLongLayout {
436+
uint8_t bits_per_digit;
437+
uint8_t digit_size;
438+
int8_t digits_order;
439+
};
440+
441+
size_t PyLong_GetDigitsNeeded(PyLongObject *obj, PyLongLayout layout);
442+
int PyLong_Export(PyLongObject *obj, PyLongLayout layout, void *buffer);
443+
PyLongObject *PyLong_Import(PyLongLayout layout, void *buffer);
444+
445+
This might work for the GMP, as this it has :c:func:`!mpz_limbs_read()` and
446+
:c:func:`!mpz_limbs_write()` functions, that can provide required "buffers".
447+
448+
The major drawback of this approach is that it's much more complex on the
449+
CPython side (i.e. actual conversion between different layouts).
450+
451+
429452
Discussions
430453
===========
431454

0 commit comments

Comments
 (0)