[Python-checkins] r82359 - in python/branches/py3k/Demo/parser: test_unparse.py unparse.py

mark.dickinson python-checkins at python.org
Tue Jun 29 10:52:36 CEST 2010


Author: mark.dickinson
Date: Tue Jun 29 10:52:36 2010
New Revision: 82359

Log:
Add parentheses around numeric literals, to avoid turning 3 .bit_length() into 3.bit_length().

Modified:
   python/branches/py3k/Demo/parser/test_unparse.py
   python/branches/py3k/Demo/parser/unparse.py

Modified: python/branches/py3k/Demo/parser/test_unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/test_unparse.py	(original)
+++ python/branches/py3k/Demo/parser/test_unparse.py	Tue Jun 29 10:52:36 2010
@@ -84,6 +84,9 @@
         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")

Modified: python/branches/py3k/Demo/parser/unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/unparse.py	(original)
+++ python/branches/py3k/Demo/parser/unparse.py	Tue Jun 29 10:52:36 2010
@@ -302,16 +302,17 @@
         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("[")


More information about the Python-checkins mailing list