(original) (raw)

changeset: 86213:406529adf156 user: Christian Heimes christian@cheimes.de date: Sat Oct 12 00:24:55 2013 +0200 files: Lib/site.py Lib/sysconfig.py Lib/test/test_site.py Misc/NEWS description: Issue #19205: Don't import the 're' module in site and sysconfig module to to speed up interpreter start. diff -r fbbf8b160e8d -r 406529adf156 Lib/site.py --- a/Lib/site.py Sat Oct 12 00:13:50 2013 +0200 +++ b/Lib/site.py Sat Oct 12 00:24:55 2013 +0200 @@ -70,7 +70,6 @@ import sys import os -import re import builtins import _sitebuiltins @@ -436,8 +435,7 @@ encodings._cache[enc] = encodings._unknown encodings.aliases.aliases[enc] = 'mbcs' - -CONFIG_LINE = re.compile(r'^(?P(\w|[-_])+)\s*=\s*(?P.*)\s*$') +CONFIG_LINE = r'^(?P(\w|[-_])+)\s*=\s*(?P.*)\s*$' def venv(known_paths): global PREFIXES, ENABLE_USER_SITE @@ -460,6 +458,8 @@ ] if candidate_confs: + import re + config_line = re.compile(CONFIG_LINE) virtual_conf = candidate_confs[0] system_site = "true" with open(virtual_conf) as f: diff -r fbbf8b160e8d -r 406529adf156 Lib/sysconfig.py --- a/Lib/sysconfig.py Sat Oct 12 00:13:50 2013 +0200 +++ b/Lib/sysconfig.py Sat Oct 12 00:24:55 2013 +0200 @@ -1,7 +1,6 @@ """Access to Python's configuration information.""" import os -import re import sys from os.path import pardir, realpath @@ -222,6 +221,7 @@ """ # Regexes needed for parsing Makefile (and similar syntaxes, # like old-style Setup files). + import re _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") @@ -435,6 +435,7 @@ """ if vars is None: vars = {} + import re define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") @@ -658,6 +659,7 @@ return "%s-%s.%s" % (osname, version, release) elif osname[:6] == "cygwin": osname = "cygwin" + import re rel_re = re.compile(r'[\d.]+') m = rel_re.match(release) if m: diff -r fbbf8b160e8d -r 406529adf156 Lib/test/test_site.py --- a/Lib/test/test_site.py Sat Oct 12 00:13:50 2013 +0200 +++ b/Lib/test/test_site.py Sat Oct 12 00:24:55 2013 +0200 @@ -420,5 +420,20 @@ self.assertEqual(code, 200, msg="Can't find " + url) +class StartupImportTests(unittest.TestCase): + + def test_startup_imports(self): + # This tests checks which modules are loaded by Python when it + # initially starts upon startup. + args = [sys.executable, '-I', '-c', + 'import sys; print(set(sys.modules))'] + stdout = subprocess.check_output(args) + modules = eval(stdout.decode('utf-8')) + self.assertIn('site', modules) + + re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'} + self.assertFalse(modules.intersection(re_mods)) + + if __name__ == "__main__": unittest.main() diff -r fbbf8b160e8d -r 406529adf156 Misc/NEWS --- a/Misc/NEWS Sat Oct 12 00:13:50 2013 +0200 +++ b/Misc/NEWS Sat Oct 12 00:24:55 2013 +0200 @@ -36,6 +36,9 @@ Library ------- +- Issue #19205: Don't import the 're' module in site and sysconfig module to + to speed up interpreter start. + - Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module./christian@cheimes.de