Repository navigation
IDLE fails to start or raises when an editor window's file does not exist #153480
Description
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 branchSuggested 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
- gh-153480: Stop IDLE crashing when a file open in the editor is deleted #153481
- [3.14] gh-153480: Stop IDLE crashing when a file open in the editor is deleted (GH-153481) #158262
- [3.13] gh-153480: Stop IDLE crashing when a file open in the editor is deleted (GH-153481) #158263
- [3.15] gh-153480: Stop IDLE crashing when a file open in the editor is deleted (GH-153481) #158264
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
terryjreedy commentedon Jul 10, 2026
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'.
serhiy-storchaka commentedon Sep 15, 2026
This is worse than a traceback on FocusIn: the same
last_mtime()runs fromset_saved(True)insideEditorWindow.__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 withFileNotFoundError. 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()returnsNonewhen there is no file or it cannot be stat'ed (not0, 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 toNoneso the question is asked once. This is an approximate plan; details can be changed later. See the review on #153481.[-]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[/+]pythongh-153480: Return None from last_mtime() and handle a deleted file
pythongh-153480: Clear mtime in the deleted-file dialog before Save As
pythongh-153480: Clear mtime before the deleted-file dialog for all c…
gh-153480: Stop IDLE crashing when a file open in the editor is delet…
[3.13] gh-153480: Stop IDLE crashing when a file open in the editor i…
[3.14] gh-153480: Stop IDLE crashing when a file open in the editor i…
[3.15] gh-153480: Stop IDLE crashing when a file open in the editor i…