mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
- relative imports - keyword-only arguments - function annotations - class decorators - raise ... from ... - except ... as ... - nonlocal - bytes literals - set literals - set comprehensions - dict comprehensions Removals: - print statement. Some of this should be backported to 2.x.
This commit is contained in:
parent
f5451e546a
commit
fa2e4e9d04
2 changed files with 194 additions and 54 deletions
|
@ -6,7 +6,7 @@ import ast
|
|||
import _ast
|
||||
import unparse
|
||||
|
||||
forelse = """\
|
||||
for_else = """\
|
||||
def f():
|
||||
for x in range(10):
|
||||
break
|
||||
|
@ -15,7 +15,7 @@ def f():
|
|||
z = 3
|
||||
"""
|
||||
|
||||
whileelse = """\
|
||||
while_else = """\
|
||||
def g():
|
||||
while True:
|
||||
break
|
||||
|
@ -24,6 +24,37 @@ def g():
|
|||
z = 3
|
||||
"""
|
||||
|
||||
relative_import = """\
|
||||
from . import fred
|
||||
from .. import barney
|
||||
from .australia import shrimp as prawns
|
||||
"""
|
||||
|
||||
nonlocal_ex = """\
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
nonlocal x
|
||||
x = 2
|
||||
y = 7
|
||||
def h():
|
||||
nonlocal x, y
|
||||
"""
|
||||
|
||||
# also acts as test for 'except ... as ...'
|
||||
raise_from = """\
|
||||
try:
|
||||
1 / 0
|
||||
except ZeroDivisionError as e:
|
||||
raise ArithmeticError from e
|
||||
"""
|
||||
|
||||
class_decorator = """\
|
||||
@f1(arg)
|
||||
@f2
|
||||
class Foo: pass
|
||||
"""
|
||||
|
||||
class UnparseTestCase(unittest.TestCase):
|
||||
# Tests for specific bugs found in earlier versions of unparse
|
||||
|
||||
|
@ -43,10 +74,10 @@ class UnparseTestCase(unittest.TestCase):
|
|||
self.check_roundtrip("13 >> 7")
|
||||
|
||||
def test_for_else(self):
|
||||
self.check_roundtrip(forelse)
|
||||
self.check_roundtrip(for_else)
|
||||
|
||||
def test_while_else(self):
|
||||
self.check_roundtrip(whileelse)
|
||||
self.check_roundtrip(while_else)
|
||||
|
||||
def test_unary_parens(self):
|
||||
self.check_roundtrip("(-1)**7")
|
||||
|
@ -57,6 +88,50 @@ class UnparseTestCase(unittest.TestCase):
|
|||
self.check_roundtrip("1 < 4 <= 5")
|
||||
self.check_roundtrip("a is b is c is not d")
|
||||
|
||||
def test_function_arguments(self):
|
||||
self.check_roundtrip("def f(): pass")
|
||||
self.check_roundtrip("def f(a): pass")
|
||||
self.check_roundtrip("def f(b = 2): pass")
|
||||
self.check_roundtrip("def f(a, b): pass")
|
||||
self.check_roundtrip("def f(a, b = 2): pass")
|
||||
self.check_roundtrip("def f(a = 5, b = 2): pass")
|
||||
self.check_roundtrip("def f(*, a = 1, b = 2): pass")
|
||||
self.check_roundtrip("def f(*, a = 1, b): pass")
|
||||
self.check_roundtrip("def f(*, a, b = 2): pass")
|
||||
self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
|
||||
self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
|
||||
self.check_roundtrip("def f(*args, **kwargs): pass")
|
||||
|
||||
def test_relative_import(self):
|
||||
self.check_roundtrip(relative_import)
|
||||
|
||||
def test_nonlocal(self):
|
||||
self.check_roundtrip(nonlocal_ex)
|
||||
|
||||
def test_raise_from(self):
|
||||
self.check_roundtrip(raise_from)
|
||||
|
||||
def test_bytes(self):
|
||||
self.check_roundtrip("b'123'")
|
||||
|
||||
def test_annotations(self):
|
||||
self.check_roundtrip("def f(a : int): pass")
|
||||
self.check_roundtrip("def f(a: int = 5): pass")
|
||||
self.check_roundtrip("def f(*args: [int]): pass")
|
||||
self.check_roundtrip("def f(**kwargs: dict): pass")
|
||||
self.check_roundtrip("def f() -> None: pass")
|
||||
|
||||
def test_set_literal(self):
|
||||
self.check_roundtrip("{'a', 'b', 'c'}")
|
||||
|
||||
def test_set_comprehension(self):
|
||||
self.check_roundtrip("{x for x in range(5)}")
|
||||
|
||||
def test_dict_comprehension(self):
|
||||
self.check_roundtrip("{x: x*x for x in range(10)}")
|
||||
|
||||
def test_class_decorators(self):
|
||||
self.check_roundtrip(class_decorator)
|
||||
|
||||
def test_main():
|
||||
test.support.run_unittest(UnparseTestCase)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue