Skip to content

Commit 94d852e

Browse files
authored
Sync implementation with PEP (#9)
1 parent 36b87d4 commit 94d852e

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

Doc/c-api/long.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ Export API
685685
- ``1`` for most significant digit first
686686
- ``-1`` for least significant digit first
687687
688-
.. c:member:: int8_t endian
688+
.. c:member:: int8_t digit_endianness
689689
690690
Digit endianness:
691691
@@ -772,19 +772,20 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
772772
773773
Create a :c:type:`PyLongWriter`.
774774
775-
On success, set *\*digits* and return a writer.
775+
On success, allocate *\*digits* and return a writer.
776776
On error, set an exception and return ``NULL``.
777777
778778
*negative* is ``1`` if the number is negative, or ``0`` otherwise.
779779
780780
*ndigits* is the number of digits in the *digits* array. It must be
781-
greater than or equal to 0.
781+
greater than 0.
782782
783-
The caller can either initialize the array of digits *digits* and then call
784-
:c:func:`PyLongWriter_Finish` to get a Python :class:`int`, or call
783+
The caller can either initialize the array of digits *digits* and then
784+
either call :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or
785785
:c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must
786-
be in the range [``0``; ``(1 << sys.int_info.bits_per_digit) - 1``]. Unused
787-
digits must be set to ``0``.
786+
be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the
787+
:c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit).
788+
The unused most-significant digits must be set to ``0``.
788789
789790
790791
.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer)
@@ -797,7 +798,11 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
797798
The function takes care of normalizing the digits and converts the object
798799
to a compact integer if needed.
799800
801+
The writer instance is invalid after the call.
802+
800803
801804
.. c:function:: void PyLongWriter_Discard(PyLongWriter *writer)
802805
803806
Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`.
807+
808+
The writer instance is invalid after the call.

Include/cpython/longintrepr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ typedef struct PyLongLayout {
145145
uint8_t bits_per_digit;
146146
uint8_t digit_size;
147147
int8_t digits_order;
148-
int8_t endianness;
148+
int8_t digit_endianness;
149149
} PyLongLayout;
150150

151151
PyAPI_FUNC(const PyLongLayout*) PyLong_GetNativeLayout(void);

Lib/test/test_capi/test_long.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def test_long_layout(self):
723723
'bits_per_digit': int_info.bits_per_digit,
724724
'digit_size': int_info.sizeof_digit,
725725
'digits_order': -1,
726-
'endianness': -1 if sys.byteorder == 'little' else 1,
726+
'digit_endianness': -1 if sys.byteorder == 'little' else 1,
727727
}
728728
self.assertEqual(layout, expected)
729729

@@ -756,7 +756,8 @@ def test_longwriter_create(self):
756756
base = 2 ** layout['bits_per_digit']
757757

758758
pylongwriter_create = _testcapi.pylongwriter_create
759-
self.assertEqual(pylongwriter_create(0, []), 0)
759+
self.assertRaises(ValueError, pylongwriter_create, 0, [])
760+
self.assertRaises(ValueError, pylongwriter_create, -123, [])
760761
self.assertEqual(pylongwriter_create(0, [0]), 0)
761762
self.assertEqual(pylongwriter_create(0, [123]), 123)
762763
self.assertEqual(pylongwriter_create(1, [123]), -123)

Modules/_testcapi/long.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ layout_to_dict(const PyLongLayout *layout)
180180
goto error;
181181
}
182182

183-
value = PyLong_FromLong(layout->endianness);
183+
value = PyLong_FromLong(layout->digit_endianness);
184184
if (value == NULL) {
185185
goto error;
186186
}
187-
res = PyDict_SetItemString(dict, "endianness", value);
187+
res = PyDict_SetItemString(dict, "digit_endianness", value);
188188
Py_DECREF(value);
189189
if (res < 0) {
190190
goto error;
@@ -215,7 +215,7 @@ pylong_export(PyObject *module, PyObject *obj)
215215
const digit *export_long_digits = export_long.digits;
216216

217217
PyObject *digits = PyList_New(0);
218-
for (Py_ssize_t i=0; i < export_long.ndigits; i++) {
218+
for (Py_ssize_t i = 0; i < export_long.ndigits; i++) {
219219
PyObject *item = PyLong_FromUnsignedLong(export_long_digits[i]);
220220
if (item == NULL) {
221221
goto error;
@@ -258,7 +258,7 @@ pylongwriter_create(PyObject *module, PyObject *args)
258258
return PyErr_NoMemory();
259259
}
260260

261-
for (Py_ssize_t i=0; i < ndigits; i++) {
261+
for (Py_ssize_t i = 0; i < ndigits; i++) {
262262
PyObject *item = PyList_GET_ITEM(list, i);
263263

264264
long num = PyLong_AsLong(item);

Objects/longobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6781,7 +6781,7 @@ int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
67816781
static const PyLongLayout PyLong_LAYOUT = {
67826782
.bits_per_digit = PyLong_SHIFT,
67836783
.digits_order = -1, // least significant first
6784-
.endianness = PY_LITTLE_ENDIAN ? -1 : 1,
6784+
.digit_endianness = PY_LITTLE_ENDIAN ? -1 : 1,
67856785
.digit_size = sizeof(digit),
67866786
};
67876787

@@ -6851,7 +6851,7 @@ PyLong_FreeExport(PyLongExport *export_long)
68516851
PyLongWriter*
68526852
PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
68536853
{
6854-
if (ndigits < 0) {
6854+
if (ndigits <= 0) {
68556855
PyErr_SetString(PyExc_ValueError, "ndigits must be positive");
68566856
return NULL;
68576857
}
@@ -6861,9 +6861,6 @@ PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
68616861
if (obj == NULL) {
68626862
return NULL;
68636863
}
6864-
if (ndigits == 0) {
6865-
assert(obj->long_value.ob_digit[0] == 0);
6866-
}
68676864
if (negative) {
68686865
_PyLong_FlipSign(obj);
68696866
}

0 commit comments

Comments
 (0)