diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-06-29 08:52:36 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-06-29 08:52:36 (GMT) |
commit | 3eb0290346199a850bc0b001c3e15ea75fdfff8f (patch) | |
tree | 61b3003af77780f910785a0108fabff3f6747b2e /Demo/parser | |
parent | 82c8d93357016d81b051f50d394bce028576967f (diff) | |
download | cpython-3eb0290346199a850bc0b001c3e15ea75fdfff8f.zip cpython-3eb0290346199a850bc0b001c3e15ea75fdfff8f.tar.gz cpython-3eb0290346199a850bc0b001c3e15ea75fdfff8f.tar.bz2 |
Add parentheses around numeric literals, to avoid turning 3 .bit_length() into 3.bit_length().
Diffstat (limited to 'Demo/parser')
-rw-r--r-- | Demo/parser/test_unparse.py | 3 | ||||
-rw-r--r-- | Demo/parser/unparse.py | 21 |
2 files changed, 14 insertions, 10 deletions
diff --git a/Demo/parser/test_unparse.py b/Demo/parser/test_unparse.py index 5832b8f..0d13897 100644 --- a/Demo/parser/test_unparse.py +++ b/Demo/parser/test_unparse.py @@ -84,6 +84,9 @@ class UnparseTestCase(unittest.TestCase): self.check_roundtrip("not True or False") self.check_roundtrip("True or not False") + def test_integer_parens(self): + self.check_roundtrip("3 .__abs__()") + def test_chained_comparisons(self): self.check_roundtrip("1 < 4 <= 5") self.check_roundtrip("a is b is c is not d") diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py index 5672873..2c383a5 100644 --- a/Demo/parser/unparse.py +++ b/Demo/parser/unparse.py @@ -302,16 +302,17 @@ class Unparser: self.write("`") def _Num(self, t): - # There are no negative numeric literals in Python; however, - # some optimizations produce a negative Num in the AST. Add - # parentheses to avoid turning (-1)**2 into -1**2. - strnum = repr(t.n) - if strnum.startswith("-"): - self.write("(") - self.write(strnum) - self.write(")") - else: - self.write(strnum) + # Add parentheses around numeric literals to avoid: + # + # (1) turning (-1)**2 into -1**2, and + # (2) turning 3 .__abs__() into 3.__abs__() + # + # For (1), note that Python doesn't actually have negative + # numeric literals, but (at least in Python 2.x) there's a CST + # transformation that can produce negative Nums in the AST. + self.write("(") + self.write(repr(t.n)) + self.write(")") def _List(self, t): self.write("[") |