Skip to content

Commit 4e1de16

Browse files
committed
Issue python#25402: in int-to-decimal-string conversion, reduce intermediate storage requirements and relax restriction on converting large integers. Patch by Serhiy Storchaka.
1 parent 583c6e8 commit 4e1de16

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 beta 1
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25402: In int-to-decimal-string conversion, improve the estimate
14+
of the intermediate memory required, and remove an unnecessarily strict
15+
overflow check. Patch by Serhiy Storchaka.
16+
1317
- Issue #27214: In long_invert, be more careful about modifying object
1418
returned by long_add, and remove an unnecessary check for small longs.
1519
Thanks Oren Milman for analysis and patch.

‎Objects/longobject.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,7 @@ long_to_decimal_string_internal(PyObject *aa,
15911591
Py_ssize_t size, strlen, size_a, i, j;
15921592
digit *pout, *pin, rem, tenpow;
15931593
int negative;
1594+
int d;
15941595
enum PyUnicode_Kind kind;
15951596

15961597
a = (PyLongObject *)aa;
@@ -1608,15 +1609,17 @@ long_to_decimal_string_internal(PyObject *aa,
16081609
16091610
But log2(a) < size_a * PyLong_SHIFT, and
16101611
log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
1611-
> 3 * _PyLong_DECIMAL_SHIFT
1612+
> 3.3 * _PyLong_DECIMAL_SHIFT
1613+
1614+
size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) =
1615+
size_a + size_a / d < size_a + size_a / floor(d),
1616+
where d = (3.3 * _PyLong_DECIMAL_SHIFT) /
1617+
(PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT)
16121618
*/
1613-
if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) {
1614-
PyErr_SetString(PyExc_OverflowError,
1615-
"int too large to format");
1616-
return -1;
1617-
}
1618-
/* the expression size_a * PyLong_SHIFT is now safe from overflow */
1619-
size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT);
1619+
d = (33 * _PyLong_DECIMAL_SHIFT) /
1620+
(10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT);
1621+
assert(size_a < PY_SSIZE_T_MAX/2);
1622+
size = 1 + size_a + size_a / d;
16201623
scratch = _PyLong_New(size);
16211624
if (scratch == NULL)
16221625
return -1;

0 commit comments

Comments
 (0)