Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e73a24

Browse files
committed
Issue #13150: sysconfig no longer parses the Makefile and config.h files
when imported, instead doing it at build time. This makes importing sysconfig faster and reduces Python startup time by 20%.
1 parent cf28eac commit 1e73a24

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

.hgignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ libpython*.so*
4949
*.pyd
5050
*.cover
5151
*~
52+
Lib/_sysconfigdata.py
5253
Lib/lib2to3/*.pickle
5354
Lib/test/data/*
5455
Misc/*.wpu

Lib/sysconfig.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,11 @@ def get_makefile_filename():
319319
config_dir_name = 'config'
320320
return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile')
321321

322-
323-
def _init_posix(vars):
324-
"""Initialize the module as appropriate for POSIX systems."""
322+
def _generate_posix_vars():
323+
"""Generate the Python module containing build-time variables."""
324+
import pprint
325+
vars = {}
326+
destfile = os.path.join(os.path.dirname(__file__), '_sysconfigdata.py')
325327
# load the installed Makefile:
326328
makefile = get_makefile_filename()
327329
try:
@@ -346,7 +348,15 @@ def _init_posix(vars):
346348
# the scripts are in another directory.
347349
if _PYTHON_BUILD:
348350
vars['LDSHARED'] = vars['BLDSHARED']
351+
with open(destfile, 'w', encoding='utf8') as f:
352+
f.write('build_time_vars = ')
353+
pprint.pprint(vars, stream=f)
349354

355+
def _init_posix(vars):
356+
"""Initialize the module as appropriate for POSIX systems."""
357+
# _sysconfigdata is generated at build time, see _generate_posix_vars()
358+
from _sysconfigdata import build_time_vars
359+
vars.update(build_time_vars)
350360

351361
def _init_non_posix(vars):
352362
"""Initialize the module as appropriate for NT"""
@@ -753,6 +763,9 @@ def _print_dict(title, data):
753763

754764
def _main():
755765
"""Display all information sysconfig detains."""
766+
if '--generate-posix-vars' in sys.argv:
767+
_generate_posix_vars()
768+
return
756769
print('Platform: "%s"' % get_platform())
757770
print('Python version: "%s"' % get_python_version())
758771
print('Current installation scheme: "%s"' % _get_default_scheme())

Makefile.pre.in

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ LIBRARY_OBJS= \
396396

397397
# Default target
398398
all: build_all
399-
build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed
399+
build_all: $(BUILDPYTHON) sysconfig oldsharedmods sharedmods gdbhooks Modules/_testembed
400400

401401
# Compile a binary with gcc profile guided optimization.
402402
profile-opt:
@@ -429,12 +429,15 @@ coverage:
429429
$(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
430430
$(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
431431

432-
platform: $(BUILDPYTHON)
432+
platform: $(BUILDPYTHON) sysconfig
433433
$(RUNSHARED) ./$(BUILDPYTHON) -E -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:3])' >platform
434434

435+
# Generate the sysconfig build-time data
436+
sysconfig: $(BUILDPYTHON)
437+
$(RUNSHARED) ./$(BUILDPYTHON) -SE -m sysconfig --generate-posix-vars
435438

436439
# Build the shared modules
437-
sharedmods: $(BUILDPYTHON)
440+
sharedmods: $(BUILDPYTHON) sysconfig
438441
@case $$MAKEFLAGS in \
439442
*s*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
440443
*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
@@ -1379,7 +1382,7 @@ patchcheck:
13791382
Python/thread.o: @THREADHEADERS@
13801383

13811384
# Declare targets that aren't real files
1382-
.PHONY: all build_all sharedmods oldsharedmods test quicktest
1385+
.PHONY: all build_all sysconfig sharedmods oldsharedmods test quicktest
13831386
.PHONY: install altinstall oldsharedinstall bininstall altbininstall
13841387
.PHONY: maninstall libinstall inclinstall libainstall sharedinstall
13851388
.PHONY: frameworkinstall frameworkinstallframework frameworkinstallstructure

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ Core and Builtins
315315
Library
316316
-------
317317

318+
- Issue #13150: sysconfig no longer parses the Makefile and config.h files
319+
when imported, instead doing it at build time. This makes importing
320+
sysconfig faster and reduces Python startup time by 20%.
321+
318322
- Issue #12448: smtplib now flushes stdout while running ``python -m smtplib``
319323
in order to display the prompt correctly.
320324

0 commit comments

Comments
 (0)