Bug report
Bug description:
When PYTHONSTARTUP environment variable is set, the startup file execution permanently leaves a __loader__ attribute in the __main__ module's __dict__. This causes linecache to use __loader__.get_source() for source code retrieval, resulting in tracebacks in the interactive console incorrectly showing source lines from the PYTHONSTARTUP file instead of the actual executed code.
The issue occurs in Modules/main.c in pymain_run_startup():
pymain_run_startup() calls _PyRun_SimpleFileObject(fp, startup, 0, &cf)
_PyRun_SimpleFileObject() in Python/pythonrun.c sets __loader__ via set_main_loader(dict, filename, "SourceFileLoader")
- The cleanup code only removes
__file__ and __cached__ but not __loader__:
|
done: |
|
if (set_file_name) { |
|
if (PyDict_PopString(dict, "__file__", NULL) < 0) { |
|
PyErr_Print(); |
|
} |
|
if (PyDict_PopString(dict, "__cached__", NULL) < 0) { |
|
PyErr_Print(); |
|
} |
|
} |
|
Py_XDECREF(main_module); |
|
return ret; |
CPython versions tested on:
3.13
Operating systems tested on:
Windows (It seems this issue only appears on Windows)
Bug report
Bug description:
When
PYTHONSTARTUPenvironment variable is set, the startup file execution permanently leaves a__loader__attribute in the__main__module's__dict__. This causes linecache to use__loader__.get_source()for source code retrieval, resulting in tracebacks in the interactive console incorrectly showing source lines from thePYTHONSTARTUPfile instead of the actual executed code.The issue occurs in
Modules/main.cinpymain_run_startup():pymain_run_startup()calls_PyRun_SimpleFileObject(fp, startup, 0, &cf)_PyRun_SimpleFileObject()inPython/pythonrun.csets__loader__viaset_main_loader(dict, filename, "SourceFileLoader")__file__and__cached__but not__loader__:cpython/Python/pythonrun.c
Lines 533 to 543 in 04c4628
CPython versions tested on:
3.13
Operating systems tested on:
Windows (It seems this issue only appears on Windows)