@@ -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
0 commit comments