Skip to content

Commit 2ed0eb3

Browse files
Petr's review
1 parent 9c74f69 commit 2ed0eb3

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

Doc/using/configure.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ General Options
261261
containing custom error messages for missing :term:`standard library` modules.
262262

263263
This option is intended for Python distributors who wish to provide
264-
distribution-specific guidance when users encounter missing standard library
265-
modules that are packaged separately.
264+
distribution-specific guidance when users encounter standard library
265+
modules that are missing or packaged separately.
266266

267267
The JSON file should map missing module names to custom error message strings.
268268
For example, if your distribution packages :mod:`tkinter` and

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,6 @@ Improved error messages
229229
AttributeError: 'Container' object has no attribute 'area'. Did you mean: 'inner.area'?
230230
231231
232-
* The new configure option :option:`--with-missing-stdlib-config=FILE` allows
233-
distributors to pass a `JSON <https://www.json.org/json-en.html>`_
234-
configuration file containing custom error messages for missing
235-
:term:`standard library` modules.
236-
(Contributed by Stan Ulbrych and Petr Viktorin in :gh:`139707`.)
237-
238-
239232
Other language changes
240233
======================
241234

@@ -1062,6 +1055,12 @@ Build changes
10621055
set to ``no`` or with :option:`!--without-system-libmpdec`.
10631056
(Contributed by Sergey B Kirpichev in :gh:`115119`.)
10641057

1058+
* The new configure option :option:`--with-missing-stdlib-config=FILE` allows
1059+
distributors to pass a `JSON <https://www.json.org/json-en.html>`_
1060+
configuration file containing custom error messages for :term:`standard library`
1061+
modules that are missing or packaged separately.
1062+
(Contributed by Stan Ulbrych and Petr Viktorin in :gh:`139707`.)
1063+
10651064

10661065
Porting to Python 3.15
10671066
======================

Lib/traceback.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
try:
1818
from _stdlib_modules_info import MISSING_STDLIB_MODULE_MESSAGES
1919
except ImportError:
20-
MISSING_STDLIB_MODULE_MESSAGES = None
20+
MISSING_STDLIB_MODULE_MESSAGES = {}
2121

2222
__all__ = ['extract_stack', 'extract_tb', 'format_exception',
2323
'format_exception_only', 'format_list', 'format_stack',
@@ -1115,14 +1115,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11151115
elif exc_type and issubclass(exc_type, ModuleNotFoundError):
11161116
module_name = getattr(exc_value, "name", None)
11171117
if module_name in sys.stdlib_module_names:
1118-
if MISSING_STDLIB_MODULE_MESSAGES is not None:
1119-
message = MISSING_STDLIB_MODULE_MESSAGES.get(
1120-
module_name,
1121-
f"Standard library module {module_name!r} was not found"
1122-
)
1123-
self._str = message
1124-
else:
1125-
self._str = f"Standard library module {module_name!r} was not found"
1118+
message = MISSING_STDLIB_MODULE_MESSAGES.get(
1119+
module_name,
1120+
f"Standard library module {module_name!r} was not found"
1121+
)
1122+
self._str = message
11261123
elif sys.flags.no_site:
11271124
self._str += (". Site initialization is disabled, did you forget to "
11281125
+ "add the site-packages directory to sys.path "

Makefile.pre.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,9 +1602,9 @@ sharedmods: $(SHAREDMODS) pybuilddir.txt
16021602
.PHONY: checksharedmods
16031603
checksharedmods: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON)
16041604
@if [ -n "@MISSING_STDLIB_CONFIG@" ]; then \
1605-
$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-stdlib-info="@MISSING_STDLIB_CONFIG@"; \
1605+
$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info --with-missing-stdlib-config="@MISSING_STDLIB_CONFIG@"; \
16061606
else \
1607-
$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-stdlib-info; \
1607+
$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info; \
16081608
fi
16091609
@$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py
16101610

Tools/build/check_extension_modules.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import logging
2828
import os
2929
import pathlib
30+
import pprint
3031
import re
3132
import sys
3233
import sysconfig
@@ -118,10 +119,15 @@
118119
)
119120

120121
parser.add_argument(
121-
"--generate-stdlib-info",
122-
nargs="?",
123-
const=True,
124-
help="Generate file with stdlib module info, with optional config file",
122+
"--generate-missing-stdlib-info",
123+
action="store_true",
124+
help="Generate file with stdlib module info",
125+
)
126+
127+
parser.add_argument(
128+
"--with-missing-stdlib-config",
129+
metavar="CONFIG_FILE",
130+
help="Path to JSON config file with custom missing module messages",
125131
)
126132

127133

@@ -289,14 +295,14 @@ def list_module_names(self, *, all: bool = False) -> set[str]:
289295
names.update(WINDOWS_MODULES)
290296
return names
291297

292-
def generate_stdlib_info(self, config_path: str | None = None) -> None:
298+
def generate_missing_stdlib_info(self, config_path: str | None = None) -> None:
293299
config_messages = {}
294300
if config_path:
295301
try:
296302
with open(config_path, encoding='utf-8') as f:
297303
config_messages = json.load(f)
298304
except (FileNotFoundError, json.JSONDecodeError) as e:
299-
logger.error("Failed to load distributor config %s: %s", config_path, e)
305+
raise RuntimeError("Failed to load missing stdlib config %s: %s", config_path, e)
300306

301307
messages = {}
302308
for name in WINDOWS_MODULES:
@@ -313,11 +319,12 @@ def generate_stdlib_info(self, config_path: str | None = None) -> None:
313319
content = f'''\
314320
# Standard library information used by the traceback module for more informative
315321
# ModuleNotFound error messages.
322+
# Generated by check_extension_modules.py
316323
317-
MISSING_STDLIB_MODULE_MESSAGES = {messages!r}
324+
MISSING_STDLIB_MODULE_MESSAGES = {pprint.pformat(messages)}
318325
'''
319326

320-
output_path = self.builddir / "_stdlib_modules_info.py"
327+
output_path = self.builddir / "_missing_stdlib_module_info.py"
321328
with open(output_path, "w", encoding="utf-8") as f:
322329
f.write(content)
323330

@@ -539,10 +546,9 @@ def main() -> None:
539546
names = checker.list_module_names(all=True)
540547
for name in sorted(names):
541548
print(name)
542-
elif args.generate_stdlib_info:
549+
elif args.generate_missing_stdlib_info:
543550
checker.check()
544-
config_path = None if args.generate_stdlib_info is True else args.generate_stdlib_info
545-
checker.generate_stdlib_info(config_path)
551+
checker.generate_missing_stdlib_info(args.with_missing_stdlib_config)
546552
else:
547553
checker.check()
548554
checker.summary(verbose=args.verbose)

0 commit comments

Comments
 (0)