@@ -340,7 +340,20 @@ def _init_posix(vars):
340340 """Initialize the module as appropriate for POSIX systems."""
341341 # _sysconfigdata is generated at build time, see _generate_posix_vars()
342342 name = _get_sysconfigdata_name ()
343- _temp = __import__ (name , globals (), locals (), ['build_time_vars' ], 0 )
343+
344+ # For cross builds, the path to the target's sysconfigdata must be specified
345+ # so it can be imported. It cannot be in PYTHONPATH, as foreign modules in
346+ # sys.path can cause crashes when loaded by the host interpreter.
347+ # Rely on truthiness as a valueless env variable is still an empty string.
348+ # See OS X note in _generate_posix_vars re _sysconfigdata.
349+ if (path := os .environ .get ('_PYTHON_SYSCONFIGDATA_PATH' )):
350+ from importlib .machinery import FileFinder , SourceFileLoader , SOURCE_SUFFIXES
351+ from importlib .util import module_from_spec
352+ spec = FileFinder (path , (SourceFileLoader , SOURCE_SUFFIXES )).find_spec (name )
353+ _temp = module_from_spec (spec )
354+ spec .loader .exec_module (_temp )
355+ else :
356+ _temp = __import__ (name , globals (), locals (), ['build_time_vars' ], 0 )
344357 build_time_vars = _temp .build_time_vars
345358 vars .update (build_time_vars )
346359
0 commit comments