-
Notifications
You must be signed in to change notification settings - Fork 52
Allow user to specify version_info #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,14 +24,15 @@ def interleave(inter, f, seq): | |
| inter() | ||
| f(x) | ||
|
|
||
| class Unparser: | ||
| class Unparser(object): | ||
| """Methods in this class recursively traverse an AST and | ||
| output source code for the abstract syntax; original formatting | ||
| is disregarded. """ | ||
|
|
||
| def __init__(self, tree, file=sys.stdout): | ||
| def __init__(self, tree, file=sys.stdout, version_info=None): | ||
| """Unparser(tree, file=sys.stdout) -> None. | ||
| Print the source for tree to file.""" | ||
| self.version_info = version_info or sys.version_info | ||
| self.f = file | ||
| self.future_imports = [] | ||
| self._indent = 0 | ||
|
|
@@ -207,7 +208,7 @@ def _YieldFrom(self, t): | |
|
|
||
| def _Raise(self, t): | ||
| self.fill("raise") | ||
| if six.PY3: | ||
| if self.version_info[0] == 3: | ||
| if not t.exc: | ||
| assert not t.cause | ||
| return | ||
|
|
@@ -281,7 +282,7 @@ def _ExceptHandler(self, t): | |
| self.dispatch(t.type) | ||
| if t.name: | ||
| self.write(" as ") | ||
| if six.PY3: | ||
| if isinstance(t.name, six.string_types): | ||
| self.write(t.name) | ||
| else: | ||
| self.dispatch(t.name) | ||
|
|
@@ -295,7 +296,7 @@ def _ClassDef(self, t): | |
| self.fill("@") | ||
| self.dispatch(deco) | ||
| self.fill("class "+t.name) | ||
| if six.PY3: | ||
| if self.version_info[0] == 3: | ||
| self.write("(") | ||
| comma = False | ||
| for e in t.bases: | ||
|
|
@@ -306,7 +307,7 @@ def _ClassDef(self, t): | |
| if comma: self.write(", ") | ||
| else: comma = True | ||
| self.dispatch(e) | ||
| if sys.version_info[:2] < (3, 5): | ||
| if self.version_info[:2] < (3, 5): | ||
| if t.starargs: | ||
| if comma: self.write(", ") | ||
| else: comma = True | ||
|
|
@@ -427,7 +428,7 @@ def _Bytes(self, t): | |
| self.write(repr(t.s)) | ||
|
|
||
| def _Str(self, tree): | ||
| if six.PY3: | ||
| if self.version_info[0] == 3: | ||
| self.write(repr(tree.s)) | ||
| else: | ||
| # if from __future__ import unicode_literals is in effect, | ||
|
|
@@ -501,7 +502,7 @@ def _Repr(self, t): | |
|
|
||
| def _Num(self, t): | ||
| repr_n = repr(t.n) | ||
| if six.PY3: | ||
| if self.version_info[0] == 3: | ||
| self.write(repr_n.replace("inf", INFSTR)) | ||
| else: | ||
| # Parenthesize negative numbers, to avoid turning (-1)**2 into -1**2. | ||
|
|
@@ -608,7 +609,7 @@ def _UnaryOp(self, t): | |
| self.write("(") | ||
| self.write(self.unop[t.op.__class__.__name__]) | ||
| self.write(" ") | ||
| if six.PY2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num): | ||
| if self.version_info[0] == 2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num): | ||
| # If we're applying unary minus to a number, parenthesize the number. | ||
| # This is necessary: -2147483648 is different from -(2147483648) on | ||
| # a 32-bit machine (the first is an int, the second a long), and | ||
|
|
@@ -671,7 +672,7 @@ def _Call(self, t): | |
| if comma: self.write(", ") | ||
| else: comma = True | ||
| self.dispatch(e) | ||
| if sys.version_info[:2] < (3, 5): | ||
| if self.version_info[:2] < (3, 5): | ||
| if t.starargs: | ||
| if comma: self.write(", ") | ||
| else: comma = True | ||
|
|
@@ -770,6 +771,11 @@ def _arguments(self, t): | |
| if t.kwarg.annotation: | ||
| self.write(": ") | ||
| self.dispatch(t.kwarg.annotation) | ||
| elif hasattr(t.kwarg, 'id'): | ||
| self.write("**"+t.kwarg.id) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I agree that would be better style, it's better to remain consistent with the rest of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Absolutely! I actually went and check how unparser.py has it, and in immediate vicinity of your changes I saw this: https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L763 , that's why I made a comment. Looking in more detail, I see cases like https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L804 , https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L809 - so yes, your change looks good wrt to them.
I'm sure that falls on maintainer's plate. (I'm just a random passer-by who spotted an issue with the lib: #39, and proceeded to check how active maintenance of this repo by looking at pending PRs, then figuring I could benefit from this PR too. Oh, btw, ping @simonpercivall ;-) ).
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Off-topic: I'm pretty bad at maintaining this repository :/ Re. style: The style (and code) line-by-line should be kept as near identical as possible to the different versions of |
||
| if t.kwarg.annotation: | ||
| self.write(": ") | ||
| self.dispatch(t.kwarg.annotation) | ||
| else: | ||
| self.write("**"+t.kwarg) | ||
| if getattr(t, 'kwargannotation', None): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is apparently change unrelated to the functionality provided.