New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tk.report_callback_exception kills process when run with pythonw.exe #66578
Comments
Seems that the statement 'sys.stderr.write("Exception in Tkinter callback\n")' in Tk.report_callback_exception fails when the program is run with pythonw.exe, and brings down the whole process. A simple sample is attached. |
May be this patch would help. |
In a pythonw process, stdout and stderr are initially None unless and until changed. (This is something we need to do for the Idle process itself.) Writing to None generates an AttributeError. Uncaught exceptions stop the Python process. The patch works, for this particular case, in the sense of preventing process termination. Print suppresses the exception-reporting exception (after trying sys.stdout as a backup). It still fails at delivering the original exception message and traceback. 'Click', and nothing happens. A developer need to see the specific message and users should at least know that something is wrong. The following alternate delivers the message in addition to suppressing the AttributeError. It is a a copy and paste replacement for the existing "def report_callback_exception" statement. (It was easier for me to experiment with my installed, non-repository Pythons). class _Errbox:
def __init__(self):
self.txt=[]
from tkinter.messagebox import showerror
self.showerror = showerror
def write(self, txt):
self.txt.append(txt)
def show(self):
self.showerror(
title="Exception in Tkinter callback",
message=''.join(self.txt))
self.txt = []
def report_callback_exception(self, exc, val, tb):
"""Internal function. It reports exception on sys.stderr."""
import traceback
try:
sys.stderr.write("Exception in Tkinter callback\n")
efile = sys.stderr
except AttributeError:
efile = self._Errbox()
sys.last_type = exc
sys.last_value = val
sys.last_traceback = tb
traceback.print_exception(exc, val, tb, file=efile)
if isinstance(efile, self._Errbox):
efile.show() I checked and this is the only direct .write in the file. There is only one other print (to the default sys.stdout). |
Error messages are already silenced if sys.stderr is None or closed.
I think that such things as _Errbox are application level solutions. report_callback_exception() is designed to be overwritten for this purpose. Application can decide to pop up message box always, not only when sys.stderr is None, or tracebacks to a log, or add application icon and scrollbar on message box, or output error on special area on main windows. Definitely it would be good to add something like _Errbox to IDLE. But this will be other issue. This issue is only about "crashing" of default implementation, and if my patch fixes it, I want to commit it and close the issue. |
Since traceback.print_exception already uses print statememts, your patch *is* sufficient to trap the remaining stderr exception. Go ahead. The doctring for report_callback_exception calls it an 'internal function'. To me, that implies 'ignore this' rather than 'override this'. I suggest changing the docstring to """Report callback exception on sys.stderr. Applications may want to override this internal function, and should when sys.stderr is None. |
And yes, I am thinking about a broader fix for Idle -- replacing stderr None with something writable. |
New changeset 994a16b51544 by Serhiy Storchaka in branch '2.7': New changeset c62fad86fac3 by Serhiy Storchaka in branch '3.4': New changeset 7191b14ca312 by Serhiy Storchaka in branch 'default': |
Thank you for suggested docstring Terry. There is related question on StackOverflow: |
Thank you Aivar for helpful report. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: