10000 Address Bénédikt's review · python/cpython@eaebef3 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaebef3

Browse files
committed
Address Bénédikt's review
1 parent b08cd55 commit eaebef3

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

Doc/c-api/long.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
657657
Export API
658658
^^^^^^^^^^
659659
660-
.. versionadded:: 3.14
660+
.. versionadded:: next
661661
662662
.. c:struct:: PyLongLayout
663663
@@ -692,7 +692,7 @@ Export API
692692
Digit endianness:
693693
694694
- ``1`` for most significant byte first (big endian)
695-
- ``-1`` for least significant first (little endian)
695+
- ``-1`` for least significant byte first (little endian)
696696
697697
698698
.. c:function:: const PyLongLayout* PyLong_GetNativeLayout(void)
@@ -743,6 +743,8 @@ Export API
743743
744744
Export a Python :class:`int` object.
745745
746+
*export_long* must not be ``NULL``.
747+
746748
On success, set *\*export_long* and return ``0``.
747749
On error, set an exception and return ``-1``.
748750
@@ -760,7 +762,7 @@ PyLongWriter API
760762
761763
The :c:type:`PyLongWriter` API can be used to import an integer.
762764
763-
.. versionadded:: 3.14
765+
.. versionadded:: next
764766
765767
.. c:struct:: PyLongWriter
766768
@@ -789,7 +791,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
789791
:c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must
790792
be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the
791793
:c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit).
792-
The unused most-significant digits must be set to ``0``.
794+
The unused most significant digits must be set to ``0``.
793795
794796
795797
.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer)
@@ -809,4 +811,6 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
809811
810812
Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`.
811813
814+
*writer* must not be ``NULL``.
815+
812816
The writer instance is invalid after the call.

Lib/test/test_capi/test_long.py

Lines changed: 3 additions & 0 deletions
Origina 8000 l file line numberDiff line numberDiff line change
@@ -738,12 +738,15 @@ def test_long_export(self):
738738
self.assertEqual(pylong_export(0), 0)
739739
self.assertEqual(pylong_export(123), 123)
740740
self.assertEqual(pylong_export(-123), -123)
741+
self.assertEqual(pylong_export(IntSubclass(123)), 123)
741742

742743
# use an array, doesn't fit into int64_t
743744
self.assertEqual(pylong_export(base**10 * 2 + 1),
744745
(0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
745746
self.assertEqual(pylong_export(-(base**10 * 2 + 1)),
746747
(1, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
748+
self.assertEqual(pylong_export(IntSubclass(base**10 * 2 + 1)),
749+
(0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
747750

748751
for value in (1.0, 0+1j, "abc"):
749752
with self.subTest(value=value):

Modules/_testcapi/long.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ pylong_export(PyObject *module, PyObject *obj)
193193
const digit *export_long_digits = export_long.digits;
194194

195195
PyObject *digits = PyList_New(0);
196+
if (digits == NULL) {
197+
goto error;
198+
}
196199
for (Py_ssize_t i = 0; i < export_long.ndigits; i++) {
197200
PyObject *item = PyLong_FromUnsignedLong(export_long_digits[i]);
198201
if (item == NULL) {
@@ -214,7 +217,7 @@ pylong_export(PyObject *module, PyObject *obj)
214217
return res;
215218

216219
error:
217-
Py_DECREF(digits);
220+
Py_XDECREF(digits);
218221
PyLong_FreeExport(&export_long);
219222
return NULL;
220223
}
@@ -225,8 +228,7 @@ pylongwriter_create(PyObject *module, PyObject *args)
225228
{
226229
int negative;
227230
PyObject *list;
228-
if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list))
229-
{
231+
if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list)) {
230232
return NULL;
231233
}
232234
Py_ssize_t ndigits = PyList_GET_SIZE(list);

Objects/longobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6853,20 +6853,24 @@ PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
68536853
{
68546854
if (ndigits <= 0) {
68556855
PyErr_SetString(PyExc_ValueError, "ndigits must be positive");
6856-
return NULL;
6856+
goto error;
68576857
}
68586858
assert(digits != NULL);
68596859

68606860
PyLongObject *obj = _PyLong_New(ndigits);
68616861
if (obj == NULL) {
6862-
return NULL;
6862+
goto error;
68636863
}
68646864
if (negative) {
68656865
_PyLong_FlipSign(obj);
68666866
}
68676867

68686868
*digits = obj->long_value.ob_digit;
68696869
return (PyLongWriter*)obj;
6870+
6871+
error:
6872+
*digits = NULL;
6873+
return NULL;
68706874
}
68716875

68726876

0 commit comments

Comments
 (0)
0