Skip to content
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

IDLE: run's tk update adds context traceback on callback error #76388

Closed
terryjreedy opened this issue Dec 4, 2017 · 5 comments
Closed

IDLE: run's tk update adds context traceback on callback error #76388

terryjreedy opened this issue Dec 4, 2017 · 5 comments
Assignees
Labels
3.7 (EOL) end of life topic-IDLE type-bug An unexpected behavior, bug, or error

Comments

@terryjreedy
Copy link
Member

BPO 32207
Nosy @terryjreedy, @serhiy-storchaka
PRs
  • bpo-32207: Improve tk event exception tracebacks in IDLE. #4703
  • [3.6] bpo-32207: Improve tk event exception tracebacks in IDLE. (GH-4703) #4705
  • 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:

    assignee = 'https://github.com/terryjreedy'
    closed_at = <Date 2017-12-04.22:04:00.148>
    created_at = <Date 2017-12-04.01:47:09.727>
    labels = ['expert-IDLE', 'type-bug', '3.7']
    title = "IDLE: run's tk update adds context traceback on callback error"
    updated_at = <Date 2017-12-04.22:04:00.147>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2017-12-04.22:04:00.147>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2017-12-04.22:04:00.148>
    closer = 'terry.reedy'
    components = ['IDLE']
    creation = <Date 2017-12-04.01:47:09.727>
    creator = 'terry.reedy'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32207
    keywords = ['patch']
    message_count = 5.0
    messages = ['307534', '307541', '307594', '307597', '307599']
    nosy_count = 2.0
    nosy_names = ['terry.reedy', 'serhiy.storchaka']
    pr_nums = ['4703', '4705']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32207'
    versions = ['Python 3.6', 'Python 3.7']

    @terryjreedy
    Copy link
    Member Author

    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.

    @terryjreedy terryjreedy added the 3.7 (EOL) end of life label Dec 4, 2017
    @terryjreedy terryjreedy self-assigned this Dec 4, 2017
    @terryjreedy terryjreedy added topic-IDLE type-bug An unexpected behavior, bug, or error labels Dec 4, 2017
    @terryjreedy terryjreedy changed the title IDLE: run's tk update adds extra traceback on callback error IDLE: run's tk update adds context traceback on callback error Dec 4, 2017
    @serhiy-storchaka
    Copy link
    Member

    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

    @terryjreedy
    Copy link
    Member Author

    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.

    @terryjreedy
    Copy link
    Member Author

    New changeset 1e2fcac by Terry Jan Reedy in branch 'master':
    bpo-32207: Improve tk event exception tracebacks in IDLE. (bpo-4703)
    1e2fcac

    @terryjreedy
    Copy link
    Member Author

    New changeset 9da33c8 by Terry Jan Reedy (Miss Islington (bot)) in branch '3.6':
    bpo-32207: Improve tk event exception tracebacks in IDLE. (GH-4703) (bpo-4705)
    9da33c8

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life topic-IDLE type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants