Skip to content

Commit 902274c

Browse files
Petr's review
1 parent 185e890 commit 902274c

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

Doc/using/configure.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,14 @@ General Options
265265
modules that are packaged separately.
266266

267267
The JSON file should map missing module names to custom error message strings.
268-
For example, a configuration for the :mod:`tkinter` module:
268+
For example, if your distribution packages :mod:`tkinter` and
269+
:mod:`_tkinter` separately and excludes :mod:`_gdbm` for legal reasons,
270+
the configuration could contain:
269271

270272
.. code-block:: json
271273
272274
{
275+
"_gdbm": "The '_gdbm' module is not available in this distribution"
273276
"tkinter": "Install the python-tk package to use tkinter",
274277
"_tkinter": "Install the python-tk package to use tkinter",
275278
}

Lib/test/test_traceback.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5072,6 +5072,16 @@ def test_missing_stdlib_module(self):
50725072

50735073
self.assertIn(b"Install 'spam4life' for 'spam'", stderr)
50745074

5075+
@unittest.skipIf(sys.platform == "win32", "Non-Windows test")
5076+
def test_windows_only_module_error(self):
5077+
try:
5078+
import msvcrt # noqa: F401
5079+
except ModuleNotFoundError:
5080+
formatted = traceback.format_exc()
5081+
self.assertIn("Unsupported platform for Windows-only standard library module 'msvcrt'", formatted)
5082+
else:
5083+
self.fail("ModuleNotFoundError was not raised")
5084+
50755085

50765086
class TestColorizedTraceback(unittest.TestCase):
50775087
maxDiff = None

Lib/traceback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,11 +1118,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11181118
if MISSING_STDLIB_MODULE_MESSAGES is not None:
11191119
message = MISSING_STDLIB_MODULE_MESSAGES.get(
11201120
module_name,
1121-
f"Standard library module '{module_name}' was not found"
1121+
f"Standard library module {module_name!r} was not found"
11221122
)
11231123
self._str = message
11241124
else:
1125-
self._str = f"Standard library module '{module_name}' was not found"
1125+
self._str = f"Standard library module {module_name!r} was not found"
11261126
elif sys.flags.no_site:
11271127
self._str += (". Site initialization is disabled, did you forget to "
11281128
+ "add the site-packages directory to sys.path "

Tools/build/check_extension_modules.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,6 @@ def list_module_names(self, *, all: bool = False) -> set[str]:
290290
return names
291291

292292
def generate_stdlib_info(self, config_path: str | None = None) -> None:
293-
294-
disabled_modules = {modinfo.name for modinfo in self.modules
295-
if modinfo.state in (ModuleState.DISABLED, ModuleState.DISABLED_SETUP)}
296-
missing_modules = {modinfo.name for modinfo in self.modules
297-
if modinfo.state == ModuleState.MISSING}
298-
na_modules = {modinfo.name for modinfo in self.modules
299-
if modinfo.state == ModuleState.NA}
300-
301293
config_messages = {}
302294
if config_path:
303295
try:
@@ -306,26 +298,22 @@ def generate_stdlib_info(self, config_path: str | None = None) -> None:
306298
except (FileNotFoundError, json.JSONDecodeError) as e:
307299
logger.error("Failed to load distributor config %s: %s", config_path, e)
308300

309-
default_messages = {
310-
**{name: f"Windows-only standard library module '{name}' was not found"
311-
for name in WINDOWS_MODULES},
312-
**{name: f"Standard library module disabled during build '{name}' was not found"
313-
for name in disabled_modules},
314-
**{name: f"Unsupported platform for standard library module '{name}'"
315-
for name in na_modules},
316-
}
301+
messages = {}
302+
for name in WINDOWS_MODULES:
303+
messages[name] = f"Unsupported platform for Windows-only standard library module {name!r}"
304+
305+
for modinfo in self.modules:
306+
if modinfo.state in (ModuleState.DISABLED, ModuleState.DISABLED_SETUP):
307+
messages[modinfo.name] = f"Standard library module disabled during build {modinfo.name!r} was not found"
308+
elif modinfo.state == ModuleState.NA:
309+
messages[modinfo.name] = f"Unsupported platform for standard library module {modinfo.name!r}"
317310

318-
messages = {**default_messages, **config_messages}
311+
messages.update(config_messages)
319312

320313
content = f'''\
321314
# Standard library information used by the traceback module for more informative
322315
# ModuleNotFound error messages.
323316
324-
DISABLED_MODULES = {sorted(disabled_modules)!r}
325-
MISSING_MODULES = {sorted(missing_modules)!r}
326-
NOT_AVAILABLE_MODULES = {sorted(na_modules)!r}
327-
WINDOWS_ONLY_MODULES = {sorted(WINDOWS_MODULES)!r}
328-
329317
MISSING_STDLIB_MODULE_MESSAGES = {messages!r}
330318
'''
331319

0 commit comments

Comments
 (0)