summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2022-12-07 16:55:12 (GMT)
committerGitHub <noreply@github.com>2022-12-07 16:55:12 (GMT)
commit68e41295b8611a990de68f15c89f1eb3dea51867 (patch)
treeea571fbecdd8cd05033707ba23da586c1cc5338f /Lib
parentb11a384dc7471ffc16de4b86e8f5fdeef151f348 (diff)
downloadcpython-68e41295b8611a990de68f15c89f1eb3dea51867.zip
cpython-68e41295b8611a990de68f15c89f1eb3dea51867.tar.gz
cpython-68e41295b8611a990de68f15c89f1eb3dea51867.tar.bz2
gh-83035: handle decorator with nested parens in inspect.getsource (#99654)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/inspect.py10
-rw-r--r--Lib/test/inspect_fodder2.py14
-rw-r--r--Lib/test/test_inspect.py6
3 files changed, 21 insertions, 9 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 31ac888..e165937 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1160,7 +1160,6 @@ class BlockFinder:
self.started = False
self.passline = False
self.indecorator = False
- self.decoratorhasargs = False
self.last = 1
self.body_col0 = None
@@ -1175,13 +1174,6 @@ class BlockFinder:
self.islambda = True
self.started = True
self.passline = True # skip to the end of the line
- elif token == "(":
- if self.indecorator:
- self.decoratorhasargs = True
- elif token == ")":
- if self.indecorator:
- self.indecorator = False
- self.decoratorhasargs = False
elif type == tokenize.NEWLINE:
self.passline = False # stop skipping when a NEWLINE is seen
self.last = srowcol[0]
@@ -1189,7 +1181,7 @@ class BlockFinder:
raise EndOfBlock
# hitting a NEWLINE when in a decorator without args
# ends the decorator
- if self.indecorator and not self.decoratorhasargs:
+ if self.indecorator:
self.indecorator = False
elif self.passline:
pass
diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py
index e7d4b53..2dc4981 100644
--- a/Lib/test/inspect_fodder2.py
+++ b/Lib/test/inspect_fodder2.py
@@ -259,3 +259,17 @@ def all_markers_with_args_and_kwargs(a, b, /, c, d, *args, e, f, **kwargs):
#line 259
def all_markers_with_defaults(a, b=1, /, c=2, d=3, *, e=4, f=5):
pass
+
+# line 263
+def deco_factory(**kwargs):
+ def deco(f):
+ @wraps(f)
+ def wrapper(*a, **kwd):
+ kwd.update(kwargs)
+ return f(*a, **kwd)
+ return wrapper
+ return deco
+
+@deco_factory(foo=(1 + 2), bar=lambda: 1)
+def complex_decorated(foo=0, bar=lambda: 0):
+ return foo + bar()
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 3f5c299..2b7977b 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -886,6 +886,12 @@ class TestNoEOL(GetSourceBase):
self.assertSourceEqual(self.fodderModule.X, 1, 2)
+class TestComplexDecorator(GetSourceBase):
+ fodderModule = mod2
+
+ def test_parens_in_decorator(self):
+ self.assertSourceEqual(self.fodderModule.complex_decorated, 273, 275)
+
class _BrokenDataDescriptor(object):
"""
A broken data descriptor. See bug #1785.