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/4] 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/4] 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/4] + 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/4] + 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;