diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2014-09-26 21:34:54 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2014-09-26 21:34:54 (GMT) |
commit | 081bbf6b28868774c1abca0a8469a517abb9f6cc (patch) | |
tree | 5a91ca676029cceeda75c4a73d2afe77696b4301 | |
parent | 2c0a91606105e1606d886c198ea7ed1b195c692f (diff) | |
download | cpython-081bbf6b28868774c1abca0a8469a517abb9f6cc.zip cpython-081bbf6b28868774c1abca0a8469a517abb9f6cc.tar.gz cpython-081bbf6b28868774c1abca0a8469a517abb9f6cc.tar.bz2 |
inspect: Fix getsource() to support decorated functions.
Issue #1764286. Patch by Claudiu Popa.
-rw-r--r-- | Lib/inspect.py | 1 | ||||
-rw-r--r-- | Lib/test/inspect_fodder2.py | 13 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 3 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 20 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 819bb8d..ae55347 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -817,6 +817,7 @@ def getsourcelines(object): corresponding to the object and the line number indicates where in the original source file the first line of code was found. An OSError is raised if the source code cannot be retrieved.""" + object = unwrap(object) lines, lnum = findsource(object) if ismodule(object): return lines, 0 diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py index bd7106f..e452235 100644 --- a/Lib/test/inspect_fodder2.py +++ b/Lib/test/inspect_fodder2.py @@ -109,3 +109,16 @@ def annotated(arg1: list): #line 109 def keyword_only_arg(*, arg): pass + +from functools import wraps + +def decorator(func): + @wraps(func) + def fake(): + return 42 + return fake + +#line 121 +@decorator +def real(): + return 20 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 6da5822..1da88c4 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -377,6 +377,9 @@ class TestDecorators(GetSourceBase): def test_replacing_decorator(self): self.assertSourceEqual(mod2.gone, 9, 10) + def test_getsource_unwrap(self): + self.assertSourceEqual(mod2.real, 122, 124) + class TestOneliners(GetSourceBase): fodderModule = mod2 def test_oneline_lambda(self): @@ -10,6 +10,9 @@ Release date: TBA Core and Builtins ----------------- +- Issue #1764286: Fix inspect.getsource() to support decorated functions. + Patch by Claudiu Popa. + - Issue #18554: os.__all__ includes posix functions. - Issue #21391: Use os.path.abspath in the shutil module. |