1+ import json
12import os
23import sys
4+ import types
35from sysconfig import (
46 _ALWAYS_STR ,
57 _PYTHON_BUILD ,
@@ -157,6 +159,19 @@ def _print_config_dict(d, stream):
157159 print ("}" , file = stream )
158160
159161
162+ def _get_pybuilddir ():
163+ pybuilddir = f'build/lib.{ get_platform ()} -{ get_python_version ()} '
164+ if hasattr (sys , "gettotalrefcount" ):
165+ pybuilddir += '-pydebug'
166+ return pybuilddir
167+
168+
169+ def _get_json_data_name ():
170+ name = _get_sysconfigdata_name ()
171+ assert name .startswith ('_sysconfigdata' )
172+ return name .replace ('_sysconfigdata' , '_sysconfig_vars' ) + '.json'
173+
174+
160175def _generate_posix_vars ():
161176 """Generate the Python module containing build-time variables."""
162177 vars = {}
@@ -185,6 +200,8 @@ def _generate_posix_vars():
185200 if _PYTHON_BUILD :
186201 vars ['BLDSHARED' ] = vars ['LDSHARED' ]
187202
203+ name = _get_sysconfigdata_name ()
204+
188205 # There's a chicken-and-egg situation on OS X with regards to the
189206 # _sysconfigdata module after the changes introduced by #15298:
190207 # get_config_vars() is called by get_platform() as part of the
@@ -196,16 +213,13 @@ def _generate_posix_vars():
196213 # _sysconfigdata module manually and populate it with the build vars.
197214 # This is more than sufficient for ensuring the subsequent call to
198215 # get_platform() succeeds.
199- name = _get_sysconfigdata_name ()
200- if 'darwin' in sys .platform :
201- import types
202- module = types .ModuleType (name )
203- module .build_time_vars = vars
204- sys .modules [name ] = module
216+ # GH-127178: Since we started generating a .json file, we also need this to
217+ # be able to run sysconfig.get_config_vars().
218+ module = types .ModuleType (name )
219+ module .build_time_vars = vars
220+ sys .modules [name ] = module
205221
206- pybuilddir = f'build/lib.{ get_platform ()} -{ get_python_version ()} '
207- if hasattr (sys , "gettotalrefcount" ):
208- pybuilddir += '-pydebug'
222+ pybuilddir = _get_pybuilddir ()
209223 os .makedirs (pybuilddir , exist_ok = True )
210224 destfile = os .path .join (pybuilddir , name + '.py' )
211225
@@ -215,6 +229,11 @@ def _generate_posix_vars():
215229 f .write ('build_time_vars = ' )
216230 _print_config_dict (vars , stream = f )
217231
232+ # Write a JSON file with the output of sysconfig.get_config_vars
233+ jsonfile = os .path .join (pybuilddir , _get_json_data_name ())
234+ with open (jsonfile , 'w' ) as f :
235+ json .dump (get_config_vars (), f , indent = 2 )
236+
218237 # Create file used for sys.path fixup -- see Modules/getpath.c
219238 with open ('pybuilddir.txt' , 'w' , encoding = 'utf8' ) as f :
220239 f .write (pybuilddir )
0 commit comments