Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit d9724ed

Browse files
authored
Merge pull request #87 from juanjux/fix/issue_77
Fix/issue 77
2 parents 3193158 + 927845d commit d9724ed

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

native/python_package/python_driver/astimprove.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def sync_node_pos(self, nodedict, add = 0):
114114
the same name will not consume that token again (except for fstrings that are
115115
a special case of a token mapping to several possible AST nodes).
116116
"""
117-
118117
node_line = nodedict.get('lineno')
119118
if node_line is None:
120119
return
@@ -359,6 +358,8 @@ def __init__(self, codestr, astdict):
359358
# on parse()
360359
self._node2endpos = None
361360

361+
self.visit_Global = self.visit_Nonlocal = self._promote_names
362+
362363
def _add_noops(self, node, visit_dict, root):
363364
if not isinstance(visit_dict, dict):
364365
return visit_dict
@@ -485,25 +486,16 @@ def visit_NoneType(self, node):
485486
"ast_type": "NoneLiteral"})
486487
return node
487488

488-
def visit_Global(self, node):
489+
def _promote_names(self, node):
489490
# Python AST by default stores global and nonlocal variable names
490491
# in a "names" array of strings. That breaks the structure of everything
491492
# else in the AST (dictionaries, properties or list of objects) so we
492493
# convert those names to Name objects
493-
names_as_nodes = [{"ast_type": "Name",
494-
"id": i,
495-
"lineno": node["lineno"],
496-
"col_offset": node["col_offset"]} for i in node["names"]]
497-
498-
node["names"] = names_as_nodes
499-
return node
494+
names_as_nodes = [self.visit({"ast_type": "Name",
495+
"id": i,
496+
"lineno": node["lineno"]})
497+
for i in node["names"]]
500498

501-
def visit_Nonlocal(self, node):
502-
# ditto
503-
names_as_nodes = [{"ast_type": "Name",
504-
"id": i,
505-
"lineno": node["lineno"],
506-
"col_offset": node["col_offset"]} for i in node["names"]]
507499
node["names"] = names_as_nodes
508500
return node
509501

tests/other_statements.py.native

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@
9494
"names": [
9595
{
9696
"ast_type": "Name",
97-
"col_offset": 4,
97+
"col_offset": 12,
98+
"end_col_offset": 12,
99+
"end_lineno": 5,
98100
"id": "a",
99101
"lineno": 5
100102
}
@@ -109,7 +111,9 @@
109111
"names": [
110112
{
111113
"ast_type": "Name",
112-
"col_offset": 4,
114+
"col_offset": 14,
115+
"end_col_offset": 14,
116+
"end_lineno": 6,
113117
"id": "b",
114118
"lineno": 6
115119
}

tests/other_statements.py.uast

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,14 @@ Module {
181181
. . . . . . . . . Roles: SimpleIdentifier,Expression
182182
. . . . . . . . . TOKEN "a"
183183
. . . . . . . . . StartPosition: {
184-
. . . . . . . . . . Offset: 30
184+
. . . . . . . . . . Offset: 38
185185
. . . . . . . . . . Line: 5
186-
. . . . . . . . . . Col: 4
186+
. . . . . . . . . . Col: 12
187+
. . . . . . . . . }
188+
. . . . . . . . . EndPosition: {
189+
. . . . . . . . . . Offset: 38
190+
. . . . . . . . . . Line: 5
191+
. . . . . . . . . . Col: 12
187192
. . . . . . . . . }
188193
. . . . . . . . . Properties: {
189194
. . . . . . . . . . internalRole: names
@@ -209,9 +214,14 @@ Module {
209214
. . . . . . . . . Roles: SimpleIdentifier,Expression
210215
. . . . . . . . . TOKEN "b"
211216
. . . . . . . . . StartPosition: {
212-
. . . . . . . . . . Offset: 43
217+
. . . . . . . . . . Offset: 53
218+
. . . . . . . . . . Line: 6
219+
. . . . . . . . . . Col: 14
220+
. . . . . . . . . }
221+
. . . . . . . . . EndPosition: {
222+
. . . . . . . . . . Offset: 53
213223
. . . . . . . . . . Line: 6
214-
. . . . . . . . . . Col: 4
224+
. . . . . . . . . . Col: 14
215225
. . . . . . . . . }
216226
. . . . . . . . . Properties: {
217227
. . . . . . . . . . internalRole: names

0 commit comments

Comments
 (0)