summaryrefslogtreecommitdiffstats
path: root/Demo/parser/unparse.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-06-28 19:44:20 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-06-28 19:44:20 (GMT)
commitae1000533ac2414975f666978e8fad5426d61095 (patch)
treea8f0ad16547ba97ea9bd2a14469f2e67fb6e4c39 /Demo/parser/unparse.py
parente893af5ab78d2cfb6403ffa6ae31145010b88101 (diff)
downloadcpython-ae1000533ac2414975f666978e8fad5426d61095.zip
cpython-ae1000533ac2414975f666978e8fad5426d61095.tar.gz
cpython-ae1000533ac2414975f666978e8fad5426d61095.tar.bz2
Merged revisions 82338,82340-82341 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r82338 | mark.dickinson | 2010-06-28 20:31:41 +0100 (Mon, 28 Jun 2010) | 9 lines Fix some shallow bugs in Demo/parser/unparse.py, and add tests: - insert commas between entries in del statement - left and right shifts were represented as >> and << (respectively); reverse - unindent properly after for: else: or while: else: - add parens around the result of an unary operation - add parens around negative numbers, to avoid turning (-1)**2 into -1**2. ........ r82340 | mark.dickinson | 2010-06-28 20:34:15 +0100 (Mon, 28 Jun 2010) | 1 line Fix typo in test_unparse.py. ........ r82341 | mark.dickinson | 2010-06-28 20:38:19 +0100 (Mon, 28 Jun 2010) | 1 line Set svn:eol-style on test_unparse.py. ........
Diffstat (limited to 'Demo/parser/unparse.py')
-rw-r--r--Demo/parser/unparse.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py
index d53a6c4..b02ef75 100644
--- a/Demo/parser/unparse.py
+++ b/Demo/parser/unparse.py
@@ -115,7 +115,7 @@ class Unparser:
def _Delete(self, t):
self.fill("del ")
- self.dispatch(t.targets)
+ interleave(lambda: self.write(", "), self.dispatch, t.targets)
def _Assert(self, t):
self.fill("assert ")
@@ -235,7 +235,7 @@ class Unparser:
self.fill("else")
self.enter()
self.dispatch(t.orelse)
- self.leave
+ self.leave()
def _If(self, t):
self.fill("if ")
@@ -260,7 +260,7 @@ class Unparser:
self.fill("else")
self.enter()
self.dispatch(t.orelse)
- self.leave
+ self.leave()
def _With(self, t):
self.fill("with ")
@@ -285,7 +285,16 @@ class Unparser:
self.write("`")
def _Num(self, t):
- self.write(repr(t.n))
+ # 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(repr(t.n))
def _List(self, t):
self.write("[")
@@ -346,13 +355,14 @@ class Unparser:
unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
def _UnaryOp(self, t):
- self.write(self.unop[t.op.__class__.__name__])
self.write("(")
+ self.write(self.unop[t.op.__class__.__name__])
+ self.write(" ")
self.dispatch(t.operand)
self.write(")")
binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
- "LShift":">>", "RShift":"<<", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
+ "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
"FloorDiv":"//", "Pow": "**"}
def _BinOp(self, t):
self.write("(")