|
16 | 16 | import io |
17 | 17 | import importlib.util |
18 | 18 | import pathlib |
19 | | -import _colorize |
20 | 19 |
|
21 | 20 | from contextlib import suppress |
| 21 | +lazy import _colorize |
22 | 22 |
|
23 | 23 | try: |
24 | 24 | from _missing_stdlib_info import _MISSING_STDLIB_MODULE_MESSAGES |
|
32 | 32 | 'FrameSummary', 'StackSummary', 'TracebackException', |
33 | 33 | 'walk_stack', 'walk_tb', 'print_list'] |
34 | 34 |
|
| 35 | + |
| 36 | +class _ShutdownTheme: |
| 37 | + """Empty stand-in if `_colorize` cannot be imported during late shutdown.""" |
| 38 | + def __getattr__(self, _): return self |
| 39 | + def __getitem__(self, _): return "" |
| 40 | + def __format__(self, _): return "" |
| 41 | + def __str__(self): return "" |
| 42 | + def __add__(self, other): return other |
| 43 | + __radd__ = __add__ |
| 44 | + |
| 45 | + |
| 46 | +_shutdown_theme = _ShutdownTheme() |
| 47 | + |
| 48 | + |
| 49 | +def _safe_get_theme(*, force_color=False, force_no_color=False): |
| 50 | + try: |
| 51 | + return _colorize.get_theme( |
| 52 | + force_color=force_color, force_no_color=force_no_color |
| 53 | + ) |
| 54 | + except ImportError: |
| 55 | + return _shutdown_theme |
| 56 | + |
| 57 | + |
| 58 | +def _safe_can_colorize(*, file=None): |
| 59 | + try: |
| 60 | + return _colorize.can_colorize(file=file) |
| 61 | + except ImportError: |
| 62 | + return False |
| 63 | + |
| 64 | + |
35 | 65 | # |
36 | 66 | # Formatting and printing lists of traceback lines. |
37 | 67 | # |
@@ -151,7 +181,7 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \ |
151 | 181 | def _print_exception_bltin(exc, file=None, /): |
152 | 182 | if file is None: |
153 | 183 | file = sys.stderr if sys.stderr is not None else sys.__stderr__ |
154 | | - colorize = _colorize.can_colorize(file=file) |
| 184 | + colorize = _safe_can_colorize(file=file) |
155 | 185 | return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize) |
156 | 186 |
|
157 | 187 |
|
@@ -199,9 +229,9 @@ def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize= |
199 | 229 | valuestr = _safe_string(value, 'exception') |
200 | 230 | end_char = "\n" if insert_final_newline else "" |
201 | 231 | if colorize: |
202 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 232 | + theme = _safe_get_theme(force_color=True).traceback |
203 | 233 | else: |
204 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 234 | + theme = _safe_get_theme(force_no_color=True).traceback |
205 | 235 | if value is None or not valuestr: |
206 | 236 | line = f"{theme.type}{etype}{theme.reset}{end_char}" |
207 | 237 | else: |
@@ -555,9 +585,9 @@ def format_frame_summary(self, frame_summary, **kwargs): |
555 | 585 | if frame_summary.filename.startswith("<stdin-") and frame_summary.filename.endswith('>'): |
556 | 586 | filename = "<stdin>" |
557 | 587 | if colorize: |
558 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 588 | + theme = _safe_get_theme(force_color=True).traceback |
559 | 589 | else: |
560 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 590 | + theme = _safe_get_theme(force_no_color=True).traceback |
561 | 591 | row.append( |
562 | 592 | ' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format( |
563 | 593 | theme.filename, |
@@ -1344,9 +1374,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs): |
1344 | 1374 | """ |
1345 | 1375 | colorize = kwargs.get("colorize", False) |
1346 | 1376 | if colorize: |
1347 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 1377 | + theme = _safe_get_theme(force_color=True).traceback |
1348 | 1378 | else: |
1349 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 1379 | + theme = _safe_get_theme(force_no_color=True).traceback |
1350 | 1380 |
|
1351 | 1381 | indent = 3 * _depth * ' ' |
1352 | 1382 | if not self._have_exc_type: |
@@ -1494,9 +1524,9 @@ def _format_syntax_error(self, stype, **kwargs): |
1494 | 1524 | # Show exactly where the problem was found. |
1495 | 1525 | colorize = kwargs.get("colorize", False) |
1496 | 1526 | if colorize: |
1497 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 1527 | + theme = _safe_get_theme(force_color=True).traceback |
1498 | 1528 | else: |
1499 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 1529 | + theme = _safe_get_theme(force_no_color=True).traceback |
1500 | 1530 | filename_suffix = '' |
1501 | 1531 | if self.lineno is not None: |
1502 | 1532 | yield ' File {}"{}"{}, line {}{}{}\n'.format( |
|
0 commit comments