gh-139707: Add mechanism for distributors to supply error messages for missing stdlib modules (GH-140783)

This commit is contained in:
Stan Ulbrych 2025-12-01 13:36:17 +00:00 committed by GitHub
parent b708485d1a
commit d4fa70706c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 149 additions and 2 deletions

View file

@ -14,6 +14,11 @@
from contextlib import suppress
try:
from _missing_stdlib_info import _MISSING_STDLIB_MODULE_MESSAGES
except ImportError:
_MISSING_STDLIB_MODULE_MESSAGES = {}
__all__ = ['extract_stack', 'extract_tb', 'format_exception',
'format_exception_only', 'format_list', 'format_stack',
'format_tb', 'print_exc', 'format_exc', 'print_exception',
@ -1110,7 +1115,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
elif exc_type and issubclass(exc_type, ModuleNotFoundError):
module_name = getattr(exc_value, "name", None)
if module_name in sys.stdlib_module_names:
self._str = f"Standard library module '{module_name}' was not found"
message = _MISSING_STDLIB_MODULE_MESSAGES.get(
module_name,
f"Standard library module {module_name!r} was not found"
)
self._str = message
elif sys.flags.no_site:
self._str += (". Site initialization is disabled, did you forget to "
+ "add the site-packages directory to sys.path "