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

Commit 364c20f

Browse files
author
Juanjo Alvarez
committed
Removed missed position workaround for fstrings
1 parent af440be commit 364c20f

File tree

3 files changed

+53
-165
lines changed

3 files changed

+53
-165
lines changed

native/python_package/python_driver/astimprove.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,6 @@ def sync_node_pos(self, nodedict, add = 0):
147147
nodedict["end_lineno"] = token[TOKEN_ENDLOC][TOKENROW]
148148
nodedict["end_col_offset"] = token[TOKEN_ENDLOC][TOKENCOL]
149149

150-
def fix_embeded_pos(self, nodedict, add):
151-
"""
152-
For nodes that wrongly start from 1 (like subcode inside f-strings)
153-
this fixes the lineno field adding the argument (which should be
154-
the parent node correct lineno)
155-
"""
156-
nodedict["lineno"] += add - 1
157-
158-
for key in nodedict:
159-
if isinstance(nodedict[key], dict):
160-
self.fix_embeded_pos(nodedict[key], add)
161-
162-
self.sync_node_pos(nodedict, add = 1)
163-
return nodedict
164-
165150

166151
class NoopExtractor(object):
167152
"""
@@ -370,10 +355,6 @@ def __init__(self, codestr, astdict):
370355
self.pos_sync = LocationFixer(codestr, token_lines)
371356
self.codestr = codestr
372357

373-
# Some nodes (f-strings currently) must update the columns after the've done some
374-
# previous line number fixing, so this state member enable or disable the checking/fixing
375-
self._checkpos_enabled = True
376-
377358
# This will store a dict of nodes to end positions, it will be filled
378359
# on parse()
379360
self._node2endpos = None
@@ -453,9 +434,7 @@ def visit(self, node, root=False):
453434
meth = getattr(self, "visit_" + node_type, self.visit_other)
454435
visit_result = meth(node)
455436
self._add_noops(node, visit_result, root)
456-
457-
if self._checkpos_enabled:
458-
self.pos_sync.sync_node_pos(visit_result)
437+
self.pos_sync.sync_node_pos(visit_result)
459438

460439
if not self.codestr:
461440
# empty files are the only case where 0-indexes are allowed
@@ -574,27 +553,6 @@ def visit_other_field(self, node):
574553
# string attribute
575554
return node
576555

577-
def visit_FormattedValue(self, node):
578-
# Subcode inside the FormattedValue.value nodes have their lineno starting from 1,
579-
# so we'll fix that storing the FormattedValue lineno and adding it to the subcode
580-
# nodes lineno
581-
self._checkpos_enabled = False # it wouldn't work without correct lineno
582-
try:
583-
value_dict = self.pos_sync.fix_embeded_pos(self.visit(node["value"]),
584-
node["lineno"])
585-
fspec = node.get("format_spec")
586-
if fspec:
587-
fspec = self.visit(fspec)
588-
node.update({
589-
"conversion": node["conversion"],
590-
"format_spec": fspec,
591-
"value": value_dict,
592-
})
593-
finally:
594-
self._checkpos_enabled = True
595-
596-
return node
597-
598556

599557
if __name__ == '__main__':
600558
import sys

tests/string_fstring.py.native

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,10 @@
8080
"lineno": 3,
8181
"value": {
8282
"ast_type": "Name",
83-
"col_offset": 11,
83+
"col_offset": 2,
8484
"ctx": "Load",
85-
"end_col_offset": 52,
86-
"end_lineno": 3,
8785
"id": "a",
88-
"lineno": 3
86+
"lineno": 1
8987
}
9088
},
9189
{
@@ -124,12 +122,10 @@
124122
"lineno": 4,
125123
"value": {
126124
"ast_type": "Name",
127-
"col_offset": 17,
125+
"col_offset": 2,
128126
"ctx": "Load",
129-
"end_col_offset": 40,
130-
"end_lineno": 4,
131127
"id": "a",
132-
"lineno": 4
128+
"lineno": 1
133129
}
134130
},
135131
{
@@ -168,12 +164,10 @@
168164
"lineno": 5,
169165
"value": {
170166
"ast_type": "Name",
171-
"col_offset": 17,
167+
"col_offset": 2,
172168
"ctx": "Load",
173-
"end_col_offset": 36,
174-
"end_lineno": 5,
175169
"id": "a",
176-
"lineno": 5
170+
"lineno": 1
177171
}
178172
},
179173
{
@@ -212,12 +206,10 @@
212206
"lineno": 6,
213207
"value": {
214208
"ast_type": "Name",
215-
"col_offset": 17,
209+
"col_offset": 2,
216210
"ctx": "Load",
217-
"end_col_offset": 37,
218-
"end_lineno": 6,
219211
"id": "a",
220-
"lineno": 6
212+
"lineno": 1
221213
}
222214
},
223215
{
@@ -267,10 +259,8 @@
267259
"LiteralValue": 2,
268260
"NumType": "int",
269261
"ast_type": "NumLiteral",
270-
"col_offset": 20,
271-
"end_col_offset": 64,
272-
"end_lineno": 7,
273-
"lineno": 7
262+
"col_offset": 2,
263+
"lineno": 1
274264
}
275265
},
276266
{
@@ -291,23 +281,19 @@
291281
"LiteralValue": 3,
292282
"NumType": "int",
293283
"ast_type": "NumLiteral",
294-
"col_offset": 24,
295-
"end_col_offset": 64,
296-
"end_lineno": 7,
297-
"lineno": 7
284+
"col_offset": 2,
285+
"lineno": 1
298286
}
299287
}
300288
]
301289
},
302290
"lineno": 7,
303291
"value": {
304292
"ast_type": "Name",
305-
"col_offset": 17,
293+
"col_offset": 2,
306294
"ctx": "Load",
307-
"end_col_offset": 64,
308-
"end_lineno": 7,
309295
"id": "b",
310-
"lineno": 7
296+
"lineno": 1
311297
}
312298
},
313299
{
@@ -437,15 +423,13 @@
437423
"col_offset": 2,
438424
"func": {
439425
"ast_type": "Name",
440-
"col_offset": 17,
426+
"col_offset": 2,
441427
"ctx": "Load",
442-
"end_col_offset": 47,
443-
"end_lineno": 10,
444428
"id": "somefunc",
445-
"lineno": 10
429+
"lineno": 1
446430
},
447431
"keywords": [],
448-
"lineno": 10
432+
"lineno": 1
449433
}
450434
},
451435
{
@@ -489,22 +473,18 @@
489473
"func": {
490474
"ast_type": "Attribute",
491475
"attr": "upper",
492-
"col_offset": 23,
476+
"col_offset": 2,
493477
"ctx": "Load",
494-
"end_col_offset": 54,
495-
"end_lineno": 11,
496-
"lineno": 11,
478+
"lineno": 1,
497479
"value": {
498480
"LiteralValue": "pok",
499481
"ast_type": "StringLiteral",
500-
"col_offset": 18,
501-
"end_col_offset": 54,
502-
"end_lineno": 11,
503-
"lineno": 11
482+
"col_offset": 2,
483+
"lineno": 1
504484
}
505485
},
506486
"keywords": [],
507-
"lineno": 11
487+
"lineno": 1
508488
}
509489
},
510490
{

0 commit comments

Comments
 (0)