Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,8 @@ _PyLong_DigitCount(const PyLongObject *op)
#if PY_VERSION_HEX >= _Py_PACK_VERSION(3, 12)
return (Py_ssize_t)(op->long_value.lv_tag >> 3);
#else
return _PyLong_Sign((PyObject*)op) < 0 ? -Py_SIZE(op) : Py_SIZE(op);
Py_ssize_t size = Py_SIZE(op);
return Py_ABS(size);
#endif
}

Expand Down Expand Up @@ -2201,7 +2202,8 @@ PyLongWriter_Finish(PyLongWriter *writer)
assert(Py_REFCNT(obj) == 1);

// Normalize and get singleton if possible
while (i > 0 && _PyLong_GetDigits(self)[i-1] == 0) {
digit *digits = _PyLong_GetDigits(self);
while (i > 0 && digits[i-1] == 0) {
--i;
}
if (i != j) {
Expand All @@ -2211,7 +2213,7 @@ PyLongWriter_Finish(PyLongWriter *writer)
_PyLong_SetSignAndDigitCount(self, sign, i);
}
if (i <= 1) {
long val = sign * (long)(_PyLong_GetDigits(self)[0]);
long val = sign * (long)digits[0];
Py_DECREF(obj);
return PyLong_FromLong(val);
}
Expand Down
11 changes: 10 additions & 1 deletion tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,11 +1457,13 @@ test_long_import_export()
return -1;
}
assert(long_export.value == -123);
assert(long_export.negative == 0);
assert(long_export.ndigits == 0);
assert(long_export.digits == NULL);
PyLong_FreeExport(&long_export);
Py_DECREF(obj);

writer = PyLongWriter_Create(0, 5, (void **)&digits);
writer = PyLongWriter_Create(1, 5, (void **)&digits);
if (writer == NULL) {
return -1;
}
Expand All @@ -1475,10 +1477,17 @@ test_long_import_export()
return -1;
}

PyObject *str = PyObject_Str(obj);
assert(str != NULL);
assert(PyUnicode_EqualToUTF8(str, "-1329227995784915872903807060280344577") == 1);
Py_DECREF(str);

if (PyLong_Export(obj, &long_export) < 0) {
return -1;
}
assert(long_export.value == 0);
assert(long_export.negative == 1);
assert(long_export.ndigits == 5);
digits = (uint32_t*)long_export.digits;
assert(digits[0] == 1);
assert(digits[1] == 0);
Expand Down
Loading