| 
 | 1 | +import cmath  | 
1 | 2 | import unittest  | 
2 | 3 | import sys  | 
3 | 4 | from test import support  | 
 | 
7 | 8 | 
 
  | 
8 | 9 | from random import random  | 
9 | 10 | from math import isnan, copysign  | 
 | 11 | +from itertools import combinations_with_replacement  | 
10 | 12 | import operator  | 
 | 13 | +import _testcapi  | 
11 | 14 | 
 
  | 
12 | 15 | INF = float("inf")  | 
13 | 16 | NAN = float("nan")  | 
@@ -412,11 +415,6 @@ def test_pow(self):  | 
412 | 415 |                     except OverflowError:  | 
413 | 416 |                         pass  | 
414 | 417 | 
 
  | 
415 |  | -        # Check that complex numbers with special components  | 
416 |  | -        # are correctly handled.  | 
417 |  | -        self.assertComplexesAreIdentical(complex(1, +0.0)**2, complex(1, +0.0))  | 
418 |  | -        self.assertComplexesAreIdentical(complex(1, -0.0)**2, complex(1, -0.0))  | 
419 |  | - | 
420 | 418 |         # gh-113841: possible undefined division by 0 in _Py_c_pow()  | 
421 | 419 |         x, y = 9j, 33j**3  | 
422 | 420 |         with self.assertRaises(OverflowError):  | 
@@ -450,6 +448,36 @@ def test_pow_with_small_integer_exponents(self):  | 
450 | 448 |                     self.assertEqual(str(float_pow), str(int_pow))  | 
451 | 449 |                     self.assertEqual(str(complex_pow), str(int_pow))  | 
452 | 450 | 
 
  | 
 | 451 | + | 
 | 452 | +        # Check that complex numbers with special components  | 
 | 453 | +        # are correctly handled.  | 
 | 454 | +        values = [complex(*_)  | 
 | 455 | +                  for _ in combinations_with_replacement([1, -1, 0.0, 0, -0.0, 2,  | 
 | 456 | +                                                          -3, INF, -INF, NAN], 2)]  | 
 | 457 | +        exponents = [0, 1, 2, 3, 4, 5, 6, 19]  | 
 | 458 | +        for z in values:  | 
 | 459 | +            for e in exponents:  | 
 | 460 | +                with self.subTest(value=z, exponent=e):  | 
 | 461 | +                    if cmath.isfinite(z) and z.real and z.imag:  | 
 | 462 | +                        continue  | 
 | 463 | +                    try:  | 
 | 464 | +                        r_pow = z**e  | 
 | 465 | +                    except OverflowError:  | 
 | 466 | +                        continue  | 
 | 467 | +                    # Use the generic complex power algorithm.  | 
 | 468 | +                    r_pro, r_pro_errno = _testcapi._py_c_pow(z, e)  | 
 | 469 | +                    self.assertEqual(r_pro_errno, 0)  | 
 | 470 | +                    if isnan(r_pow.real):  | 
 | 471 | +                        self.assertTrue(isnan(r_pro.real))  | 
 | 472 | +                    else:  | 
 | 473 | +                        self.assertEqual(copysign(1, r_pow.real),  | 
 | 474 | +                                         copysign(1, r_pro.real))  | 
 | 475 | +                    if isnan(r_pow.imag):  | 
 | 476 | +                        self.assertTrue(isnan(r_pro.imag))  | 
 | 477 | +                    else:  | 
 | 478 | +                        self.assertEqual(copysign(1, r_pow.imag),  | 
 | 479 | +                                         copysign(1, r_pro.imag))  | 
 | 480 | + | 
453 | 481 |     def test_boolcontext(self):  | 
454 | 482 |         for i in range(100):  | 
455 | 483 |             self.assertTrue(complex(random() + 1e-6, random() + 1e-6))  | 
 | 
0 commit comments