Skip to content

Commit 92007d1

Browse files
committed
Address Petr's review
1 parent 45517ab commit 92007d1

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

Doc/c-api/long.rst

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ Export API
703703
704704
The function must not be called before Python initialization nor after
705705
Python finalization. The returned layout is valid until Python is
706-
finalized. The layout is the same for all Python sub-interpreters and
707-
so it can be cached.
706+
finalized. The layout is the same for all Python sub-interpreters
707+
in a process, and so it can be cached.
708708
709709
710710
.. c:struct:: PyLongExport
@@ -714,10 +714,8 @@ Export API
714714
There are two cases:
715715
716716
* If :c:member:`digits` is ``NULL``, only use the :c:member:`value` member.
717-
Calling :c:func:`PyLong_FreeExport` is optional in this case.
718717
* If :c:member:`digits` is not ``NULL``, use :c:member:`negative`,
719718
:c:member:`ndigits` and :c:member:`digits` members.
720-
Calling :c:func:`PyLong_FreeExport` is mandatory in this case.
721719
722720
.. c:member:: int64_t value
723721
@@ -743,20 +741,28 @@ Export API
743741
744742
Export a Python :class:`int` object.
745743
746-
*export_long* must not be ``NULL``.
744+
*export_long* must point to a :c:struct:`PyLongExport` structure allocated
745+
by the caller. It must not be ``NULL``.
747746
748-
On success, set *\*export_long* and return ``0``.
747+
On success, fill in *\*export_long* and return ``0``.
749748
On error, set an exception and return ``-1``.
750749
751-
If *export_long->digits* is not ``NULL``, :c:func:`PyLong_FreeExport` must
752-
be called when the export is no longer needed. Otherwise, calling
753-
:c:func:`PyLong_FreeExport` is optional.
750+
:c:func:`PyLong_FreeExport` must be called when the export is no longer
751+
needed.
752+
753+
.. impl-detail::
754+
This function always succeeds if *obj* is a Python :class:`int` object
755+
or a subclass.
754756
755757
756758
.. c:function:: void PyLong_FreeExport(PyLongExport *export_long)
757759
758760
Release the export *export_long* created by :c:func:`PyLong_Export`.
759761
762+
.. impl-detail::
763+
Calling :c:func:`PyLong_FreeExport` is optional if *export_long->digits*
764+
is ``NULL``.
765+
760766
761767
PyLongWriter API
762768
^^^^^^^^^^^^^^^^
@@ -787,12 +793,18 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
787793
788794
*digits* must not be NULL.
789795
790-
The caller can either initialize the array of digits *digits* and then
791-
either call :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or
792-
:c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must
793-
be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the
794-
:c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit).
795-
The unused most significant digits must be set to ``0``.
796+
After a successful call to this function, the caller should fill in the
797+
array of digits *digits* and then call :c:func:`PyLongWriter_Finish` to get
798+
a Python :class:`int`.
799+
The layout of *digits* is described by :c:func:`PyLong_GetNativeLayout`.
800+
801+
Digits must be in the range [``0``; ``(1 << bits_per_digit) - 1``]
802+
(where the :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits
803+
per digit).
804+
Any unused most significant digits must be set to ``0``.
805+
806+
Alternately, call :c:func:`PyLongWriter_Discard` to destroy the writer
807+
instance without creating an :class:`~int` object.
796808
797809
798810
.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer)
@@ -805,7 +817,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
805817
The function takes care of normalizing the digits and converts the object
806818
to a compact integer if needed.
807819
808-
The writer instance is invalid after the call.
820+
The writer instance and the *digits* array are invalid after the call.
809821
810822
811823
.. c:function:: void PyLongWriter_Discard(PyLongWriter *writer)
@@ -814,4 +826,4 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
814826
815827
*writer* must not be ``NULL``.
816828
817-
The writer instance is invalid after the call.
829+
The writer instance and the *digits* array are invalid after the call.

Doc/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
('c:type', 'size_t'),
159159
('c:type', 'ssize_t'),
160160
('c:type', 'time_t'),
161-
('c:type', 'int8_t'),
162161
('c:type', 'uint8_t'),
163162
('c:type', 'uint16_t'),
164163
('c:type', 'uint32_t'),

Modules/_testcapi/long.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ pylong_export(PyObject *module, PyObject *obj)
164164
assert(export_long.negative == 0);
165165
assert(export_long.ndigits == 0);
166166
assert(export_long.digits == NULL);
167-
return PyLong_FromInt64(export_long.value);
168-
// PyLong_FreeExport() is not needed in this case
167+
PyObject *res = PyLong_FromInt64(export_long.value);
168+
PyLong_FreeExport(&export_long);
169+
return res;
169170
}
170171

171172
assert(PyLong_GetNativeLayout()->digit_size == sizeof(digit));

Objects/longobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6797,6 +6797,7 @@ int
67976797
PyLong_Export(PyObject *obj, PyLongExport *export_long)
67986798
{
67996799
if (!PyLong_Check(obj)) {
6800+
memset(export_long, 0, sizeof(*export_long));
68006801
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
68016802
return -1;
68026803
}

0 commit comments

Comments
 (0)