Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit68e4129

Browse files
authored
gh-83035: handle decorator with nested parens in inspect.getsource (#99654)
1 parentb11a384 commit68e4129

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

‎Lib/inspect.py

Lines changed: 1 addition & 9 deletions
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-
eliftoken=="(":
1179-
ifself.indecorator:
1180-
self.decoratorhasargs=True
1181-
eliftoken==")":
1182-
ifself.indecorator:
1183-
self.indecorator=False
1184-
self.decoratorhasargs=False
11851177
eliftype==tokenize.NEWLINE:
11861178
self.passline=False# stop skipping when a NEWLINE is seen
11871179
self.last=srowcol[0]
11881180
ifself.islambda:# lambdas always end at the first NEWLINE
11891181
raiseEndOfBlock
11901182
# hitting a NEWLINE when in a decorator without args
11911183
# ends the decorator
1192-
ifself.indecoratorandnotself.decoratorhasargs:
1184+
ifself.indecorator:
11931185
self.indecorator=False
11941186
elifself.passline:
11951187
pass

‎Lib/test/inspect_fodder2.py

Lines changed: 14 additions & 0 deletions
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
defall_markers_with_defaults(a,b=1,/,c=2,d=3,*,e=4,f=5):
261261
pass
262+
263+
# line 263
264+
defdeco_factory(**kwargs):
265+
defdeco(f):
266+
@wraps(f)
267+
defwrapper(*a,**kwd):
268+
kwd.update(kwargs)
269+
returnf(*a,**kwd)
270+
returnwrapper
271+
returndeco
272+
273+
@deco_factory(foo=(1+2),bar=lambda:1)
274+
defcomplex_decorated(foo=0,bar=lambda:0):
275+
returnfoo+bar()

‎Lib/test/test_inspect.py

Lines changed: 6 additions & 0 deletions
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+
classTestComplexDecorator(GetSourceBase):
890+
fodderModule=mod2
891+
892+
deftest_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.
Lines changed: 1 addition & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp