This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tk.report_callback_exception kills process when run with pythonw.exe
Type: behavior Stage: resolved
Components: Tkinter Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Aivar.Annamaa, python-dev, serhiy.storchaka, terry.reedy
Priority: normal Keywords: needs review, patch

Created on 2014-09-10 20:14 by Aivar.Annamaa, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
demo.py Aivar.Annamaa, 2014-09-10 20:14
tkinter_report_callback_exception.patch serhiy.storchaka, 2014-09-11 13:13 review
Messages (9)
msg226712 - (view) Author: Aivar Annamaa (Aivar.Annamaa) * Date: 2014-09-10 20:14
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.
msg226772 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-11 13:13
May be this patch would help.
msg226855 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-09-13 21:06
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).
msg226876 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-14 16:43
Error messages are already silenced if sys.stderr is None or closed.

>>> sys.stderr.close()
>>> 1/0
>>> 

>>> sys.stderr = None
>>> 1/0
>>> 

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.
msg226880 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-09-14 17:44
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.
"""
msg226881 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-09-14 17:45
And yes, I am thinking about a broader fix for Idle -- replacing stderr None with something writable.
msg226883 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-14 18:20
New changeset 994a16b51544 by Serhiy Storchaka in branch '2.7':
Issue #22384: An exception in Tkinter callback no longer crashes the program
https://hg.python.org/cpython/rev/994a16b51544

New changeset c62fad86fac3 by Serhiy Storchaka in branch '3.4':
Issue #22384: An exception in Tkinter callback no longer crashes the program
https://hg.python.org/cpython/rev/c62fad86fac3

New changeset 7191b14ca312 by Serhiy Storchaka in branch 'default':
Issue #22384: An exception in Tkinter callback no longer crashes the program
https://hg.python.org/cpython/rev/7191b14ca312
msg226884 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-14 18:26
Thank you for suggested docstring Terry. There is related question on StackOverflow:

http://stackoverflow.com/questions/4770993/silent-exceptions-in-python-tkinter-should-i-make-them-louder-how
msg226885 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-14 18:39
Thank you Aivar for helpful report.
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66578
2014-09-14 18:39:51serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg226885

stage: patch review -> resolved
2014-09-14 18:26:23serhiy.storchakasetmessages: + msg226884
2014-09-14 18:20:49python-devsetnosy: + python-dev
messages: + msg226883
2014-09-14 17:45:56terry.reedysetmessages: + msg226881
2014-09-14 17:44:33terry.reedysetmessages: + msg226880
2014-09-14 16:43:38serhiy.storchakasetmessages: + msg226876
2014-09-13 21:06:38terry.reedysetmessages: + msg226855
2014-09-13 16:07:12serhiy.storchakasetkeywords: + needs review
nosy: + terry.reedy
2014-09-11 13:13:10serhiy.storchakasetfiles: + tkinter_report_callback_exception.patch

type: crash -> behavior
assignee: serhiy.storchaka
versions: + Python 2.7, Python 3.5
keywords: + patch
nosy: + serhiy.storchaka

messages: + msg226772
stage: patch review
2014-09-10 20:14:53Aivar.Annamaasettype: crash
components: + Tkinter
versions: + Python 3.4
2014-09-10 20:14:34Aivar.Annamaacreate