Skip to content

Pytest breaks code that relies on threading.excepthook #9193

Answered by graingert
klamann asked this question in Q&A
Discussion options

You must be logged in to vote

you can disable the plugin https://docs.pytest.org/en/6.2.x/usage.html#warning-about-unraisable-exceptions-and-unhandled-thread-exceptions with -p no:threadexception

but rather than rely on global state, you could also refactor your application and test to set your thread hook in a context manager:

import contextlib
import threading

@contextlib.contextmanager
def threadhook_context(new_hook):
    old_hook, threading.excepthook = threading.excepthook, new_hook
    try:
        yield old_hook
    finally:
        assert threading.excepthook is new_hook
        threading.excepthook = old_hook

def excepthook(args: threading.ExceptHookArgs):
    print("what happens next is very important...")

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@klamann
Comment options

@graingert
Comment options

@klamann
Comment options

Answer selected by klamann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #9192 on October 12, 2021 09:45.