Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f86d1fd

Browse files
committed
merge 3.4 (#21295)
2 parents 0e259f1 + 7a66fc2 commit f86d1fd

4 files changed

Lines changed: 379 additions & 402 deletions

File tree

Lib/test/test_ast.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -180,36 +180,20 @@ def to_tuple(t):
180180

181181
class AST_Tests(unittest.TestCase):
182182

183-
def _assertTrueorder(self, ast_node, parent_pos, reverse_check = False):
184-
def should_reverse_check(parent, child):
185-
# In some situations, the children of nodes occur before
186-
# their parents, for example in a.b.c, a occurs before b
187-
# but a is a child of b.
188-
if isinstance(parent, ast.Call):
189-
if parent.func == child:
190-
return True
191-
if isinstance(parent, (ast.Attribute, ast.Subscript)):
192-
return True
193-
return False
194-
183+
def _assertTrueorder(self, ast_node, parent_pos):
195184
if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
196185
return
197186
if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
198187
node_pos = (ast_node.lineno, ast_node.col_offset)
199-
if reverse_check:
200-
self.assertTrue(node_pos <= parent_pos)
201-
else:
202-
self.assertTrue(node_pos >= parent_pos)
188+
self.assertTrue(node_pos >= parent_pos)
203189
parent_pos = (ast_node.lineno, ast_node.col_offset)
204190
for name in ast_node._fields:
205191
value = getattr(ast_node, name)
206192
if isinstance(value, list):
207193
for child in value:
208-
self._assertTrueorder(child, parent_pos,
209-
should_reverse_check(ast_node, child))
194+
self._assertTrueorder(child, parent_pos)
210195
elif value is not None:
211-
self._assertTrueorder(value, parent_pos,
212-
should_reverse_check(ast_node, value))
196+
self._assertTrueorder(value, parent_pos)
213197

214198
def test_AST_objects(self):
215199
x = ast.AST()
@@ -278,9 +262,8 @@ def test_field_attr_existence(self):
278262

279263
def test_arguments(self):
280264
x = ast.arguments()
281-
self.assertEqual(x._fields, ('args', 'vararg',
282-
'kwonlyargs', 'kw_defaults',
283-
'kwarg', 'defaults'))
265+
self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs',
266+
'kw_defaults', 'kwarg', 'defaults'))
284267

285268
with self.assertRaises(AttributeError):
286269
x.vararg
@@ -455,7 +438,7 @@ def test_dump(self):
455438
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
456439
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
457440
"col_offset=11)], keywords=[], starargs=None, kwargs=None, "
458-
"lineno=1, col_offset=4), lineno=1, col_offset=0)])"
441+
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
459442
)
460443

461444
def test_copy_location(self):
@@ -476,7 +459,7 @@ def test_fix_missing_locations(self):
476459
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
477460
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
478461
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
479-
"lineno=1, col_offset=5), lineno=1, col_offset=0), "
462+
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
480463
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
481464
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
482465
"keywords=[], starargs=None, kwargs=None, lineno=1, "
@@ -973,7 +956,7 @@ def main():
973956
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
974957
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
975958
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
976-
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 58))], [], None)]),
959+
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 58))], [], None)]),
977960
('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
978961
('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], None, None, [('Pass', (1, 17))], [])]),
979962
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
@@ -985,7 +968,7 @@ def main():
985968
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
986969
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
987970
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))])]),
988-
('Module', [('Raise', (1, 0), ('Call', (1, 15), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
971+
('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
989972
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
990973
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
991974
('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
@@ -1022,17 +1005,17 @@ def main():
10221005
('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
10231006
('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
10241007
('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
1025-
('Expression', ('Call', (1, 1), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
1008+
('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
10261009
('Expression', ('Num', (1, 0), 10)),
10271010
('Expression', ('Str', (1, 0), 'string')),
1028-
('Expression', ('Attribute', (1, 2), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1029-
('Expression', ('Subscript', (1, 2), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
1011+
('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1012+
('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
10301013
('Expression', ('Name', (1, 0), 'v', ('Load',))),
10311014
('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
10321015
('Expression', ('List', (1, 0), [], ('Load',))),
10331016
('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
10341017
('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
10351018
('Expression', ('Tuple', (1, 0), [], ('Load',))),
1036-
('Expression', ('Call', (1, 7), ('Attribute', (1, 6), ('Attribute', (1, 4), ('Attribute', (1, 2), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 12), ('Attribute', (1, 10), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
1019+
('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
10371020
]
10381021
main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #21295: Revert some changes (issue #16795) to AST line numbers and
14+
column offsets that constituted a regression.
15+
1316
- Issue #22986: Allow changing an object's __class__ between a dynamic type and
1417
static type in some cases.
1518

Python/ast.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ ast_for_arg(struct compiling *c, const node *n)
11271127
identifier name;
11281128
expr_ty annotation = NULL;
11291129
node *ch;
1130-
arg_ty tmp;
1130+
arg_ty ret;
11311131

11321132
assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
11331133
ch = CHILD(n, 0);
@@ -1143,13 +1143,12 @@ ast_for_arg(struct compiling *c, const node *n)
11431143
return NULL;
11441144
}
11451145

1146-
tmp = arg(name, annotation, c->c_arena);
1147-
if (!tmp)
1146+
ret = arg(name, annotation, c->c_arena);
1147+
if (!ret)
11481148
return NULL;
1149-
1150-
tmp->lineno = LINENO(n);
1151-
tmp->col_offset = n->n_col_offset;
1152-
return tmp;
1149+
ret->lineno = LINENO(n);
1150+
ret->col_offset = n->n_col_offset;
1151+
return ret;
11531152
}
11541153

11551154
/* returns -1 if failed to handle keyword only arguments
@@ -2107,22 +2106,15 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
21072106
if (NCH(n) == 2)
21082107
return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
21092108
n->n_col_offset, c->c_arena);
2110-
else {
2111-
expr_ty tmp = ast_for_call(c, CHILD(n, 1), left_expr);
2112-
if (!tmp)
2113-
return NULL;
2114-
2115-
tmp->lineno = LINENO(n);
2116-
tmp->col_offset = n->n_col_offset;
2117-
return tmp;
2118-
}
2109+
else
2110+
return ast_for_call(c, CHILD(n, 1), left_expr);
21192111
}
2120-
else if (TYPE(CHILD(n, 0)) == DOT ) {
2112+
else if (TYPE(CHILD(n, 0)) == DOT) {
21212113
PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
21222114
if (!attr_id)
21232115
return NULL;
21242116
return Attribute(left_expr, attr_id, Load,
2125-
LINENO(CHILD(n, 1)), CHILD(n, 1)->n_col_offset, c->c_arena);
2117+
LINENO(n), n->n_col_offset, c->c_arena);
21262118
}
21272119
else {
21282120
REQ(CHILD(n, 0), LSQB);
@@ -2223,16 +2215,15 @@ ast_for_power(struct compiling *c, const node *n)
22232215
tmp = ast_for_trailer(c, ch, e);
22242216
if (!tmp)
22252217
return NULL;
2218+
tmp->lineno = e->lineno;
2219+
tmp->col_offset = e->col_offset;
22262220
e = tmp;
22272221
}
22282222
if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
22292223
expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
22302224
if (!f)
22312225
return NULL;
2232-
tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
2233-
if (!tmp)
2234-
return NULL;
2235-
e = tmp;
2226+
e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
22362227
}
22372228
return e;
22382229
}

0 commit comments

Comments
 (0)