|
16 | 16 | import io |
17 | 17 | import importlib.util |
18 | 18 | import pathlib |
19 | | -import _colorize |
| 19 | +lazy import _colorize |
20 | 20 |
|
21 | 21 | from contextlib import suppress |
22 | 22 |
|
| 23 | + |
| 24 | +class _ShutdownTheme: |
| 25 | + """Empty stand-in if `_colorize` cannot be imported during late shutdown.""" |
| 26 | + def __getattr__(self, _): return self |
| 27 | + def __getitem__(self, _): return "" |
| 28 | + def __format__(self, _): return "" |
| 29 | + def __str__(self): return "" |
| 30 | + def __add__(self, other): return other |
| 31 | + __radd__ = __add__ |
| 32 | + |
| 33 | + |
| 34 | +_shutdown_theme = _ShutdownTheme() |
| 35 | + |
| 36 | + |
| 37 | +def _safe_get_theme(*, force_color=False, force_no_color=False): |
| 38 | + try: |
| 39 | + return _colorize.get_theme( |
| 40 | + force_color=force_color, force_no_color=force_no_color |
| 41 | + ) |
| 42 | + except ImportError: |
| 43 | + return _shutdown_theme |
| 44 | + |
| 45 | + |
| 46 | +def _safe_can_colorize(*, file=None): |
| 47 | + try: |
| 48 | + return _colorize.can_colorize(file=file) |
| 49 | + except ImportError: |
| 50 | + return False |
| 51 | + |
23 | 52 | try: |
24 | 53 | from _missing_stdlib_info import _MISSING_STDLIB_MODULE_MESSAGES |
25 | 54 | except ImportError: |
@@ -151,7 +180,7 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \ |
151 | 180 | def _print_exception_bltin(exc, file=None, /): |
152 | 181 | if file is None: |
153 | 182 | file = sys.stderr if sys.stderr is not None else sys.__stderr__ |
154 | | - colorize = _colorize.can_colorize(file=file) |
| 183 | + colorize = _safe_can_colorize(file=file) |
155 | 184 | return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize) |
156 | 185 |
|
157 | 186 |
|
@@ -199,9 +228,9 @@ def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize= |
199 | 228 | valuestr = _safe_string(value, 'exception') |
200 | 229 | end_char = "\n" if insert_final_newline else "" |
201 | 230 | if colorize: |
202 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 231 | + theme = _safe_get_theme(force_color=True).traceback |
203 | 232 | else: |
204 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 233 | + theme = _safe_get_theme(force_no_color=True).traceback |
205 | 234 | if value is None or not valuestr: |
206 | 235 | line = f"{theme.type}{etype}{theme.reset}{end_char}" |
207 | 236 | else: |
@@ -555,9 +584,9 @@ def format_frame_summary(self, frame_summary, **kwargs): |
555 | 584 | if frame_summary.filename.startswith("<stdin-") and frame_summary.filename.endswith('>'): |
556 | 585 | filename = "<stdin>" |
557 | 586 | if colorize: |
558 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 587 | + theme = _safe_get_theme(force_color=True).traceback |
559 | 588 | else: |
560 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 589 | + theme = _safe_get_theme(force_no_color=True).traceback |
561 | 590 | row.append( |
562 | 591 | ' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format( |
563 | 592 | theme.filename, |
@@ -1336,9 +1365,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs): |
1336 | 1365 | """ |
1337 | 1366 | colorize = kwargs.get("colorize", False) |
1338 | 1367 | if colorize: |
1339 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 1368 | + theme = _safe_get_theme(force_color=True).traceback |
1340 | 1369 | else: |
1341 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 1370 | + theme = _safe_get_theme(force_no_color=True).traceback |
1342 | 1371 |
|
1343 | 1372 | indent = 3 * _depth * ' ' |
1344 | 1373 | if not self._have_exc_type: |
@@ -1486,9 +1515,9 @@ def _format_syntax_error(self, stype, **kwargs): |
1486 | 1515 | # Show exactly where the problem was found. |
1487 | 1516 | colorize = kwargs.get("colorize", False) |
1488 | 1517 | if colorize: |
1489 | | - theme = _colorize.get_theme(force_color=True).traceback |
| 1518 | + theme = _safe_get_theme(force_color=True).traceback |
1490 | 1519 | else: |
1491 | | - theme = _colorize.get_theme(force_no_color=True).traceback |
| 1520 | + theme = _safe_get_theme(force_no_color=True).traceback |
1492 | 1521 | filename_suffix = '' |
1493 | 1522 | if self.lineno is not None: |
1494 | 1523 | yield ' File {}"{}"{}, line {}{}{}\n'.format( |
|
0 commit comments