Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-144384: Lazily import _colorize#149318
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e5559c25d16d31c31b1a56a88c2d1d366009fc265e5990755dfe32c53a6306c2d346d582e17cfbe0b1e30bb09567bcfc9c85588e1ed1f8e6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,9 +16,9 @@ | ||
| import io | ||
| import importlib.util | ||
| import pathlib | ||
| import _colorize | ||
| from contextlib import suppress | ||
| lazy import _colorize | ||
| try: | ||
| from _missing_stdlib_info import _MISSING_STDLIB_MODULE_MESSAGES | ||
| @@ -32,6 +32,36 @@ | ||
| 'FrameSummary', 'StackSummary', 'TracebackException', | ||
| 'walk_stack', 'walk_tb', 'print_list'] | ||
| class _ShutdownTheme: | ||
| """Empty stand-in if `_colorize` cannot be imported during late shutdown.""" | ||
| def __getattr__(self, _): return self | ||
| def __getitem__(self, _): return "" | ||
| def __format__(self, _): return "" | ||
| def __str__(self): return "" | ||
| def __add__(self, other): return other | ||
| __radd__ = __add__ | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _shutdown_theme = _ShutdownTheme() | ||
| def _safe_get_theme(*, force_color=False, force_no_color=False): | ||
| try: | ||
| return _colorize.get_theme( | ||
| force_color=force_color, force_no_color=force_no_color | ||
| ) | ||
| except ImportError: | ||
| return _shutdown_theme | ||
hugovk marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def _safe_can_colorize(*, file=None): | ||
| try: | ||
| return _colorize.can_colorize(file=file) | ||
| except ImportError: | ||
| return False | ||
| # | ||
| # Formatting and printing lists of traceback lines. | ||
| # | ||
| @@ -151,7 +181,7 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \ | ||
| def _print_exception_bltin(exc, file=None, /): | ||
| if file is None: | ||
| file = sys.stderr if sys.stderr is not None else sys.__stderr__ | ||
| colorize = _colorize.can_colorize(file=file) | ||
| colorize = _safe_can_colorize(file=file) | ||
| return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize) | ||
| @@ -199,9 +229,9 @@ def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize= | ||
| valuestr = _safe_string(value, 'exception') | ||
| end_char = "\n" if insert_final_newline else "" | ||
| if colorize: | ||
| theme = _colorize.get_theme(force_color=True).traceback | ||
| theme = _safe_get_theme(force_color=True).traceback | ||
| else: | ||
| theme = _colorize.get_theme(force_no_color=True).traceback | ||
| theme = _safe_get_theme(force_no_color=True).traceback | ||
| if value is None or not valuestr: | ||
| line = f"{theme.type}{etype}{theme.reset}{end_char}" | ||
| else: | ||
| @@ -555,9 +585,9 @@ def format_frame_summary(self, frame_summary, **kwargs): | ||
| if frame_summary.filename.startswith("<stdin-") and frame_summary.filename.endswith('>'): | ||
| filename = "<stdin>" | ||
| if colorize: | ||
| theme = _colorize.get_theme(force_color=True).traceback | ||
| theme = _safe_get_theme(force_color=True).traceback | ||
| else: | ||
| theme = _colorize.get_theme(force_no_color=True).traceback | ||
| theme = _safe_get_theme(force_no_color=True).traceback | ||
| row.append( | ||
| ' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format( | ||
| theme.filename, | ||
| @@ -1344,9 +1374,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs): | ||
| """ | ||
| colorize = kwargs.get("colorize", False) | ||
| if colorize: | ||
| theme = _colorize.get_theme(force_color=True).traceback | ||
| theme = _safe_get_theme(force_color=True).traceback | ||
| else: | ||
| theme = _colorize.get_theme(force_no_color=True).traceback | ||
| theme = _safe_get_theme(force_no_color=True).traceback | ||
| indent = 3 * _depth * ' ' | ||
| if not self._have_exc_type: | ||
| @@ -1494,9 +1524,9 @@ def _format_syntax_error(self, stype, **kwargs): | ||
| # Show exactly where the problem was found. | ||
| colorize = kwargs.get("colorize", False) | ||
| if colorize: | ||
| theme = _colorize.get_theme(force_color=True).traceback | ||
| theme = _safe_get_theme(force_color=True).traceback | ||
| else: | ||
| theme = _colorize.get_theme(force_no_color=True).traceback | ||
| theme = _safe_get_theme(force_no_color=True).traceback | ||
| filename_suffix = '' | ||
| if self.lineno is not None: | ||
| yield ' File {}"{}"{}, line {}{}{}\n'.format( | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Lazily import :mod:`!_colorize`. Patch by Hugo van Kemenade. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.