Skip to content

Commit fe8440a

Browse files
committed
Issue python#14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
2 parents 6dbca36 + 1b2e944 commit fe8440a

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Library
2828
- Issue #14127 and #10148: shutil.copystat now preserves exact mtime and atime
2929
on filesystems providing nanosecond resolution.
3030

31+
Tools/Demos
32+
-----------
33+
34+
- Issue #14965: Bring Tools/parser/unparse.py support up to date with
35+
the Python 3.3 Grammar.
36+
3137

3238
What's New in Python 3.3.0 Alpha 3?
3339
===================================

Tools/parser/test_unparse.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ class Foo: pass
9393
suite5
9494
"""
9595

96+
with_simple = """\
97+
with f():
98+
suite1
99+
"""
100+
101+
with_as = """\
102+
with f() as x:
103+
suite1
104+
"""
105+
106+
with_two_items = """\
107+
with f() as x, g() as y:
108+
suite1
109+
"""
110+
96111
class ASTTestCase(unittest.TestCase):
97112
def assertASTEqual(self, ast1, ast2):
98113
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
@@ -209,6 +224,22 @@ def test_elifs(self):
209224
def test_try_except_finally(self):
210225
self.check_roundtrip(try_except_finally)
211226

227+
def test_starred_assignment(self):
228+
self.check_roundtrip("a, *b, c = seq")
229+
self.check_roundtrip("a, (*b, c) = seq")
230+
self.check_roundtrip("a, *b[0], c = seq")
231+
self.check_roundtrip("a, *(b, c) = seq")
232+
233+
def test_with_simple(self):
234+
self.check_roundtrip(with_simple)
235+
236+
def test_with_as(self):
237+
self.check_roundtrip(with_as)
238+
239+
def test_with_two_items(self):
240+
self.check_roundtrip(with_two_items)
241+
242+
212243
class DirectoryTestCase(ASTTestCase):
213244
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
214245

Tools/parser/unparse.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ def _Yield(self, t):
147147
self.dispatch(t.value)
148148
self.write(")")
149149

150+
def _YieldFrom(self, t):
151+
self.write("(")
152+
self.write("yield from")
153+
if t.value:
154+
self.write(" ")
155+
self.dispatch(t.value)
156+
self.write(")")
157+
150158
def _Raise(self, t):
151159
self.fill("raise")
152160
if not t.exc:
@@ -158,35 +166,24 @@ def _Raise(self, t):
158166
self.write(" from ")
159167
self.dispatch(t.cause)
160168

161-
def _TryExcept(self, t):
169+
def _Try(self, t):
162170
self.fill("try")
163171
self.enter()
164172
self.dispatch(t.body)
165173
self.leave()
166-
167174
for ex in t.handlers:
168175
self.dispatch(ex)
169176
if t.orelse:
170177
self.fill("else")
171178
self.enter()
172179
self.dispatch(t.orelse)
173180
self.leave()
174-
175-
def _TryFinally(self, t):
176-
if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
177-
# try-except-finally
178-
self.dispatch(t.body)
179-
else:
180-
self.fill("try")
181+
if t.finalbody:
182+
self.fill("finally")
181183
self.enter()
182-
self.dispatch(t.body)
184+
self.dispatch(t.finalbody)
183185
self.leave()
184186

185-
self.fill("finally")
186-
self.enter()
187-
self.dispatch(t.finalbody)
188-
self.leave()
189-
190187
def _ExceptHandler(self, t):
191188
self.fill("except")
192189
if t.type:
@@ -296,10 +293,7 @@ def _While(self, t):
296293

297294
def _With(self, t):
298295
self.fill("with ")
299-
self.dispatch(t.context_expr)
300-
if t.optional_vars:
301-
self.write(" as ")
302-
self.dispatch(t.optional_vars)
296+
interleave(lambda: self.write(", "), self.dispatch, t.items)
303297
self.enter()
304298
self.dispatch(t.body)
305299
self.leave()
@@ -472,6 +466,10 @@ def _Subscript(self, t):
472466
self.dispatch(t.slice)
473467
self.write("]")
474468

469+
def _Starred(self, t):
470+
self.write("*")
471+
self.dispatch(t.value)
472+
475473
# slice
476474
def _Ellipsis(self, t):
477475
self.write("...")
@@ -560,6 +558,12 @@ def _alias(self, t):
560558
if t.asname:
561559
self.write(" as "+t.asname)
562560

561+
def _withitem(self, t):
562+
self.dispatch(t.context_expr)
563+
if t.optional_vars:
564+
self.write(" as ")
565+
self.dispatch(t.optional_vars)
566+
563567
def roundtrip(filename, output=sys.stdout):
564568
with open(filename, "rb") as pyfile:
565569
encoding = tokenize.detect_encoding(pyfile.readline)[0]

0 commit comments

Comments
 (0)