diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-06-30 11:13:36 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-06-30 11:13:36 (GMT) |
commit | 50b79a80bd3fe400fe60ead0ed563080fe0cac88 (patch) | |
tree | 3ebf1bd5f060cd3d77e8cfc2d09103605f32b09e /Demo/parser | |
parent | af0e1544bfce6ea500672d2140c88ad351a73c5e (diff) | |
download | cpython-50b79a80bd3fe400fe60ead0ed563080fe0cac88.zip cpython-50b79a80bd3fe400fe60ead0ed563080fe0cac88.tar.gz cpython-50b79a80bd3fe400fe60ead0ed563080fe0cac88.tar.bz2 |
Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Diffstat (limited to 'Demo/parser')
-rw-r--r-- | Demo/parser/test_unparse.py | 14 | ||||
-rwxr-xr-x[-rw-r--r--] | Demo/parser/unparse.py | 33 |
2 files changed, 42 insertions, 5 deletions
diff --git a/Demo/parser/test_unparse.py b/Demo/parser/test_unparse.py index 3a795e9..170dffa 100644 --- a/Demo/parser/test_unparse.py +++ b/Demo/parser/test_unparse.py @@ -123,6 +123,8 @@ class UnparseTestCase(ASTTestCase): def test_unary_parens(self): self.check_roundtrip("(-1)**7") + self.check_roundtrip("(-1.)**8") + self.check_roundtrip("(-1j)**6") self.check_roundtrip("not True or False") self.check_roundtrip("True or not False") @@ -132,6 +134,16 @@ class UnparseTestCase(ASTTestCase): def test_huge_float(self): self.check_roundtrip("1e1000") self.check_roundtrip("-1e1000") + self.check_roundtrip("1e1000j") + self.check_roundtrip("-1e1000j") + + def test_min_int(self): + self.check_roundtrip(str(-2**31)) + self.check_roundtrip(str(-2**63)) + + def test_imag_literals(self): + self.check_roundtrip("7j") + self.check_roundtrip("-7j") def test_lambda_parentheses(self): self.check_roundtrip("(lambda: int)()") @@ -201,7 +213,7 @@ class DirectoryTestCase(ASTTestCase): # test directories, relative to the root of the distribution test_directories = 'Lib', os.path.join('Lib', 'test') - def test_files(self): + def Xtest_files(self): # get names of files to test dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py index e1c2719..3fac1c0 100644..100755 --- a/Demo/parser/unparse.py +++ b/Demo/parser/unparse.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3.1 "Usage: unparse.py <path to source file>" import sys import math @@ -311,11 +312,35 @@ class Unparser: self.write(t.id) def _Num(self, t): - if isinstance(t.n, float) and math.isinf(t.n): - # Subsitute overflowing decimal literal for AST infinity - self.write("1e" + repr(sys.float_info.max_10_exp + 1)) + # Python doesn't have negative numeric literals, but in Python + # 2.x and early versions of Python 3.1, there's a compile-time + # operation that turns "-<number>" into a single _Num, instead + # of an unary minus applied to a _Num. Here we reverse that. + infstr = "1e" + repr(sys.float_info.max_10_exp + 1) + + if isinstance(t.n, complex): + # check that real part is as expected: 0 with appropriate sign + print(t.n) + print(str(t.n.real), str(math.copysign(0.0, t.n.imag))) + assert str(t.n.real) == str(math.copysign(0.0, t.n.imag)) + negate = math.copysign(1.0, t.n.imag) < 0 + elif isinstance(t.n, float): + negate = math.copysign(1.0, t.n) < 0 + elif isinstance(t.n, int): + negate = t.n < 0 + + if negate: + self.write("(- ") + val = -t.n else: - self.write(repr(t.n)) + val = t.n + + # Subsitute an overflowing decimal literal for an AST infinity + self.write(repr(t.n).replace("inf", infstr)) + + if negate: + self.write(")") + def _List(self, t): self.write("[") |