Skip to content

Commit 3ac3b13

Browse files
FFY00ebonnal
authored andcommitted
pythonGH-127178: install a _sysconfig_vars_(...).json file in the stdlib directory (python#127302)
1 parent a4841be commit 3ac3b13

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

Lib/sysconfig/__main__.py

+28-9
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

+31-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from test.support import (
1313
captured_stdout,
14+
is_android,
1415
is_apple_mobile,
1516
is_wasi,
1617
PythonSymlink,
@@ -25,8 +26,9 @@
2526
from sysconfig import (get_paths, get_platform, get_config_vars,
2627
get_path, get_path_names, _INSTALL_SCHEMES,
2728
get_default_scheme, get_scheme_names, get_config_var,
28-
_expand_vars, _get_preferred_schemes)
29-
from sysconfig.__main__ import _main, _parse_makefile
29+
_expand_vars, _get_preferred_schemes,
30+
is_python_build, _PROJECT_BASE)
31+
from sysconfig.__main__ import _main, _parse_makefile, _get_pybuilddir, _get_json_data_name
3032
import _imp
3133
import _osx_support
3234
import _sysconfig
@@ -39,6 +41,7 @@ class TestSysConfig(unittest.TestCase):
3941

4042
def setUp(self):
4143
super(TestSysConfig, self).setUp()
44+
self.maxDiff = None
4245
self.sys_path = sys.path[:]
4346
# patching os.uname
4447
if hasattr(os, 'uname'):
@@ -625,6 +628,32 @@ def test_makefile_overwrites_config_vars(self):
625628
self.assertNotEqual(data['prefix'], data['base_prefix'])
626629
self.assertNotEqual(data['exec_prefix'], data['base_exec_prefix'])
627630

631+
@unittest.skipIf(os.name != 'posix', '_sysconfig-vars JSON file is only available on POSIX')
632+
@unittest.skipIf(is_wasi, "_sysconfig-vars JSON file currently isn't available on WASI")
633+
@unittest.skipIf(is_android or is_apple_mobile, 'Android and iOS change the prefix')
634+
def test_sysconfigdata_json(self):
635+
if '_PYTHON_SYSCONFIGDATA_PATH' in os.environ:
636+
data_dir = os.environ['_PYTHON_SYSCONFIGDATA_PATH']
637+
elif is_python_build():
638+
data_dir = os.path.join(_PROJECT_BASE, _get_pybuilddir())
639+
else:
640+
data_dir = sys._stdlib_dir
641+
642+
json_data_path = os.path.join(data_dir, _get_json_data_name())
643+
644+
with open(json_data_path) as f:
645+
json_config_vars = json.load(f)
646+
647+
system_config_vars = get_config_vars()
648+
649+
# Ignore keys in the check
650+
for key in ('projectbase', 'srcdir'):
651+
json_config_vars.pop(key)
652+
system_config_vars.pop(key)
653+
654+
self.assertEqual(system_config_vars, json_config_vars)
655+
656+
628657
class MakefileTests(unittest.TestCase):
629658

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

Makefile.pre.in

+2-2
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
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)