gh-159739: Propagate float rounding allocation failures (#160195)

This commit is contained in:
Jelle Zijlstra authored and GitHub committed 2026-10-22 19:35:48 +00:00
1 parent cc36604f7b
commit 3e7393bdf5
2 files changed
+8 -2

No files matched your search

@@ -0,0 +1 @@
Raise :exc:`MemoryError` instead of returning an incorrect result if :func:`round` runs out of memory while rounding a float.
+7 -2
View File
@@ -942,11 +942,16 @@ double_round(double x, int ndigits) {
_Py_SET_53BIT_PRECISION_START;
rounded = _Py_dg_strtod(mybuf, NULL);
_Py_SET_53BIT_PRECISION_END;
if (errno == ERANGE && fabs(rounded) >= 1.)
if (errno == ENOMEM) {
PyErr_NoMemory();
}
else if (errno == ERANGE && fabs(rounded) >= 1.) {
PyErr_SetString(PyExc_OverflowError,
"rounded value too large to represent");
else
}
else {
result = PyFloat_FromDouble(rounded);
}
/* done computing value; now clean up */
if (mybuf != shortbuf)