classification
Title: linecache cannot get source for the __main__ module with a custom loader
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eltoder, ncoghlan, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2020-10-23 05:26 by eltoder, last changed 2020-12-18 23:30 by eltoder.

Files
File name Uploaded Description Edit
t.zip eltoder, 2020-10-23 05:26
Pull Requests
URL Status Linked Edit
PR 22908 open python-dev, 2020-10-23 05:30
Messages (1)
msg379408 - (view) Author: Eugene Toder (eltoder) * Date: 2020-10-23 05:26
If a module has a loader, linecache calls its get_source() passing __name__ as the argument. This works most of the time, except that the __main__ module has it set to "__main__", which is commonly not the real name of the module. Luckily, we now have __spec__ which has the real name, so we can just use it.

Attached zip file reproduces the problem:
$ python t.zip
Traceback (most recent call last):
  ...
  File "t.zip/t.py", line 11, in <module>
  File "t.zip/t.py", line 8, in f
  File "t.zip/t.py", line 8, in f
  File "t.zip/t.py", line 8, in f
  [Previous line repeated 2 more times]
  File "t.zip/t.py", line 7, in f
ValueError

Note that entries from t.py don't have source code lines.
History
Date User Action Args
2020-12-18 23:30:30eltodersetnosy: + ncoghlan, vstinner
2020-12-16 22:00:43brett.cannonsetnosy: - brett.cannon
2020-12-16 03:07:02eltodersetnosy: + brett.cannon
2020-10-23 05:30:25python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request21839
stage: patch review
2020-10-23 05:26:53eltodercreate