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

Skip to content

Commit 081bbf6

Browse files
committed
inspect: Fix getsource() to support decorated functions.
Issue #1764286. Patch by Claudiu Popa.
1 parent 2c0a916 commit 081bbf6

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/inspect.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ def getsourcelines(object):
817817
corresponding to the object and the line number indicates where in the
818818
original source file the first line of code was found. An OSError is
819819
raised if the source code cannot be retrieved."""
820+
object = unwrap(object)
820821
lines, lnum = findsource(object)
821822

822823
if ismodule(object): return lines, 0

Lib/test/inspect_fodder2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,16 @@ def annotated(arg1: list):
109109
#line 109
110110
def keyword_only_arg(*, arg):
111111
pass
112+
113+
from functools import wraps
114+
115+
def decorator(func):
116+
@wraps(func)
117+
def fake():
118+
return 42
119+
return fake
120+
121+
#line 121
122+
@decorator
123+
def real():
124+
return 20

Lib/test/test_inspect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ def test_wrapped_decorator(self):
377377
def test_replacing_decorator(self):
378378
self.assertSourceEqual(mod2.gone, 9, 10)
379379

380+
def test_getsource_unwrap(self):
381+
self.assertSourceEqual(mod2.real, 122, 124)
382+
380383
class TestOneliners(GetSourceBase):
381384
fodderModule = mod2
382385
def test_oneline_lambda(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #1764286: Fix inspect.getsource() to support decorated functions.
14+
Patch by Claudiu Popa.
15+
1316
- Issue #18554: os.__all__ includes posix functions.
1417

1518
- Issue #21391: Use os.path.abspath in the shutil module.

0 commit comments

Comments
 (0)