8000 Fix regression with inner decorated nodes · python/cpython@afd3b7e · GitHub
[go: up one dir, main page]

Skip to content

Commit afd3b7e

Browse files
committed
Fix regression with inner decorated nodes
1 parent 44a3cd9 commit afd3b7e

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Other Language Changes
8080
* Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`.
8181
(Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.)
8282

83+
* Corrected the AST to give the actual line number of a decorated definition.
84+
The first line number of the code object still points to the first
85+
decorator.
86+
(Contributed by Ethan Smith in :issue:`33211`.)
87+
8388

8489
New Modules
8590
===========

Lib/test/inspect_fodder2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,15 @@ def func136():
137137
def func137():
138138
never_reached1
139139
never_reached2
140+
141+
#line 141
142+
def func142():
143+
@wrap
144+
def inner():
145+
pass
146+
147+
#line 147
148+
def func148():
149+
@wrap
150+
class A:
151+
pass

Lib/test/test_inspect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ def test_getsource_unwrap(self):
542542
def test_decorator_with_lambda(self):
543543
self.assertSourceEqual(mod2.func114, 113, 115)
544544

545+
def test_inner_decorated(self):
546+
self.assertSourceEqual(mod2.func142, 142, 145)
547+
self.assertSourceEqual(mod2.func148, 148, 151)
548+
545549
class TestOneliners(GetSourceBase):
546550
fodderModule = mod2
547551
def test_oneline_lambda(self):

Misc/ACKS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ Václav Šmilauer
15021502
Allen W. Smith
15031503
Christopher Smith
15041504
Eric V. Smith
1505-
Ethan H. Smith
1505+
Ethan Smith
15061506
Gregory P. Smith
15071507
Mark Smith
15081508
Nathaniel J. Smith

Python/ast.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,13 +1686,14 @@ ast_for_decorated(struct compiling *c, const node *n)
16861686

16871687
if (TYPE(CHILD(n, 1)) == funcdef) {
16881688
return ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1689-
} else if (TYPE(CHILD(n, 1)) == classdef) {
1689+
}
1690+
if (TYPE(CHILD(n, 1)) == classdef) {
16901691
return ast_for_classdef(c, CHILD(n, 1), decorator_seq);
1691-
} else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1692+
}
1693+
if (TYPE(CHILD(n, 1)) == async_funcdef) {
16921694
return ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
1693-
} else {
1694-
Py_UNREACHABLE();
16951695
}
1696+
Py_UNREACHABLE();
16961697
}
16971698

16981699
static expr_ty

Python/compile.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,6 @@ static int corrected_firstlineno(struct compiler *c, stmt_ty s,
19601960
*/
19611961
if (asdl_seq_LEN(decos)) {
19621962
expr_ty first_decorator = asdl_seq_GET(decos, 0);
1963-
c->u->u_firstlineno = first_decorator->lineno;
19641963
return first_decorator->lineno;
19651964
}
19661965
else {
@@ -2026,6 +2025,12 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
20262025
return 0;
20272026
}
20282027

2028+
// modify first line number for decorated nodes
2029+
if (asdl_seq_LEN(decos)) {
2030+
expr_ty first_decorator = asdl_seq_GET(decos, 0);
2031+
c->u->u_firstlineno = first_decorator->lineno;
2032+
}
2033+
20292034
/* if not -OO mode, add docstring */
20302035
if (c->c_optimize < 2 && s->v.FunctionDef.docstring)
20312036
docstring = s->v.FunctionDef.docstring;
@@ -2091,6 +2096,12 @@ compiler_class(struct compiler *c, stmt_ty s)
20912096
return 0;
20922097
/* this block represents what we do in the new scope */
20932098
{
2099+
// modify first line number for decorated nodes
2100+
if (asdl_seq_LEN(decos)) {
2101+
expr_ty first_decorator = asdl_seq_GET(decos, 0);
2102+
c->u->u_firstlineno = first_decorator->lineno;
2103+
}
2104+
20942105
/* use the class name for name mangling */
20952106
Py_INCREF(s->v.ClassDef.name);
20962107
Py_XSETREF(c->u->u_private, s->v.ClassDef.name);

Python/importlib.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ const unsigned char _Py_M__importlib[] = {
790790
114,114,0,0,0,218,8,112,114,111,112,101,114,116,121,114,
791791
112,0,0,0,218,6,115,101,116,116,101,114,114,119,0,0,
792792
0,114,113,0,0,0,114,10,0,0,0,114,10,0,0,0,
793-
114,10,0,0,0,114,11,0,0,0,114,102,0,0,0,172,
794-
1,0,0,115,18,0,0,0,12,197,4,1,14,11,8,10,
793+
114,10,0,0,0,114,11,0,0,0,114,102,0,0,0,76,
794+
1,0,0,115,18,0,0,0,12,37,4,1,14,11,8,10,
795795
8,13,12,9,14,4,12,8,12,4,114,102,0,0,0,41,
796796
2,114,103,0,0,0,114,105,0,0,0,99,2,0,0,0,
797797
2,0,0,0,6,0,0,0,8,0,0,0,67,0,0,0,
@@ -1196,8 +1196,8 @@ const unsigned char _Py_M__importlib[] = {
11961196
0,0,114,74,0,0,0,114,147,0,0,0,114,148,0,0,
11971197
0,114,105,0,0,0,114,84,0,0,0,114,137,0,0,0,
11981198
114,10,0,0,0,114,10,0,0,0,114,10,0,0,0,114,
1199-
11,0,0,0,114,141,0,0,0,253,2,0,0,115,18,0,
1200-
0,0,12,202,12,9,14,9,14,12,12,8,12,6,16,6,
1199+
11,0,0,0,114,141,0,0,0,189,2,0,0,115,18,0,
1200+
0,0,12,10,12,9,14,9,14,12,12,8,12,6,16,6,
12011201
16,6,16,4,114,141,0,0,0,99,0,0,0,0,0,0,
12021202
0,0,0,0,0,0,4,0,0,0,64,0,0,0,115,140,
12031203
0,0,0,101,0,90,1,100,0,90,2,100,1,90,3,101,
@@ -1340,7 +1340,7 @@ const unsigned char _Py_M__importlib[] = {
13401340
0,114,137,0,0,0,114,77,0,0,0,114,147,0,0,0,
13411341
114,148,0,0,0,114,105,0,0,0,114,10,0,0,0,114,
13421342
10,0,0,0,114,10,0,0,0,114,11,0,0,0,114,151,
1343-
0,0,0,74,3,0,0,115,18,0,0,0,12,198,12,9,
1343+
0,0,0,6,3,0,0,115,18,0,0,0,12,10,12,9,
13441344
14,7,14,9,12,4,12,9,12,10,16,6,16,6,114,151,
13451345
0,0,0,99,0,0,0,0,0,0,0,0,0,0,0,0,
13461346
2,0,0,0,64,0,0,0,115,32,0,0,0,101,0,90,

Python/importlib_external.h

Lines changed: 8 additions & 8 deletions
1241
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ const unsigned char _Py_M__importlib_external[] = {
10051005
116,104,111,100,114,173,0,0,0,114,179,0,0,0,114,182,
10061006
0,0,0,114,183,0,0,0,114,2,0,0,0,114,2,0,
10071007
0,0,114,2,0,0,0,114,4,0,0,0,114,170,0,0,
1008-
0,180,2,0,0,115,14,0,0,0,12,211,4,3,4,2,
1008+
0,130,2,0,0,115,14,0,0,0,12,5,4,3,4,2,
10091009
4,3,12,7,12,15,14,16,114,170,0,0,0,99,0,0,
10101010
0,0,0,0,0,0,0,0,0,0,2,0,0,0,64,0,
10111011
0,0,115,48,0,0,0,101,0,90,1,100,0,90,2,100,
@@ -1474,8 +1474,8 @@ const unsigned char _Py_M__importlib_external[] = {
14741474
0,0,114,223,0,0,0,114,228,0,0,0,90,13,95,95,
14751475
99,108,97,115,115,99,101,108,108,95,95,114,2,0,0,0,
14761476
114,2,0,0,0,41,1,114,213,0,0,0,114,4,0,0,
1477-
0,114,212,0,0,0,153,3,0,0,115,22,0,0,0,12,
1478-
219,8,6,8,4,8,4,16,12,12,4,8,8,12,5,8,
1477+
0,114,212,0,0,0,111,3,0,0,115,22,0,0,0,12,
1478+
5,8,6,8,4,8,4,16,12,12,4,8,8,12,5,8,
14791479
4,8,6,8,6,114,212,0,0,0,99,0,0,0,0,0,
14801480
0,0,0,0,0,0,0,3,0,0,0,64,0,0,0,115,
14811481
46,0,0,0,101,0,90,1,100,0,90,2,100,1,90,3,
@@ -1729,7 +1729,7 @@ const unsigned char _Py_M__importlib_external[] = {
17291729
0,0,0,114,161,0,0,0,114,188,0,0,0,114,202,0,
17301730
0,0,114,118,0,0,0,114,159,0,0,0,114,2,0,0,
17311731
0,114,2,0,0,0,114,2,0,0,0,114,4,0,0,0,
1732-
114,235,0,0,0,44,4,0,0,115,18,0,0,0,12,217,
1732+
114,235,0,0,0,253,3,0,0,115,18,0,0,0,12,8,
17331733
8,4,8,4,8,3,8,8,8,6,8,6,8,4,8,5,
17341734
114,235,0,0,0,99,0,0,0,0,0,0,0,0,0,0,
17351735
0,0,2,0,0,0,64,0,0,0,115,96,0,0,0,101,
@@ -1973,8 +1973,8 @@ const unsigned char _Py_M__importlib_external[] = {
19731973
0,114,4,1,0,0,114,161,0,0,0,114,202,0,0,0,
19741974
114,188,0,0,0,114,187,0,0,0,114,192,0,0,0,114,
19751975
194,0,0,0,114,2,0,0,0,114,2,0,0,0,114,2,
1976-
0,0,0,114,4,0,0,0,114,3,1,0,0,114,4,0,
1977-
0,115,16,0,0,0,8,253,8,4,12,8,8,3,8,3,
1976+
0,0,0,114,4,0,0,0,114,3,1,0,0,110,4,0,
1977+
0,115,16,0,0,0,8,1,8,4,12,8,8,3,8,3,
19781978
8,3,8,3,8,3,114,3,1,0,0,99,0,0,0,0,
19791979
0,0,0,0,0,0,0,0,4,0,0,0,64,0,0,0,
19801980
115,106,0,0,0,101,0,90,1,100,0,90,2,100,1,90,
@@ -2166,7 +2166,7 @@ const unsigned char _Py_M__importlib_external[] = {
21662166
114,14,1,0,0,114,15,1,0,0,114,18,1,0,0,114,
21672167
182,0,0,0,114,183,0,0,0,114,2,0,0,0,114,2,
21682168
0,0,0,114,2,0,0,0,114,4,0,0,0,114,5,1,
2169-
0,0,16,5,0,0,115,14,0,0,0,12,141,12,10,12,
2169+
0,0,152,4,0,0,115,14,0,0,0,12,5,12,10,12,
21702170
13,12,22,12,15,14,32,14,24,114,5,1,0,0,99,0,
21712171
0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,64,
21722172
0,0,0,115,90,0,0,0,101,0,90,1,100,0,90,2,
@@ -2430,7 +2430,7 @@ const unsigned char _Py_M__importlib_external[] = {
24302430
182,0,0,0,114,26,1,0,0,114,184,0,0,0,114,32,
24312431
1,0,0,114,0,1,0,0,114,2,0,0,0,114,2,0,
24322432
0,0,114,2,0,0,0,114,4,0,0,0,114,19,1,0,
2433-
0,155,5,0,0,115,18,0,0,0,12,140,8,14,8,4,
2433+
0,30,5,0,0,115,18,0,0,0,12,9,8,14,8,4,
24342434
4,2,8,12,8,5,10,48,8,32,12,17,114,19,1,0,
24352435
0,99,4,0,0,0,0,0,0,0,6,0,0,0,8,0,
24362436
0,0,67,0,0,0,115,146,0,0,0,124,0,160,0,100,

0 commit comments

Comments
 (0)
0