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

Skip to content

bpo-38854: Adjust inspect.getsource to properly extract source for decorated functions #17374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ def __init__(self):
self.indecorator = False
self.decoratorhasargs = False
self.last = 1
self.decoratorparens = []

def tokeneater(self, type, token, srowcol, erowcol, line):
if not self.started and not self.indecorator:
Expand All @@ -913,11 +914,18 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
self.passline = True # skip to the end of the line
elif token == "(":
if self.indecorator:
self.decoratorparens.append(token)
self.decoratorhasargs = True
elif token == ")":
if self.indecorator:
self.indecorator = False
self.decoratorhasargs = False
if self.decoratorparens:
if self.decoratorparens[-1] == "(":
self.decoratorparens = self.decoratorparens[:-1]
if not self.decoratorparens:
self.indecorator = False
self.decoratorhasargs = False
else:
self.decoratorparens.append(token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Why) Is it necessary to keep track of the tokens?
(came here via #21425, which only uses a count)

elif type == tokenize.NEWLINE:
self.passline = False # stop skipping when a NEWLINE is seen
self.last = srowcol[0]
Expand Down
31 changes: 30 additions & 1 deletion Lib/test/inspect_fodder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# line 1
'A module docstring.'

import sys, inspect
import sys, collections, functools, inspect
# line 5

# line 7
Expand Down Expand Up @@ -91,3 +91,32 @@ def as_method_of(self, obj):

custom_method = Callable().as_method_of(42)
del Callable


def decorator(*args):
def inner(func):
@functools.wraps(func)
def wrapper():
pass
return wrapper
return inner


@decorator(dict(), 24)
@decorator(dict(), 1)
@decorator(dict(), collections.defaultdict(lambda: 1))
@decorator("string containing )", "other string ()")
@decorator(
(()),
collections.defaultdict(lambda: 1),
[(i, j) for i, j in enumerate(range(5))],
)
def decorated():
local_var = 2
return local_var + 42


@decorator()
def other_decorated():
local_var = 2
return local_var + 42
7 changes: 6 additions & 1 deletion Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,11 @@ def test_getclasses(self):

def test_getfunctions(self):
functions = inspect.getmembers(mod, inspect.isfunction)
self.assertEqual(functions, [('eggs', mod.eggs),
self.assertEqual(functions, [('decorated', mod.decorated),
('decorator', mod.decorator),
('eggs', mod.eggs),
('lobbest', mod.lobbest),
('other_decorated', mod.other_decorated),
('spam', mod.spam)])

@unittest.skipIf(sys.flags.optimize >= 2,
Expand Down Expand Up @@ -493,6 +496,8 @@ def test_getsource(self):
self.assertSourceEqual(git.abuse, 29, 39)
self.assertSourceEqual(mod.StupidGit, 21, 51)
self.assertSourceEqual(mod.lobbest, 75, 76)
self.assertSourceEqual(mod.decorated, 105, 116)
self.assertSourceEqual(mod.other_decorated, 119, 122)

def test_getsourcefile(self):
self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Properly extract source with ``inspect.getsource`` for decorated functions.

Patch by Claudiu Popa