Skip to content

Commit 68e4129

Browse files
authored
gh-83035: handle decorator with nested parens in inspect.getsource (#99654)
1 parent b11a384 commit 68e4129

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

Lib/inspect.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,6 @@ def __init__(self):
11601160
self.started = False
11611161
self.passline = False
11621162
self.indecorator = False
1163-
self.decoratorhasargs = False
11641163
self.last = 1
11651164
self.body_col0 = None
11661165

@@ -1175,21 +1174,14 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
11751174
self.islambda = True
11761175
self.started = True
11771176
self.passline = True # skip to the end of the line
1178-
elif token == "(":
1179-
if self.indecorator:
1180-
self.decoratorhasargs = True
1181-
elif token == ")":
1182-
if self.indecorator:
1183-
self.indecorator = False
1184-
self.decoratorhasargs = False
11851177
elif type == tokenize.NEWLINE:
11861178
self.passline = False # stop skipping when a NEWLINE is seen
11871179
self.last = srowcol[0]
11881180
if self.islambda: # lambdas always end at the first NEWLINE
11891181
raise EndOfBlock
11901182
# hitting a NEWLINE when in a decorator without args
11911183
# ends the decorator
1192-
if self.indecorator and not self.decoratorhasargs:
1184+
if self.indecorator:
11931185
self.indecorator = False
11941186
elif self.passline:
11951187
pass

Lib/test/inspect_fodder2.py

+14
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,17 @@ def all_markers_with_args_and_kwargs(a, b, /, c, d, *args, e, f, **kwargs):
259259
#line 259
260260
def all_markers_with_defaults(a, b=1, /, c=2, d=3, *, e=4, f=5):
261261
pass
262+
263+
# line 263
264+
def deco_factory(**kwargs):
265+
def deco(f):
266+
@wraps(f)
267+
def wrapper(*a, **kwd):
268+
kwd.update(kwargs)
269+
return f(*a, **kwd)
270+
return wrapper
271+
return deco
272+
273+
@deco_factory(foo=(1 + 2), bar=lambda: 1)
274+
def complex_decorated(foo=0, bar=lambda: 0):
275+
return foo + bar()

Lib/test/test_inspect.py

+6
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,12 @@ def test_class(self):
886886
self.assertSourceEqual(self.fodderModule.X, 1, 2)
887887

888888

889+
class TestComplexDecorator(GetSourceBase):
890+
fodderModule = mod2
891+
892+
def test_parens_in_decorator(self):
893+
self.assertSourceEqual(self.fodderModule.complex_decorated, 273, 275)
894+
889895
class _BrokenDataDescriptor(object):
890896
"""
891897
A broken data descriptor. See bug #1785.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`inspect.getsource` handling of decorator calls with nested parentheses.

0 commit comments

Comments
 (0)