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

Introduce the uninstall method #2972

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

LOCALS_MAX_LENGTH = 10
LOCALS_MAX_STRING = 80
_UNINSTALL_CALLABLE = None


def install(
Expand Down Expand Up @@ -160,16 +161,39 @@ def ipy_display_traceback(
*args, is_syntax=True, **kwargs
)

global _UNINSTALL_CALLABLE
try: # pragma: no cover
# if within ipython, use customized traceback
ip = get_ipython() # type: ignore[name-defined]
old_hooks = [ip._showtraceback, ip.showtraceback, ip.showsyntaxerror]
ipy_excepthook_closure(ip)
return sys.excepthook

def _uninstall():
# if within ipython, use customized traceback
ip = get_ipython() # type: ignore[name-defined]
ip._showtraceback = old_hooks[0]
# add wrapper to capture tb_data
ip.showtraceback = old_hooks[1]
ip.showsyntaxerror = old_hooks[2]

_UNINSTALL_CALLABLE = _uninstall
return _uninstall
except Exception:
# otherwise use default system hook
old_excepthook = sys.excepthook
sys.excepthook = excepthook
return old_excepthook

def _uninstall():
sys.excepthook = old_excepthook

_UNINSTALL_CALLABLE = _uninstall
return _uninstall


def uninstall() -> None:
"""Reset any installed traceback or except hook(s) if any."""
if _UNINSTALL_CALLABLE:
_UNINSTALL_CALLABLE()


@dataclass
Expand Down
Loading