classification
Title: IDLE: run's tk update adds context traceback on callback error
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2017-12-04 01:47 by terry.reedy, last changed 2017-12-04 22:04 by terry.reedy. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4703 merged terry.reedy, 2017-12-04 20:07
PR 4705 merged python-dev, 2017-12-04 21:17
Messages (5)
msg307534 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-12-04 01:47
import tkinter as tk
root = tk.Tk()

def bad(): print(a, 'bad')
def good(): print('good')

root.after(1000, bad)
root.after(1500, good)
root.mainloop()


# Correctly gives this traceback and output:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python37\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Programs\Python37\lib\tkinter\__init__.py", line 745, in callit
    func(*args)
  File "F:\Python\a\tem2.py", line 13, in bad
    def bad(): print(a, 'bad')
NameError: name 'a' is not defined
good
>>> 
====================================

Remove or comment-out the blocking 'root.mainloop()' call and run the result from an IDLE editor.  The callbacks are still called because after user code is run, run.py calls tcl.update in a loop nominally 20 x per second.  This allows developers to interact with a 'live' gui by entering statements in the shell at '>>>' prompts.  The result is this.
-------------
>>> Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python37\lib\idlelib\run.py", line 137, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Programs\Python37\lib\queue.py", line 169, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
<The NameError traceback and message, as before.>
--------------

The relevant code in run.py was written before callback chaining.

            try:
                seq, request = rpc.request_queue.get(block=True, timeout=0.05)
            except queue.Empty:
                handle_tk_events()
                continue

Experiments with normal exceptions in a shell suggest that wrapping handle_tk_events in try:except and re-raising any exception 'from None' should work.
                try:
                    handle_tk_events()
                except BaseException as e:
                    raise e from None

However, it appears that callback errors resulting from handle_tk_events() are not being caught here.  (print('message') and 1/0 before 'raise' have no visible effect.)  I will investigate more later.
msg307541 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-04 06:17
try:
                seq, request = rpc.request_queue.get(block=True, timeout=0.05)
            except queue.Empty:
                request = None
            if request is None:
                handle_tk_events()
                continue
msg307594 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-12-04 20:12
Thanks.  I am still puzzled why the nested within nested try-except did not work as I expected, but I care more about fixing this. The result based on your suggestion is better (to read, I think) than extra nesting that did work.
msg307597 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-12-04 21:16
New changeset 1e2fcac4972530aa2c963d7e4011021df5ba866e by Terry Jan Reedy in branch 'master':
bpo-32207: Improve tk event exception tracebacks in IDLE. (#4703)
https://github.com/python/cpython/commit/1e2fcac4972530aa2c963d7e4011021df5ba866e
msg307599 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-12-04 22:02
New changeset 9da33c82124f27eb58ba4cf145675fe7a1035744 by Terry Jan Reedy (Miss Islington (bot)) in branch '3.6':
bpo-32207: Improve tk event exception tracebacks in IDLE. (GH-4703) (#4705)
https://github.com/python/cpython/commit/9da33c82124f27eb58ba4cf145675fe7a1035744
History
Date User Action Args
2017-12-04 22:04:00terry.reedysetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-12-04 22:02:34terry.reedysetmessages: + msg307599
2017-12-04 21:17:26python-devsetpull_requests: + pull_request4617
2017-12-04 21:16:20terry.reedysetmessages: + msg307597
2017-12-04 20:34:02terry.reedysetstage: needs patch -> patch review
2017-12-04 20:12:32terry.reedysetmessages: + msg307594
stage: patch review -> needs patch
2017-12-04 20:07:31terry.reedysetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request4615
2017-12-04 06:17:05serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg307541
2017-12-04 01:47:39terry.reedysettitle: IDLE: run's tk update adds extra traceback on callback error -> IDLE: run's tk update adds context traceback on callback error
2017-12-04 01:47:09terry.reedycreate