[2.7] 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>
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 0a6cfd7..cbced17 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -688,8 +688,15 @@
raised if the source code cannot be retrieved."""
lines, lnum = findsource(object)
- if ismodule(object): return lines, 0
- else: return getblock(lines[lnum:]), lnum + 1
+ 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
def getsource(object):
"""Return the text of the source code for an object.