From 38f103eef01321b4b4ad4496dee4f520d904e442 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 06:20:45 +0300 Subject: [PATCH 1/7] Document, that PyLongWriter_Create allocate memory for the caller --- peps/pep-0757.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index 2c5820e0fce..e04d1cb933d 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -177,7 +177,7 @@ create a Python :class:`int` object from a digits array. Create a :c:type:`PyLongWriter`. - On success, set *\*digits* and return a writer. + On success, allocate *\*digits* and return a writer. On error, set an exception and return ``NULL``. *negative* is ``1`` if the number is negative, or ``0`` otherwise. From 794209598e8aff0079a9d4af9c4429a253d5fafa Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 06:25:01 +0300 Subject: [PATCH 2/7] Document that PyLongWriter_Finish/Discard are mutually exclusive --- peps/pep-0757.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index e04d1cb933d..4439ea512d3 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -185,8 +185,8 @@ create a Python :class:`int` object from a digits array. *ndigits* is the number of digits in the *digits* array. It must be greater than or equal to 0. - The caller can either initialize the array of digits *digits* and then call - :c:func:`PyLongWriter_Finish` to get a Python :class:`int`, or call + The caller can either initialize the array of digits *digits* and then either call + :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or :c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must be in the range [``0``; ``(1 << sys.int_info.bits_per_digit) - 1``]. Unused digits must be set to ``0``. @@ -206,11 +206,15 @@ wrapper to the private :c:func:`!_PyLong_New()` function. The function takes care of normalizing the digits and converts the object to a compact integer if needed. + The writter instance is invalid after the call. + .. c:function:: void PyLongWriter_Discard(PyLongWriter *writer) Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`. + The writter instance is invalid after the call. + Optimize import for small integers ================================== From 3497305829866699ec4874f1eb4ca83fbb3a93bb Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 06:33:27 +0300 Subject: [PATCH 3/7] Use struct field instead of sys.int_info.bits_per_digit --- peps/pep-0757.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index 4439ea512d3..2acfa5ae31b 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -185,11 +185,12 @@ create a Python :class:`int` object from a digits array. *ndigits* is the number of digits in the *digits* array. It must be greater than or equal to 0. - The caller can either initialize the array of digits *digits* and then either call - :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or + The caller can either initialize the array of digits *digits* and then + either call :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or :c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must - be in the range [``0``; ``(1 << sys.int_info.bits_per_digit) - 1``]. Unused - digits must be set to ``0``. + be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the + :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit). + Unused digits must be set to ``0``. On CPython 3.14, the :c:func:`PyLongWriter_Create` implementation is a thin From ff879e1283eef507d2ae0b08a57986d3a7e23852 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 06:39:59 +0300 Subject: [PATCH 4/7] Document that only most significand digits must be zeroed. --- peps/pep-0757.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index 2acfa5ae31b..e68a3e8b203 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -190,7 +190,7 @@ create a Python :class:`int` object from a digits array. :c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit). - Unused digits must be set to ``0``. + Unused most significand digits must be set to ``0``. On CPython 3.14, the :c:func:`PyLongWriter_Create` implementation is a thin From 14cbb59d5d605523d9eaf3d55bce597af0864068 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 06:40:31 +0300 Subject: [PATCH 5/7] Require that ndigits > 0 --- peps/pep-0757.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index e68a3e8b203..43ae85f00d1 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -182,8 +182,8 @@ create a Python :class:`int` object from a digits array. *negative* is ``1`` if the number is negative, or ``0`` otherwise. - *ndigits* is the number of digits in the *digits* array. It must be - greater than or equal to 0. + *ndigits* is the number of digits in the *digits* array. It must be + greater than 0. The caller can either initialize the array of digits *digits* and then either call :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or From c0e939f9e394550112a25bceac2dd8bb3d539098 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 08:48:06 +0300 Subject: [PATCH 6/7] endianness -> digit_endianness --- peps/pep-0757.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index 43ae85f00d1..c37af860eb7 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -79,7 +79,7 @@ functions. - ``1`` for most significant digit first - ``-1`` for least significant digit first - .. c:member:: int8_t endianness + .. c:member:: int8_t digit_endianness Digit endianness: @@ -257,7 +257,7 @@ Code:: int int_digits_order = layout->digits_order; size_t int_bits_per_digit = layout->bits_per_digit; size_t int_nails = int_digit_size*8 - int_bits_per_digit; - int int_endianness = layout->endianness; + int int_endianness = layout->digit_endianness; Export: :c:func:`PyLong_Export()` with gmpy2 From f1137b3f6f5e2e1a5d34331bf919016bb79c9cdf Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Dec 2024 14:50:38 +0300 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- peps/pep-0757.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/peps/pep-0757.rst b/peps/pep-0757.rst index c37af860eb7..6b72ae3588b 100644 --- a/peps/pep-0757.rst +++ b/peps/pep-0757.rst @@ -190,7 +190,7 @@ create a Python :class:`int` object from a digits array. :c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit). - Unused most significand digits must be set to ``0``. + The unused most-significant digits must be set to ``0``. On CPython 3.14, the :c:func:`PyLongWriter_Create` implementation is a thin @@ -207,14 +207,14 @@ wrapper to the private :c:func:`!_PyLong_New()` function. The function takes care of normalizing the digits and converts the object to a compact integer if needed. - The writter instance is invalid after the call. + The writer instance is invalid after the call. .. c:function:: void PyLongWriter_Discard(PyLongWriter *writer) Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`. - The writter instance is invalid after the call. + The writer instance is invalid after the call. Optimize import for small integers