2323import _imp
2424import argparse
2525import enum
26+ import json
2627import logging
2728import os
2829import pathlib
118119
119120parser .add_argument (
120121 "--generate-stdlib-info" ,
121- action = "store_true" ,
122- help = "Generate file with stdlib module info" ,
122+ nargs = "?" ,
123+ const = True ,
124+ help = "Generate file with stdlib module info, with optional config file" ,
123125)
124126
125127
@@ -287,7 +289,7 @@ def list_module_names(self, *, all: bool = False) -> set[str]:
287289 names .update (WINDOWS_MODULES )
288290 return names
289291
290- def generate_stdlib_info (self ) -> None :
292+ def generate_stdlib_info (self , config_path : str | None = None ) -> None :
291293
292294 disabled_modules = {modinfo .name for modinfo in self .modules
293295 if modinfo .state in (ModuleState .DISABLED , ModuleState .DISABLED_SETUP )}
@@ -296,6 +298,25 @@ def generate_stdlib_info(self) -> None:
296298 na_modules = {modinfo .name for modinfo in self .modules
297299 if modinfo .state == ModuleState .NA }
298300
301+ config_messages = {}
302+ if config_path :
303+ try :
304+ with open (config_path , encoding = 'utf-8' ) as f :
305+ config_messages = json .load (f )
306+ except (FileNotFoundError , json .JSONDecodeError ) as e :
307+ logger .error ("Failed to load distributor config %s: %s" , config_path , e )
308+
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+ }
317+
318+ messages = {** default_messages , ** config_messages }
319+
299320 content = f'''\
300321 # Standard library information used by the traceback module for more informative
301322# ModuleNotFound error messages.
@@ -305,14 +326,7 @@ def generate_stdlib_info(self) -> None:
305326NOT_AVAILABLE_MODULES = { sorted (na_modules )!r}
306327WINDOWS_ONLY_MODULES = { sorted (WINDOWS_MODULES )!r}
307328
308- MISSING_STDLIB_MODULE_MESSAGES = {{
309- **{{name: f"Windows-only standard library module '{{name}}' was not found"
310- for name in WINDOWS_ONLY_MODULES}},
311- **{{name: f"Standard library module disabled during build '{{name}}' was not found"
312- for name in DISABLED_MODULES}},
313- **{{name: f"Unsupported platform for standard library module '{{name}}'"
314- for name in NOT_AVAILABLE_MODULES}},
315- }}
329+ MISSING_STDLIB_MODULE_MESSAGES = { messages !r}
316330'''
317331
318332 output_path = self .builddir / "_stdlib_modules_info.py"
@@ -539,7 +553,8 @@ def main() -> None:
539553 print (name )
540554 elif args .generate_stdlib_info :
541555 checker .check ()
542- checker .generate_stdlib_info ()
556+ config_path = None if args .generate_stdlib_info is True else args .generate_stdlib_info
557+ checker .generate_stdlib_info (config_path )
543558 else :
544559 checker .check ()
545560 checker .summary (verbose = args .verbose )
0 commit comments