Skip to content

Commit 99b8236

Browse files
committed
GH-127178: install a _sysconfig_vars_(...).json in the stdlib directory
Signed-off-by: Filipe Laíns <[email protected]>
1 parent dcf6292 commit 99b8236

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

Lib/sysconfig/__main__.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
import os
23
import sys
4+
import types
35
from 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+
160175
def _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)

Lib/test/test_sysconfig.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
from sysconfig import (get_paths, get_platform, get_config_vars,
2626
get_path, get_path_names, _INSTALL_SCHEMES,
2727
get_default_scheme, get_scheme_names, get_config_var,
28-
_expand_vars, _get_preferred_schemes)
29-
from sysconfig.__main__ import _main, _parse_makefile
28+
_expand_vars, _get_preferred_schemes,
29+
is_python_build, _PROJECT_BASE)
30+
from sysconfig.__main__ import _main, _parse_makefile, _get_pybuilddir, _get_json_data_name
3031
import _imp
3132
import _osx_support
3233
import _sysconfig
@@ -625,6 +626,22 @@ def test_makefile_overwrites_config_vars(self):
625626
self.assertNotEqual(data['prefix'], data['base_prefix'])
626627
self.assertNotEqual(data['exec_prefix'], data['base_exec_prefix'])
627628

629+
def test_sysconfigdata_json(self):
630+
if '_PYTHON_SYSCONFIGDATA_PATH' in os.environ:
631+
data_dir = os.environ['_PYTHON_SYSCONFIGDATA_PATH']
632+
elif is_python_build():
633+
data_dir = os.path.join(_PROJECT_BASE, _get_pybuilddir())
634+
else:
635+
data_dir = sys._stdlib_dir
636+
637+
json_data_path = os.path.join(data_dir, _get_json_data_name())
638+
639+
with open(json_data_path) as f:
640+
json_config_vars = json.load(f)
641+
642+
self.assertEqual(get_config_vars(), json_config_vars)
643+
644+
628645
class MakefileTests(unittest.TestCase):
629646

630647
@unittest.skipIf(sys.platform.startswith('win'),

Makefile.pre.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,8 +2645,8 @@ libinstall: all $(srcdir)/Modules/xxmodule.c
26452645
esac; \
26462646
done; \
26472647
done
2648-
$(INSTALL_DATA) `cat pybuilddir.txt`/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py \
2649-
$(DESTDIR)$(LIBDEST); \
2648+
$(INSTALL_DATA) `cat pybuilddir.txt`/_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).py $(DESTDIR)$(LIBDEST); \
2649+
$(INSTALL_DATA) `cat pybuilddir.txt`/_sysconfig_vars_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH).json $(DESTDIR)$(LIBDEST); \
26502650
$(INSTALL_DATA) $(srcdir)/LICENSE $(DESTDIR)$(LIBDEST)/LICENSE.txt
26512651
@ # If app store compliance has been configured, apply the patch to the
26522652
@ # installed library code. The patch has been previously validated against
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A ``_sysconfig_vars_(...).json`` file is now shipped in the standard library
2+
directory. It contains the output of :func:`sysconfig.get_config_vars` on
3+
the default environment encoded as JSON data. This is an implementation
4+
detail, and may change at any time.

0 commit comments

Comments
 (0)