https://github.com/python/cpython/commit/3e7393bdf5dae21784dacbb7fbc32f2cdbfe7707
commit: 3e7393bdf5dae21784dacbb7fbc32f2cdbfe7707
branch: main
author: Jelle Zijlstra <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-10-22T19:35:48Z
summary:
gh-159739: Propagate float rounding allocation failures (#160195)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst
M Objects/floatobject.c
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst
new file mode 100644
index 00000000000000..628a146b059a43
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst
@@ -0,0 +1 @@
+Raise :exc:`MemoryError` instead of returning an incorrect result if
:func:`round` runs out of memory while rounding a float.
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index d5a879cdd53ad6..c19ed6e6b60935 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -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)
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]