Skip to content

Commit bb8572f

Browse files
vstinnerskirpichevhpkfft
authored
[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 340cfba) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: hpkfft.com <paul@hpkfft.com>
1 parent edabbff commit bb8572f

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎Lib/test/test_complex.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import errno
12
import unittest
23
import sys
34
from test import support
5+
from test.support import import_helper
46
from test.support.testcase import ComplexesAreIdenticalMixin
57
from test.support.numbers import (
68
VALID_UNDERSCORE_LITERALS,
@@ -9,6 +11,7 @@
911

1012
from random import random
1113
from math import isnan, copysign
14+
import cmath
1215
import operator
1316

1417
INF = float("inf")
@@ -613,8 +616,30 @@ def test_abs(self):
613616
for num in nums:
614617
self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
615618

619+
for x in 0.0, -0.0, INF, -INF, NAN:
620+
for y in 0.0, -0.0, INF, -INF, NAN:
621+
with self.subTest(x=x, y=y):
622+
z = complex(x, y)
623+
r = abs(z)
624+
if cmath.isfinite(z):
625+
self.assertFloatsAreIdentical(r, 0.0)
626+
elif cmath.isinf(z):
627+
self.assertEqual(r, INF)
628+
else:
629+
self.assertTrue(cmath.isnan(z))
630+
self.assertTrue(isnan(r))
631+
616632
self.assertRaises(OverflowError, abs, complex(DBL_MAX, DBL_MAX))
617633

634+
def test_abs_errno_handling(self):
635+
_testcapi = import_helper.import_module('_testcapi')
636+
z = complex('nan')
637+
_testcapi.set_errno(errno.ERANGE)
638+
try:
639+
self.assertTrue(isnan(abs(z)))
640+
finally:
641+
_testcapi.set_errno(0)
642+
618643
def test_repr_str(self):
619644
def test(v, expected, test_fn=self.assertEqual):
620645
test_fn(repr(v), expected)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix spurious :exc:`OverflowError` for ``abs(nanj)`` in case :c:data:`errno` was
2+
previously set to :c:macro:`!ERANGE` by some library call.
3+
Patch by Sergey B Kirpichev.

‎Modules/cmathmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ cmath_polar_impl(PyObject *module, Py_complex z)
991991
{
992992
double r, phi;
993993

994-
errno = 0;
995994
phi = c_atan2(z); /* should not cause any exception */
995+
errno = 0;
996996
r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
997997
if (errno != 0)
998998
return math_error();

‎Objects/complexobject.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ complex_abs(PyComplexObject *v)
589589
{
590590
double result;
591591

592+
errno = 0;
592593
result = _Py_c_abs(v->cval);
593594

594595
if (errno == ERANGE) {

0 commit comments

Comments
 (0)