Skip to content

PYTHONSTARTUP causing traceback source mismatch in interactive console #139244

@CNSeniorious000

Description

@CNSeniorious000

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():

  1. pymain_run_startup() calls _PyRun_SimpleFileObject(fp, startup, 0, &cf)
  2. _PyRun_SimpleFileObject() in Python/pythonrun.c sets __loader__ via set_main_loader(dict, filename, "SourceFileLoader")
  3. The cleanup code only removes __file__ and __cached__ but not __loader__:

    cpython/Python/pythonrun.c

    Lines 533 to 543 in 04c4628

    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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions