summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-08-24 14:41:39 (GMT)
committerGitHub <noreply@github.com>2018-08-24 14:41:39 (GMT)
commit0e707b4c6a47086d8e723c7a010c3f5cf8296946 (patch)
treea8c700ed5f1ebb1291d959724307a173a83dc567
parent902f1618388280eeeb3957b273f0ccb3df938723 (diff)
downloadcpython-0e707b4c6a47086d8e723c7a010c3f5cf8296946.zip
cpython-0e707b4c6a47086d8e723c7a010c3f5cf8296946.tar.gz
cpython-0e707b4c6a47086d8e723c7a010c3f5cf8296946.tar.bz2
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
(cherry picked from commit 91cb298f811961277fd4cc4a32211899d48bedcb) Co-authored-by: Vladimir Matveev <v2matveev@outlook.com>
-rw-r--r--Lib/inspect.py7
-rw-r--r--Lib/test/inspect_fodder.py6
-rw-r--r--Lib/test/test_inspect.py14
-rw-r--r--Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst2
4 files changed, 26 insertions, 3 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 362ae9a..227f6b8 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -954,7 +954,12 @@ def getsourcelines(object):
object = unwrap(object)
lines, lnum = findsource(object)
- if ismodule(object):
+ if istraceback(object):
+ object = object.tb_frame
+
+ # for module or frame that corresponds to module, return all source lines
+ if (ismodule(object) or
+ (isframe(object) and object.f_code.co_name == "<module>")):
return lines, 0
else:
return getblock(lines[lnum:]), lnum + 1
diff --git a/Lib/test/inspect_fodder.py b/Lib/test/inspect_fodder.py
index 711bada..ff3f0e4 100644
--- a/Lib/test/inspect_fodder.py
+++ b/Lib/test/inspect_fodder.py
@@ -74,3 +74,9 @@ class FesteringGob(MalodorousPervert, ParrotDroppings):
async def lobbest(grenade):
pass
+
+currentframe = inspect.currentframe()
+try:
+ raise Exception()
+except:
+ tb = sys.exc_info()[2]
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index d64bd45..b968ae6 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -322,7 +322,7 @@ class GetSourceBase(unittest.TestCase):
def sourcerange(self, top, bottom):
lines = self.source.split("\n")
- return "\n".join(lines[top-1:bottom]) + "\n"
+ return "\n".join(lines[top-1:bottom]) + ("\n" if bottom else "")
def assertSourceEqual(self, obj, top, bottom):
self.assertEqual(inspect.getsource(obj),
@@ -497,6 +497,16 @@ class TestRetrievingSourceCode(GetSourceBase):
def test_getsource_on_code_object(self):
self.assertSourceEqual(mod.eggs.__code__, 12, 18)
+class TestGettingSourceOfToplevelFrames(GetSourceBase):
+ fodderModule = mod
+
+ def test_range_toplevel_frame(self):
+ self.maxDiff = None
+ self.assertSourceEqual(mod.currentframe, 1, None)
+
+ def test_range_traceback_toplevel_frame(self):
+ self.assertSourceEqual(mod.tb, 1, None)
+
class TestDecorators(GetSourceBase):
fodderModule = mod2
@@ -3747,7 +3757,7 @@ def test_main():
TestBoundArguments, TestSignaturePrivateHelpers,
TestSignatureDefinitions,
TestGetClosureVars, TestUnwrap, TestMain, TestReload,
- TestGetCoroutineState
+ TestGetCoroutineState, TestGettingSourceOfToplevelFrames
)
if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst
new file mode 100644
index 0000000..d95c737
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst
@@ -0,0 +1,2 @@
+Fix inspect.getsourcelines for module level frames/tracebacks.
+Patch by Vladimir Matveev.