Skip to content

IDLE fails to start or raises when an editor window's file does not exist #153480

Description

@tonghuaroot

Bug report

Bug description:

IDLE raises an uncaught traceback when a file that is open in the editor is deleted or renamed by another program and the IDLE window is then refocused.

EditorWindow.focus_in_event runs on every <FocusIn> event and calls EditorWindow.last_mtime, which calls os.path.getmtime(self.io.filename) with no error handling. When the open file has been removed or renamed, getmtime raises FileNotFoundError, which escapes the Tk event handler and breaks the modified-on-disk reload check.

Reproduction

Open a file in the IDLE editor, delete or rename it in another program, then click back into the IDLE window. A traceback like this reaches the console and the reload check stops working:

FileNotFoundError: [Errno 2] No such file or directory: '.../somefile.py'

The same call reproduced without a GUI:

import os, tempfile, types
from idlelib.editor import EditorWindow

with tempfile.TemporaryDirectory() as d:
    path = os.path.join(d, 'gone.py')
    open(path, 'w').close()
    mtime = os.path.getmtime(path)
    os.remove(path)
    stub = types.SimpleNamespace(io=types.SimpleNamespace(filename=path), mtime=mtime)
    EditorWindow.last_mtime(stub)   # raises FileNotFoundError on the main branch

Suggested fix

Guard os.path.getmtime in last_mtime with try/except OSError and return the last known mtime, so focus_in_event sees no change and does not prompt to reload a file that is gone.

CPython versions tested on:

3.16, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. terryjreedy commented on Jul 10, 2026

    @terryjreedy
    Member

    I choose the IDLE Issues topic "File save" as the closest because this is not about editing but about interaction with the external filesystem. I suspect that there could also be problems if Shell or an output window have been 'saved as'.

  2. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Aug 7, 2026
  3. moved this to In Progress in IDLE Issueson Aug 7, 2026
  4. serhiy-storchaka commented on Sep 15, 2026

    @serhiy-storchaka
    Member

    This is worse than a traceback on FocusIn: the same last_mtime() runs from set_saved(True) inside EditorWindow.__init__, so opening a file that does not exist yet — python -m idlelib new.py, the usual way to start a new script — aborts IDLE at startup with FileNotFoundError. Regression from gh-94523; affects 3.13.15, 3.14.7, 3.15.0b1 and main.

    Traceback: pyshell.main → filelist.open → EditorWindow.__init__ (editor.py:242) → iomenu.set_filename → set_saved → editor.set_saved (1033) → last_mtime (1121) → os.path.getmtime.

    Proposed behavior: last_mtime() returns None when there is no file or it cannot be stat'ed (not 0, which is a valid mtime on filesystems without timestamps). If a file that was there is gone on FocusIn, ask Close / Save As / Ignore (default Save As); Ignore marks the window unsaved and resets the stored mtime to None so the question is asked once. This is an approximate plan; details can be changed later. See the review on #153481.

  5. changed the title [-]IDLE crashes when a file open in the editor is deleted or renamed externally[/-] [+]IDLE fails to start or raises when an editor window's file does not exist[/+] on Sep 15, 2026
  6. added
    3.14bugs and security fixes
    3.15pre-release feature fixes, bugs and security fixes
    on Sep 15, 2026
  7. added 3 commits that reference this issue on Sep 17, 2026
  8. added 3 commits that reference this issue on Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-IDLEtype-bugAn unexpected behavior, bug, or errortype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions