]> granicus.if.org Git - python/commitdiff
inspect: Fix getsource() to support decorated functions.
authorYury Selivanov <yselivanov@sprymix.com>
Fri, 26 Sep 2014 21:34:54 +0000 (17:34 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Fri, 26 Sep 2014 21:34:54 +0000 (17:34 -0400)
Issue #1764286. Patch by Claudiu Popa.

Lib/inspect.py
Lib/test/inspect_fodder2.py
Lib/test/test_inspect.py
Misc/NEWS

index 819bb8de2d5b1f1255166c2089f101e6c1198454..ae55347adc145f1a2854a089e42bba7b8ef95e27 100644 (file)
@@ -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
index bd7106fea86bebd8f4fff28bf1339089ab78802e..e452235cd8e53fe421bf91c95a47270185290843 100644 (file)
@@ -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
index 6da58227689a0b7f55af6daa74a52949daa1a6dc..1da88c4ddb461e86279f1bb67be027207210f424 100644 (file)
@@ -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):
index ad539e39543df936fbd85472a3b4c6cce434791a..47fbafa70c0939c830668a370507db52c17b74b6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.