https://github.com/python/cpython/commit/bb8572f90adf19d2802bbec9383f244710ed4d4f
commit: bb8572f90adf19d2802bbec9383f244710ed4d4f
branch: 3.13
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-24T00:09:46+02:00
summary:

[3.13] gh-155526: Don't check errno in abs(complex) (GH-155527) (#157341) 
(#157558)

[3.15] gh-155526: Don't check errno in abs(complex) (GH-155527) (#157341)



(cherry picked from commit 340cfbaf679c1230fd438042d764b6922b483242)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: hpkfft.com <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst
M Lib/test/test_complex.py
M Modules/cmathmodule.c
M Objects/complexobject.c

diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index af4eba5451ba196..8ba120727657247 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -1,6 +1,8 @@
+import errno
 import unittest
 import sys
 from test import support
+from test.support import import_helper
 from test.support.testcase import ComplexesAreIdenticalMixin
 from test.support.numbers import (
     VALID_UNDERSCORE_LITERALS,
@@ -9,6 +11,7 @@
 
 from random import random
 from math import isnan, copysign
+import cmath
 import operator
 
 INF = float("inf")
@@ -613,8 +616,30 @@ def test_abs(self):
         for num in nums:
             self.assertAlmostEqual((num.real**2 + num.imag**2)  ** 0.5, 
abs(num))
 
+        for x in 0.0, -0.0, INF, -INF, NAN:
+            for y in 0.0, -0.0, INF, -INF, NAN:
+                with self.subTest(x=x, y=y):
+                    z = complex(x, y)
+                    r = abs(z)
+                    if cmath.isfinite(z):
+                        self.assertFloatsAreIdentical(r, 0.0)
+                    elif cmath.isinf(z):
+                        self.assertEqual(r, INF)
+                    else:
+                        self.assertTrue(cmath.isnan(z))
+                        self.assertTrue(isnan(r))
+
         self.assertRaises(OverflowError, abs, complex(DBL_MAX, DBL_MAX))
 
+    def test_abs_errno_handling(self):
+        _testcapi = import_helper.import_module('_testcapi')
+        z = complex('nan')
+        _testcapi.set_errno(errno.ERANGE)
+        try:
+            self.assertTrue(isnan(abs(z)))
+        finally:
+            _testcapi.set_errno(0)
+
     def test_repr_str(self):
         def test(v, expected, test_fn=self.assertEqual):
             test_fn(repr(v), expected)
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst
new file mode 100644
index 000000000000000..ae0c08973a8195b
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst
@@ -0,0 +1,3 @@
+Fix spurious :exc:`OverflowError` for ``abs(nanj)`` in case :c:data:`errno` was
+previously set to :c:macro:`!ERANGE` by some library call.
+Patch by Sergey B Kirpichev.
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index affb717f31b1100..538dc1c0e22425b 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -991,8 +991,8 @@ cmath_polar_impl(PyObject *module, Py_complex z)
 {
     double r, phi;
 
-    errno = 0;
     phi = c_atan2(z); /* should not cause any exception */
+    errno = 0;
     r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
     if (errno != 0)
         return math_error();
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 502b4a9a96119e7..a0685f0ee7de21c 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -589,6 +589,7 @@ complex_abs(PyComplexObject *v)
 {
     double result;
 
+    errno = 0;
     result = _Py_c_abs(v->cval);
 
     if (errno == ERANGE) {

_______________________________________________
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]

Reply via email to