Skip to content

Commit 3e7393b

Browse files
authored
gh-159739: Propagate float rounding allocation failures (#160195)
1 parent cc36604 commit 3e7393b

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise :exc:`MemoryError` instead of returning an incorrect result if :func:`round` runs out of memory while rounding a float.

‎Objects/floatobject.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,16 @@ double_round(double x, int ndigits) {
942942
_Py_SET_53BIT_PRECISION_START;
943943
rounded = _Py_dg_strtod(mybuf, NULL);
944944
_Py_SET_53BIT_PRECISION_END;
945-
if (errno == ERANGE && fabs(rounded) >= 1.)
945+
if (errno == ENOMEM) {
946+
PyErr_NoMemory();
947+
}
948+
else if (errno == ERANGE && fabs(rounded) >= 1.) {
946949
PyErr_SetString(PyExc_OverflowError,
947950
"rounded value too large to represent");
948-
else
951+
}
952+
else {
949953
result = PyFloat_FromDouble(rounded);
954+
}
950955

951956
/* done computing value; now clean up */
952957
if (mybuf != shortbuf)

0 commit comments

Comments
 (0)