(original) (raw)
changeset: 92593:ad9cc6124a19 user: Yury Selivanov yselivanov@sprymix.com date: Fri Sep 26 17:34:54 2014 -0400 files: Lib/inspect.py Lib/test/inspect_fodder2.py Lib/test/test_inspect.py Misc/NEWS description: inspect: Fix getsource() to support decorated functions. Issue #1764286. Patch by Claudiu Popa. diff -r bbe57429eba0 -r ad9cc6124a19 Lib/inspect.py --- a/Lib/inspect.py Fri Sep 26 23:31:59 2014 +0200 +++ b/Lib/inspect.py Fri Sep 26 17:34:54 2014 -0400 @@ -817,6 +817,7 @@ 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 -r bbe57429eba0 -r ad9cc6124a19 Lib/test/inspect_fodder2.py --- a/Lib/test/inspect_fodder2.py Fri Sep 26 23:31:59 2014 +0200 +++ b/Lib/test/inspect_fodder2.py Fri Sep 26 17:34:54 2014 -0400 @@ -109,3 +109,16 @@ #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 -r bbe57429eba0 -r ad9cc6124a19 Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py Fri Sep 26 23:31:59 2014 +0200 +++ b/Lib/test/test_inspect.py Fri Sep 26 17:34:54 2014 -0400 @@ -377,6 +377,9 @@ 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): diff -r bbe57429eba0 -r ad9cc6124a19 Misc/NEWS --- a/Misc/NEWS Fri Sep 26 23:31:59 2014 +0200 +++ b/Misc/NEWS Fri Sep 26 17:34:54 2014 -0400 @@ -10,6 +10,9 @@ 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. /yselivanov@sprymix.com