mirror of https://github.com/python/cpython
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.
This commit is contained in:
parent
8c996ef458
commit
623b979553
|
@ -0,0 +1,61 @@
|
||||||
|
import unittest
|
||||||
|
from test import test_support
|
||||||
|
|
||||||
|
import cStringIO
|
||||||
|
import ast
|
||||||
|
import _ast
|
||||||
|
import unparse
|
||||||
|
|
||||||
|
forelse = """\
|
||||||
|
def f():
|
||||||
|
for x in range(10):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
y = 2
|
||||||
|
z = 3
|
||||||
|
"""
|
||||||
|
|
||||||
|
whileelse = """\
|
||||||
|
def g():
|
||||||
|
while True:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
y = 2
|
||||||
|
z = 3
|
||||||
|
"""
|
||||||
|
|
||||||
|
class UnparseTestCase(unittest.TestCase):
|
||||||
|
# Tests for specific bugs found in earlier versions of unparse
|
||||||
|
|
||||||
|
def check_roundtrip(self, code1, filename="internal"):
|
||||||
|
ast1 = compile(code1, filename, "exec", _ast.PyCF_ONLY_AST)
|
||||||
|
unparse_buffer = cStringIO.StringIO()
|
||||||
|
unparse.Unparser(ast1, unparse_buffer)
|
||||||
|
code2 = unparse_buffer.getvalue()
|
||||||
|
ast2 = compile(code2, filename, "exec", _ast.PyCF_ONLY_AST)
|
||||||
|
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
|
||||||
|
|
||||||
|
def test_del_statement(self):
|
||||||
|
self.check_roundtrip("del x, y, z")
|
||||||
|
|
||||||
|
def test_shifts(self):
|
||||||
|
self.check_roundtrip("45 << 2")
|
||||||
|
self.check_roundtrip("13 >> 7")
|
||||||
|
|
||||||
|
def test_for_else(self):
|
||||||
|
self.check_roundtrip(forelse)
|
||||||
|
|
||||||
|
def test_while_else(self):
|
||||||
|
self.check_roundtrip(forelse)
|
||||||
|
|
||||||
|
def test_unary_parens(self):
|
||||||
|
self.check_roundtrip("(-1)**7")
|
||||||
|
self.check_roundtrip("not True or False")
|
||||||
|
self.check_roundtrip("True or not False")
|
||||||
|
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(UnparseTestCase)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_main()
|
|
@ -115,7 +115,7 @@ class Unparser:
|
||||||
|
|
||||||
def _Delete(self, t):
|
def _Delete(self, t):
|
||||||
self.fill("del ")
|
self.fill("del ")
|
||||||
self.dispatch(t.targets)
|
interleave(lambda: self.write(", "), self.dispatch, t.targets)
|
||||||
|
|
||||||
def _Assert(self, t):
|
def _Assert(self, t):
|
||||||
self.fill("assert ")
|
self.fill("assert ")
|
||||||
|
@ -245,7 +245,7 @@ class Unparser:
|
||||||
self.fill("else")
|
self.fill("else")
|
||||||
self.enter()
|
self.enter()
|
||||||
self.dispatch(t.orelse)
|
self.dispatch(t.orelse)
|
||||||
self.leave
|
self.leave()
|
||||||
|
|
||||||
def _If(self, t):
|
def _If(self, t):
|
||||||
self.fill("if ")
|
self.fill("if ")
|
||||||
|
@ -270,7 +270,7 @@ class Unparser:
|
||||||
self.fill("else")
|
self.fill("else")
|
||||||
self.enter()
|
self.enter()
|
||||||
self.dispatch(t.orelse)
|
self.dispatch(t.orelse)
|
||||||
self.leave
|
self.leave()
|
||||||
|
|
||||||
def _With(self, t):
|
def _With(self, t):
|
||||||
self.fill("with ")
|
self.fill("with ")
|
||||||
|
@ -295,7 +295,16 @@ class Unparser:
|
||||||
self.write("`")
|
self.write("`")
|
||||||
|
|
||||||
def _Num(self, t):
|
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):
|
def _List(self, t):
|
||||||
self.write("[")
|
self.write("[")
|
||||||
|
@ -355,13 +364,14 @@ class Unparser:
|
||||||
|
|
||||||
unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
|
unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
|
||||||
def _UnaryOp(self, t):
|
def _UnaryOp(self, t):
|
||||||
self.write(self.unop[t.op.__class__.__name__])
|
|
||||||
self.write("(")
|
self.write("(")
|
||||||
|
self.write(self.unop[t.op.__class__.__name__])
|
||||||
|
self.write(" ")
|
||||||
self.dispatch(t.operand)
|
self.dispatch(t.operand)
|
||||||
self.write(")")
|
self.write(")")
|
||||||
|
|
||||||
binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
|
binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
|
||||||
"LShift":">>", "RShift":"<<", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
|
"LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
|
||||||
"FloorDiv":"//", "Pow": "**"}
|
"FloorDiv":"//", "Pow": "**"}
|
||||||
def _BinOp(self, t):
|
def _BinOp(self, t):
|
||||||
self.write("(")
|
self.write("(")
|
||||||
|
|
Loading…
Reference in New Issue