Skip to content

Commit 615af70

Browse files
committed
Allow user to specify version_info rather than assuming AST version matches the current Python version. This is useful if the user is using e.g. gast to get a different AST version from the Python version they are using.
1 parent c363330 commit 615af70

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/astunparse/unparser.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ def interleave(inter, f, seq):
2424
inter()
2525
f(x)
2626

27-
class Unparser:
27+
class Unparser(object):
2828
"""Methods in this class recursively traverse an AST and
2929
output source code for the abstract syntax; original formatting
3030
is disregarded. """
3131

32-
def __init__(self, tree, file=sys.stdout):
32+
def __init__(self, tree, file=sys.stdout, version_info=None):
3333
"""Unparser(tree, file=sys.stdout) -> None.
3434
Print the source for tree to file."""
35+
self.version_info = version_info or sys.version_info
3536
self.f = file
3637
self.future_imports = []
3738
self._indent = 0
@@ -207,7 +208,7 @@ def _YieldFrom(self, t):
207208

208209
def _Raise(self, t):
209210
self.fill("raise")
210-
if six.PY3:
211+
if self.version_info[0] == 3:
211212
if not t.exc:
212213
assert not t.cause
213214
return
@@ -281,7 +282,7 @@ def _ExceptHandler(self, t):
281282
self.dispatch(t.type)
282283
if t.name:
283284
self.write(" as ")
284-
if six.PY3:
285+
if isinstance(t.name, six.string_types):
285286
self.write(t.name)
286287
else:
287288
self.dispatch(t.name)
@@ -295,7 +296,7 @@ def _ClassDef(self, t):
295296
self.fill("@")
296297
self.dispatch(deco)
297298
self.fill("class "+t.name)
298-
if six.PY3:
299+
if self.version_info[0] == 3:
299300
self.write("(")
300301
comma = False
301302
for e in t.bases:
@@ -306,7 +307,7 @@ def _ClassDef(self, t):
306307
if comma: self.write(", ")
307308
else: comma = True
308309
self.dispatch(e)
309-
if sys.version_info[:2] < (3, 5):
310+
if self.version_info[:2] < (3, 5):
310311
if t.starargs:
311312
if comma: self.write(", ")
312313
else: comma = True
@@ -427,7 +428,7 @@ def _Bytes(self, t):
427428
self.write(repr(t.s))
428429

429430
def _Str(self, tree):
430-
if six.PY3:
431+
if self.version_info[0] == 3:
431432
self.write(repr(tree.s))
432433
else:
433434
# if from __future__ import unicode_literals is in effect,
@@ -483,7 +484,7 @@ def _Repr(self, t):
483484

484485
def _Num(self, t):
485486
repr_n = repr(t.n)
486-
if six.PY3:
487+
if self.version_info[0] == 3:
487488
self.write(repr_n.replace("inf", INFSTR))
488489
else:
489490
# Parenthesize negative numbers, to avoid turning (-1)**2 into -1**2.
@@ -586,7 +587,7 @@ def _UnaryOp(self, t):
586587
self.write("(")
587588
self.write(self.unop[t.op.__class__.__name__])
588589
self.write(" ")
589-
if six.PY2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
590+
if self.version_info[0] == 2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
590591
# If we're applying unary minus to a number, parenthesize the number.
591592
# This is necessary: -2147483648 is different from -(2147483648) on
592593
# a 32-bit machine (the first is an int, the second a long), and
@@ -649,7 +650,7 @@ def _Call(self, t):
649650
if comma: self.write(", ")
650651
else: comma = True
651652
self.dispatch(e)
652-
if sys.version_info[:2] < (3, 5):
653+
if self.version_info[:2] < (3, 5):
653654
if t.starargs:
654655
if comma: self.write(", ")
655656
else: comma = True
@@ -748,6 +749,11 @@ def _arguments(self, t):
748749
if t.kwarg.annotation:
749750
self.write(": ")
750751
self.dispatch(t.kwarg.annotation)
752+
elif hasattr(t.kwarg, 'id'):
753+
self.write("**"+t.kwarg.id)
754+
if t.kwarg.annotation:
755+
self.write(": ")
756+
self.dispatch(t.kwarg.annotation)
751757
else:
752758
self.write("**"+t.kwarg)
753759
if getattr(t, 'kwargannotation', None):

0 commit comments

Comments
 (0)