1
+ import json
1
2
import os
2
3
import sys
4
+ import types
3
5
from sysconfig import (
4
6
_ALWAYS_STR ,
5
7
_PYTHON_BUILD ,
@@ -157,6 +159,19 @@ def _print_config_dict(d, stream):
157
159
print ("}" , file = stream )
158
160
159
161
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
+
160
175
def _generate_posix_vars ():
161
176
"""Generate the Python module containing build-time variables."""
162
177
vars = {}
@@ -185,6 +200,8 @@ def _generate_posix_vars():
185
200
if _PYTHON_BUILD :
186
201
vars ['BLDSHARED' ] = vars ['LDSHARED' ]
187
202
203
+ name = _get_sysconfigdata_name ()
204
+
188
205
# There's a chicken-and-egg situation on OS X with regards to the
189
206
# _sysconfigdata module after the changes introduced by #15298:
190
207
# get_config_vars() is called by get_platform() as part of the
@@ -196,16 +213,13 @@ def _generate_posix_vars():
196
213
# _sysconfigdata module manually and populate it with the build vars.
197
214
# This is more than sufficient for ensuring the subsequent call to
198
215
# 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
205
221
206
- pybuilddir = f'build/lib.{ get_platform ()} -{ get_python_version ()} '
207
- if hasattr (sys , "gettotalrefcount" ):
208
- pybuilddir += '-pydebug'
222
+ pybuilddir = _get_pybuilddir ()
209
223
os .makedirs (pybuilddir , exist_ok = True )
210
224
destfile = os .path .join (pybuilddir , name + '.py' )
211
225
@@ -215,6 +229,11 @@ def _generate_posix_vars():
215
229
f .write ('build_time_vars = ' )
216
230
_print_config_dict (vars , stream = f )
217
231
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
+
218
237
# Create file used for sys.path fixup -- see Modules/getpath.c
219
238
with open ('pybuilddir.txt' , 'w' , encoding = 'utf8' ) as f :
220
239
f .write (pybuilddir )
0 commit comments