This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getsource() returns incorrect source lines at the module level
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Aivar.Annamaa, Claudiu.Popa, ajaksu2, ggenellina, jamesls, miss-islington, taleinat, yselivanov
Priority: normal Keywords: patch

Created on 2009-08-14 04:19 by ggenellina, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
inspect.diff ggenellina, 2009-08-14 04:19 patch against trunk review
show_inspect_bug.py ggenellina, 2009-08-14 04:29
issue6700.patch jamesls, 2013-05-23 02:56 review
Pull Requests
URL Status Linked Edit
PR 8864 merged v2m, 2018-08-23 00:34
PR 8898 merged miss-islington, 2018-08-24 14:18
PR 8899 merged miss-islington, 2018-08-24 14:18
PR 8900 merged taleinat, 2018-08-24 14:40
Messages (8)
msg91541 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-08-14 04:19
inspect.getsource(obj) returns incorrect results when obj is a 
traceback or frame object outside any function (that is, at the module 
level).

This demo script shows the problem. The correct output should contain 
all source lines in the module, but it only returns the first two:


D:\temp>type show_inspect_bug.py
def foo(x):
  return y + z

import inspect
frame = inspect.currentframe()
print inspect.getsource(frame)


D:\temp>python show_inspect_bug.py
def foo(x):
  return y + z


The attached patch fixes the problem and adds some missing test cases.
Originally reported by Juanjo Conti at the local Python group.
msg102651 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2010-04-08 22:59
Confirmed in trunk and py3k. Also affects inspect.getsourcelines.
msg189842 - (view) Author: James Saryerwinnie (jamesls) * Date: 2013-05-23 02:56
I confirmed the issue in tip.  One of the issues with the original patch is
that it modifies the tokeneater method used by getblock which won't work
if the first token is any of the special cased tokens in the original patch
('@', 'def', 'class').  I've added additional tests that show where the
original patch fails.

An alternative approach is to check in getsourcelines whether or not we're
dealing with a traceback or frame object in the module scope, and if so,
return the lines of the entire module.  I've attached an updated patch
that implements this along with additional tests.
msg324000 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-08-24 14:18
New changeset 91cb298f811961277fd4cc4a32211899d48bedcb by Tal Einat (Vladimir Matveev) in branch 'master':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
https://github.com/python/cpython/commit/91cb298f811961277fd4cc4a32211899d48bedcb
msg324002 - (view) Author: miss-islington (miss-islington) Date: 2018-08-24 14:41
New changeset 0e707b4c6a47086d8e723c7a010c3f5cf8296946 by Miss Islington (bot) in branch '3.6':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
https://github.com/python/cpython/commit/0e707b4c6a47086d8e723c7a010c3f5cf8296946
msg324004 - (view) Author: miss-islington (miss-islington) Date: 2018-08-24 14:44
New changeset 3e6020c4ddf7acea91efdae770320c6ce06b93db by Miss Islington (bot) in branch '3.7':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
https://github.com/python/cpython/commit/3e6020c4ddf7acea91efdae770320c6ce06b93db
msg324114 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-08-26 08:45
New changeset 491740f116755e220135e596ec802ea3a0f65596 by Tal Einat in branch '2.7':
[2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
https://github.com/python/cpython/commit/491740f116755e220135e596ec802ea3a0f65596
msg324115 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-08-26 08:47
Thanks for reporting this, Gabriel!

Thanks for the PR, Vladimir!
History
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50949
2018-08-26 08:47:22taleinatsetstatus: open -> closed
resolution: fixed
messages: + msg324115

stage: patch review -> resolved
2018-08-26 08:45:04taleinatsetmessages: + msg324114
2018-08-24 14:44:50miss-islingtonsetmessages: + msg324004
2018-08-24 14:41:43miss-islingtonsetnosy: + miss-islington
messages: + msg324002
2018-08-24 14:40:15taleinatsetpull_requests: + pull_request8371
2018-08-24 14:18:20miss-islingtonsetpull_requests: + pull_request8370
2018-08-24 14:18:14miss-islingtonsetpull_requests: + pull_request8369
2018-08-24 14:18:08taleinatsetnosy: + taleinat
messages: + msg324000
2018-08-23 09:40:09taleinatsetversions: + Python 2.7
2018-08-23 08:51:55taleinatsetversions: + Python 3.8, - Python 3.5
2018-08-23 00:34:56v2msetpull_requests: + pull_request8338
2018-08-17 04:21:47Aivar.Annamaasetversions: + Python 3.6, Python 3.7
title: inspect.getsource() returns incorrect source lines -> inspect.getsource() returns incorrect source lines at the module level
2018-08-17 04:19:52Aivar.Annamaasetnosy: + Aivar.Annamaa
2015-01-17 15:58:05Claudiu.Popasetnosy: + Claudiu.Popa, yselivanov

versions: + Python 3.5, - Python 2.6, Python 3.1, Python 2.7, Python 3.2
2013-05-23 02:56:40jameslssetfiles: + issue6700.patch
nosy: + jamesls
messages: + msg189842

2010-04-08 22:59:18ajaksu2setpriority: normal

nosy: + ajaksu2
messages: + msg102651

stage: patch review
2009-08-14 04:29:20ggenellinasetfiles: + show_inspect_bug.py
2009-08-14 04:19:21ggenellinacreate