diff -r a4515186bf9c Lib/locale.py --- a/Lib/locale.py Fri Oct 11 12:09:51 2013 -0400 +++ b/Lib/locale.py Fri Oct 11 22:10:08 2013 +0200 @@ -14,8 +14,7 @@ import sys import encodings import encodings.aliases -import re -import collections +from collections.abc import Mapping from builtins import str as _builtin_str import functools @@ -178,8 +177,9 @@ amount -= 1 return s[lpos:rpos+1] -_percent_re = re.compile(r'%(?:\((?P.*?)\))?' - r'(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') +# Compilation of _percent_re is delayed to reduce startup time. +_percent_re = (r'%(?:\((?P.*?)\))?' + r'(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') def format(percent, value, grouping=False, monetary=False, *additional): """Returns the locale-aware substitution of a %? specifier @@ -187,6 +187,10 @@ additional is for format strings which contain one or more '*' modifiers.""" + global _percent_re + if isinstance(_percent_re, str): + import re + _percent_re = re.compile(_percent_re) # this is only for one-percent-specifier strings and this should be checked match = _percent_re.match(percent) if not match or len(match.group())!= len(percent): @@ -222,10 +226,14 @@ """Formats a string in the same way that the % formatting would use, but takes the current locale into account. Grouping is applied if the third parameter is true.""" + global _percent_re + if isinstance(_percent_re, str): + import re + _percent_re = re.compile(_percent_re) percents = list(_percent_re.finditer(f)) new_f = _percent_re.sub('%s', f) - if isinstance(val, collections.Mapping): + if isinstance(val, Mapping): new_val = [] for perc in percents: if perc.group()[-1]=='%': diff -r a4515186bf9c Lib/site.py --- a/Lib/site.py Fri Oct 11 12:09:51 2013 -0400 +++ b/Lib/site.py Fri Oct 11 22:10:08 2013 +0200 @@ -70,7 +70,6 @@ import sys import os -import re import builtins import _sitebuiltins @@ -436,11 +435,10 @@ encodings._cache[enc] = encodings._unknown encodings.aliases.aliases[enc] = 'mbcs' - -CONFIG_LINE = re.compile(r'^(?P(\w|[-_])+)\s*=\s*(?P.*)\s*$') +CONFIG_LINE = None def venv(known_paths): - global PREFIXES, ENABLE_USER_SITE + global PREFIXES, ENABLE_USER_SITE, CONFIG_LINE env = os.environ if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: @@ -460,6 +458,10 @@ ] if candidate_confs: + if CONFIG_LINE is None: + import re + CONFIG_LINE = re.compile(r'^(?P(\w|[-_])+)\s*' + r'=\s*(?P.*)\s*$') virtual_conf = candidate_confs[0] system_site = "true" with open(virtual_conf) as f: diff -r a4515186bf9c Lib/sysconfig.py --- a/Lib/sysconfig.py Fri Oct 11 12:09:51 2013 -0400 +++ b/Lib/sysconfig.py Fri Oct 11 22:10:08 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 a4515186bf9c Lib/test/test_site.py --- a/Lib/test/test_site.py Fri Oct 11 12:09:51 2013 -0400 +++ b/Lib/test/test_site.py Fri Oct 11 22:10:08 2013 +0200 @@ -18,6 +18,7 @@ import subprocess import sysconfig from copy import copy +import ast # Need to make sure to not import 'site' if someone specified ``-S`` at the # command-line. Detect this by just making sure 'site' has not been imported @@ -420,5 +421,19 @@ 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(sorted(sys.modules))'] + stdout = subprocess.check_output(args).decode('utf-8') + modules = set(ast.literal_eval(stdout)) + + re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'} + self.assertFalse(modules.intersection(re_mods)) + + if __name__ == "__main__": unittest.main()