diff options
Diffstat (limited to 'Tools/parser')
-rw-r--r-- | Tools/parser/test_unparse.py | 31 | ||||
-rw-r--r-- | Tools/parser/unparse.py | 39 |
2 files changed, 49 insertions, 21 deletions
diff --git a/Tools/parser/test_unparse.py b/Tools/parser/test_unparse.py index 2ac1ea6..be84400 100644 --- a/Tools/parser/test_unparse.py +++ b/Tools/parser/test_unparse.py @@ -2,9 +2,10 @@ import unittest import test.support import io import os +import random import tokenize -import ast import unparse +import ast def read_pyfile(filename): """Read and return the contents of a Python source file (as a @@ -93,6 +94,21 @@ finally: suite5 """ +with_simple = """\ +with f(): + suite1 +""" + +with_as = """\ +with f() as x: + suite1 +""" + +with_two_items = """\ +with f() as x, g() as y: + suite1 +""" + class ASTTestCase(unittest.TestCase): def assertASTEqual(self, ast1, ast2): self.assertEqual(ast.dump(ast1), ast.dump(ast2)) @@ -215,6 +231,15 @@ class UnparseTestCase(ASTTestCase): self.check_roundtrip("a, *b[0], c = seq") self.check_roundtrip("a, *(b, c) = seq") + def test_with_simple(self): + self.check_roundtrip(with_simple) + + def test_with_as(self): + self.check_roundtrip(with_as) + + def test_with_two_items(self): + self.check_roundtrip(with_two_items) + class DirectoryTestCase(ASTTestCase): """Test roundtrip behaviour on all files in Lib and Lib/test.""" @@ -233,6 +258,10 @@ class DirectoryTestCase(ASTTestCase): if n.endswith('.py') and not n.startswith('bad'): names.append(os.path.join(test_dir, n)) + # Test limited subset of files unless the 'cpu' resource is specified. + if not test.support.is_resource_enabled("cpu"): + names = random.sample(names, 10) + for filename in names: if test.support.verbose: print('Testing %s' % filename) diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py index d9fca97..b55e2c6 100644 --- a/Tools/parser/unparse.py +++ b/Tools/parser/unparse.py @@ -1,6 +1,5 @@ "Usage: unparse.py <path to source file>" import sys -import math import ast import tokenize import io @@ -147,6 +146,14 @@ class Unparser: self.dispatch(t.value) self.write(")") + def _YieldFrom(self, t): + self.write("(") + self.write("yield from") + if t.value: + self.write(" ") + self.dispatch(t.value) + self.write(")") + def _Raise(self, t): self.fill("raise") if not t.exc: @@ -158,12 +165,11 @@ class Unparser: self.write(" from ") self.dispatch(t.cause) - def _TryExcept(self, t): + def _Try(self, t): self.fill("try") self.enter() self.dispatch(t.body) self.leave() - for ex in t.handlers: self.dispatch(ex) if t.orelse: @@ -171,22 +177,12 @@ class Unparser: self.enter() self.dispatch(t.orelse) self.leave() - - def _TryFinally(self, t): - if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept): - # try-except-finally - self.dispatch(t.body) - else: - self.fill("try") + if t.finalbody: + self.fill("finally") self.enter() - self.dispatch(t.body) + self.dispatch(t.finalbody) self.leave() - self.fill("finally") - self.enter() - self.dispatch(t.finalbody) - self.leave() - def _ExceptHandler(self, t): self.fill("except") if t.type: @@ -296,10 +292,7 @@ class Unparser: def _With(self, t): self.fill("with ") - self.dispatch(t.context_expr) - if t.optional_vars: - self.write(" as ") - self.dispatch(t.optional_vars) + interleave(lambda: self.write(", "), self.dispatch, t.items) self.enter() self.dispatch(t.body) self.leave() @@ -564,6 +557,12 @@ class Unparser: if t.asname: self.write(" as "+t.asname) + def _withitem(self, t): + self.dispatch(t.context_expr) + if t.optional_vars: + self.write(" as ") + self.dispatch(t.optional_vars) + def roundtrip(filename, output=sys.stdout): with open(filename, "rb") as pyfile: encoding = tokenize.detect_encoding(pyfile.readline)[0] |