summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-26 19:29:52 (GMT)
committerGitHub <noreply@github.com>2021-07-26 19:29:52 (GMT)
commit256d97c8a3151ec7caba6c06bf2a1e682008c304 (patch)
tree32a599138cd5d81cbd8e56a4cec2a590083af760 /Lib
parent8a37e8cf45105fbb592c31ebd30501bbdea5ab81 (diff)
downloadcpython-256d97c8a3151ec7caba6c06bf2a1e682008c304.zip
cpython-256d97c8a3151ec7caba6c06bf2a1e682008c304.tar.gz
cpython-256d97c8a3151ec7caba6c06bf2a1e682008c304.tar.bz2
bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278) (#27366)
(cherry picked from commit 1d582bbc969e05896addf97844ddf17ce9830e5e) Co-authored-by: T. Wouters <thomas@python.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_complex.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index af39ee8..badb234 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -1,4 +1,5 @@
import unittest
+import sys
from test import support
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
@@ -248,6 +249,26 @@ class ComplexTest(unittest.TestCase):
b = 5.1+2.3j
self.assertRaises(ValueError, pow, a, b, 0)
+ # Check some boundary conditions; some of these used to invoke
+ # undefined behaviour (https://bugs.python.org/issue44698). We're
+ # not actually checking the results of these operations, just making
+ # sure they don't crash (for example when using clang's
+ # UndefinedBehaviourSanitizer).
+ values = (sys.maxsize, sys.maxsize+1, sys.maxsize-1,
+ -sys.maxsize, -sys.maxsize+1, -sys.maxsize+1)
+ for real in values:
+ for imag in values:
+ with self.subTest(real=real, imag=imag):
+ c = complex(real, imag)
+ try:
+ c ** real
+ except OverflowError:
+ pass
+ try:
+ c ** c
+ except OverflowError:
+ pass
+
def test_boolcontext(self):
for i in range(100):
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))