- Windows 2025 v24H2 (OS Build 26100.6905) with Python 3.14.
- Program is running in Windows Terminal, not a cmd.exe box.
I’m posting this as a new thread as this may be a bug and I have narrowed down what is happening.
Since the file handles are not being closed, when I exit the debugger I get 3 I/O errors. Minimal code shows the file handle is still populated whether I use the WITH clause or manual close.
"""file: errfile.py
errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
"""
import os
errfn = "errtest.txt"
with open(errfn, 'w') as errfile:
errfile.write("This is a test line\n")
print(f"1) errfile={errfile}")
errfile = open(errfn, 'w')
errfile.write("This is test line 2\n")
errfile.close()
print(f"2) errfile={errfile}")
Command line: python -m pdb errfile.py
Program output is:
>python -m pdb errfile.py
> e:\myprog\errfile.py(1)<module>()
-> """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
(Pdb) c
1) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>
2) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='cp1252'>
The program finished and will be restarted
The errors are:
> e:\myprog\errfile.py(1)<module>()
-> """errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
(Pdb) q
*** SystemExit: None
(Pdb) Traceback (most recent call last):
File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
pdb._run(target)
~~~~~~~~^^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
self.run(target.code)
~~~~~~~~^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
exec(cmd, globals, locals)
~~~~^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
File "E:\myprog\errfile.py", line 1, in <module>
"""errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
"""
File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
ret = func(frame, *args)
File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
frame.f_trace(frame, 'line', None)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
return self.dispatch_line(frame)
~~~~~~~~~~~~~~~~~~^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
self.user_line(frame)
~~~~~~~~~~~~~~^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
self.interaction(frame, None)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
self._cmdloop()
~~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
self.cmdloop()
~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
line = input(self.prompt)
ValueError: I/O operation on closed file.
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\program files\python314\lib\cmd.py(137)cmdloop()
-> line = input(self.prompt)
(Pdb) Traceback (most recent call last):
File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
pdb._run(target)
~~~~~~~~^^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
self.run(target.code)
~~~~~~~~^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
exec(cmd, globals, locals)
~~~~^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
File "E:\myprog\errfile.py", line 1, in <module>
"""errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
"""
File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
ret = func(frame, *args)
File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
frame.f_trace(frame, 'line', None)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
return self.dispatch_line(frame)
~~~~~~~~~~~~~~~~~~^^^^^^^
File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
self.user_line(frame)
~~~~~~~~~~~~~~^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
self.interaction(frame, None)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
self._cmdloop()
~~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
self.cmdloop()
~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
line = input(self.prompt)
ValueError: I/O operation on closed file.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Program Files\Python314\Lib\pdb.py", line 3627, in <module>
pdb.main()
~~~~~~~~^^
File "C:\Program Files\Python314\Lib\pdb.py", line 3614, in main
pdb.interaction(None, e)
~~~~~~~~~~~~~~~^^^^^^^^^
File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
self._cmdloop()
~~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
self.cmdloop()
~~~~~~~~~~~~^^
File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
line = input(self.prompt)
ValueError: I/O operation on closed file.
- Should I report this as a bug on Github?
