From 5aa9e730ecb921fe2dce8179f7cff82dec773977 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 11 Aug 2026 06:05:05 +0300 Subject: [PATCH 1/6] gh-155526: correct errno handling in complex_abs() --- Lib/test/test_complex.py | 7 +++++++ .../2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst | 2 ++ Objects/complexobject.c | 14 +++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index bb307191dffcc14..1862288e32e6107 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -1,6 +1,7 @@ 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, @@ -791,6 +792,12 @@ def test_abs(self): 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(34) + self.assertTrue(isnan(abs(z))) + 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..624cd2c19744b33 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst @@ -0,0 +1,2 @@ +Correct ``errno`` handling in ``abs(complex)``. Patch by Sergey B +Kirpichev. diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 3612c2699a557db..c3c4e6abbc890cc 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -367,7 +367,7 @@ c_powi(Py_complex x, long n) double _Py_c_abs(Py_complex z) { - /* sets errno = ERANGE on overflow; otherwise errno = 0 */ + /* sets errno = ERANGE on overflow */ double result; if (!isfinite(z.real) || !isfinite(z.imag)) { @@ -376,12 +376,10 @@ _Py_c_abs(Py_complex z) NaN. */ if (isinf(z.real)) { result = fabs(z.real); - errno = 0; return result; } if (isinf(z.imag)) { result = fabs(z.imag); - errno = 0; return result; } /* either the real or imaginary part is a NaN, @@ -389,10 +387,9 @@ _Py_c_abs(Py_complex z) return Py_NAN; } result = hypot(z.real, z.imag); - if (!isfinite(result)) + if (!isfinite(result)) { errno = ERANGE; - else - errno = 0; + } return result; } @@ -796,7 +793,10 @@ static PyObject * complex_abs(PyObject *op) { PyComplexObject *v = _PyComplexObject_CAST(op); - double result = _Py_c_abs(v->cval); + double result; + + errno = 0; + result = _Py_c_abs(v->cval); if (errno == ERANGE) { PyErr_SetString(PyExc_OverflowError, "absolute value too large"); From 576f6cd356483914dd8c17f1a6b10dfa3d79faa6 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 11 Aug 2026 10:55:37 +0300 Subject: [PATCH 2/6] address review: restore errno + readability --- Lib/test/test_complex.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 1862288e32e6107..b08488ff14358a1 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -1,3 +1,4 @@ +import errno import unittest import sys from test import support @@ -795,8 +796,11 @@ def test_abs(self): def test_abs_errno_handling(self): _testcapi = import_helper.import_module('_testcapi') z = complex('nan') - _testcapi.set_errno(34) - self.assertTrue(isnan(abs(z))) + _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): From 3b851ac2d9bfa286ee5ac6ebb1cd4ec857f3baa5 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 27 Aug 2026 06:25:02 +0300 Subject: [PATCH 3/6] + test special components for complex_abs() --- Lib/test/test_complex.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index b08488ff14358a1..1916260465c1641 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -11,6 +11,7 @@ from random import random from math import isnan, copysign +import cmath import operator INF = float("inf") @@ -791,6 +792,19 @@ 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): From fde8fc7668c6466aa5c52b31c70c6de9720ace4e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 31 Aug 2026 08:59:16 +0300 Subject: [PATCH 4/6] + don't use errno in complex_abs() --- Objects/complexobject.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Objects/complexobject.c b/Objects/complexobject.c index c3c4e6abbc890cc..6301106d3093bbb 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -795,9 +795,11 @@ complex_abs(PyObject *op) PyComplexObject *v = _PyComplexObject_CAST(op); double result; - errno = 0; - result = _Py_c_abs(v->cval); - if (errno == ERANGE) { + result = hypot(v->cval.real, v->cval.imag); + /* Testing FE_OVERFLOW floating-point exception is slow. */ + if (isfinite(v->cval.real) && isfinite(v->cval.imag) + && !isfinite(result)) + { PyErr_SetString(PyExc_OverflowError, "absolute value too large"); return NULL; From 5fa214f8f93bdfbff9349afc3df9f1ff33300a37 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 5 Sep 2026 19:23:38 +0300 Subject: [PATCH 5/6] ignore errno from atan2 in cmath_polar_impl() --- Modules/cmathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index f6e1475b00ecfbb..e756b550e7753e1 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -1029,8 +1029,8 @@ cmath_polar_impl(PyObject *module, Py_complex z) { double r, phi; - errno = 0; phi = atan2(z.imag, z.real); /* should not cause any exception */ + errno = 0; r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */ if (errno != 0) return math_error(); From 28ba0748c99c825ecd1cb8c16adc0b65d5a4a550 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 6 Sep 2026 04:39:11 +0300 Subject: [PATCH 6/6] restore errno on success --- Lib/test/test_capi/test_complex.py | 39 +++++++++++++++++++++--------- Modules/_testcapi/complex.c | 1 - Objects/complexobject.c | 17 +++++++------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_capi/test_complex.py b/Lib/test/test_capi/test_complex.py index c3189a67cc7e2d3..7d64237acd38864 100644 --- a/Lib/test/test_capi/test_complex.py +++ b/Lib/test/test_capi/test_complex.py @@ -281,18 +281,33 @@ def test_py_c_abs(self): # Test _Py_c_abs() _py_c_abs = _testcapi._py_c_abs - self.assertEqual(_py_c_abs(-1), (1.0, 0)) - self.assertEqual(_py_c_abs(1j), (1.0, 0)) - - self.assertEqual(_py_c_abs(complex('+inf+1j')), (INF, 0)) - self.assertEqual(_py_c_abs(complex('-inf+1j')), (INF, 0)) - self.assertEqual(_py_c_abs(complex('1.25+infj')), (INF, 0)) - self.assertEqual(_py_c_abs(complex('1.25-infj')), (INF, 0)) - - self.assertTrue(isnan(_py_c_abs(complex('1.25+nanj'))[0])) - self.assertTrue(isnan(_py_c_abs(complex('nan-1j'))[0])) - - self.assertEqual(_py_c_abs(complex(*[DBL_MAX]*2))[1], errno.ERANGE) + try: + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(-1), (1.0, 0)) + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(1j), (1.0, 0)) + + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(complex('+inf+1j')), (INF, 0)) + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(complex('-inf+1j')), (INF, 0)) + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(complex('1.25+infj')), (INF, 0)) + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(complex('1.25-infj')), (INF, 0)) + + _testcapi.set_errno(0) + self.assertTrue(isnan(_py_c_abs(complex('1.25+nanj'))[0])) + _testcapi.set_errno(0) + self.assertTrue(isnan(_py_c_abs(complex('nan-1j'))[0])) + + _testcapi.set_errno(0) + self.assertEqual(_py_c_abs(complex(*[DBL_MAX]*2))[1], errno.ERANGE) + + _testcapi.set_errno(errno.EACCES) # preserve errno + self.assertEqual(_py_c_abs(1j), (1, errno.EACCES)) + finally: + _testcapi.set_errno(0) if __name__ == "__main__": diff --git a/Modules/_testcapi/complex.c b/Modules/_testcapi/complex.c index fb5234d03cf0676..f1bbeb4804a1918 100644 --- a/Modules/_testcapi/complex.c +++ b/Modules/_testcapi/complex.c @@ -76,7 +76,6 @@ _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj) return NULL; } - errno = 0; res = _Py_c_abs(complex); return Py_BuildValue("di", res, errno); } diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 1494e5afefa867d..4d2b5dc8e4613f3 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -381,6 +381,7 @@ _Py_c_abs(Py_complex z) { /* sets errno = ERANGE on overflow */ double result; + int saved_errno = errno; if (!isfinite(z.real) || !isfinite(z.imag)) { /* C99 rules: if either the real or the imaginary part is an @@ -388,20 +389,24 @@ _Py_c_abs(Py_complex z) NaN. */ if (isinf(z.real)) { result = fabs(z.real); + errno = saved_errno; return result; } if (isinf(z.imag)) { result = fabs(z.imag); + errno = saved_errno; return result; } /* either the real or imaginary part is a NaN, and neither is infinite. Result should be NaN. */ + errno = saved_errno; return Py_NAN; } result = hypot(z.real, z.imag); - if (!isfinite(result)) { + if (!isfinite(result)) errno = ERANGE; - } + else + errno = saved_errno; return result; } @@ -811,11 +816,9 @@ complex_abs(PyObject *op) PyComplexObject *v = _PyComplexObject_CAST(op); double result; - result = hypot(v->cval.real, v->cval.imag); - /* Testing FE_OVERFLOW floating-point exception is slow. */ - if (isfinite(v->cval.real) && isfinite(v->cval.imag) - && !isfinite(result)) - { + errno = 0; + result = _Py_c_abs(v->cval); + if (errno == ERANGE) { PyErr_SetString(PyExc_OverflowError, "absolute value too large"); return NULL;