Skip to content

Commit abc310b

Browse files
committed
Switch global vars from regex objects to raw strings and import re only when needed
1 parent 6015255 commit abc310b

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Lib/distutils/extension.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
modules in setup scripts."""
55

66
import os
7+
import re
78
import warnings
89

910
# This class is really only used by the "build_ext" command, so it might
@@ -161,7 +162,7 @@ def read_setup_file(filename):
161162
line = file.readline()
162163
if line is None: # eof
163164
break
164-
if _variable_rx.match(line): # VAR=VALUE, handled in first pass
165+
if re.match(_variable_rx, line): # VAR=VALUE, handled in first pass
165166
continue
166167

167168
if line[0] == line[-1] == "*":

Lib/sysconfig.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,9 @@ def joinuser(*args):
133133

134134
# Regexes needed for parsing Makefile (and similar syntaxes,
135135
# like old-style Setup files).
136-
try:
137-
import re
138-
_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
139-
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
140-
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
141-
except ImportError:
142-
pass
136+
_variable_rx = r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)"
137+
_findvar1_rx = r"\$\(([A-Za-z][A-Za-z0-9_]*)\)"
138+
_findvar2_rx = r"\${([A-Za-z][A-Za-z0-9_]*)}"
143139

144140

145141
def _safe_realpath(path):
@@ -233,6 +229,8 @@ def _parse_makefile(filename, vars=None, keep_unresolved=True):
233229
optional dictionary is passed in as the second argument, it is
234230
used instead of a new dictionary.
235231
"""
232+
import re
233+
236234
if vars is None:
237235
vars = {}
238236
done = {}
@@ -245,7 +243,7 @@ def _parse_makefile(filename, vars=None, keep_unresolved=True):
245243
for line in lines:
246244
if line.startswith('#') or line.strip() == '':
247245
continue
248-
m = _variable_rx.match(line)
246+
m = re.match(_variable_rx, line)
249247
if m:
250248
n, v = m.group(1, 2)
251249
v = v.strip()
@@ -278,8 +276,8 @@ def _parse_makefile(filename, vars=None, keep_unresolved=True):
278276
while len(variables) > 0:
279277
for name in tuple(variables):
280278
value = notdone[name]
281-
m1 = _findvar1_rx.search(value)
282-
m2 = _findvar2_rx.search(value)
279+
m1 = re.search(_findvar1_rx, value)
280+
m2 = re.search(_findvar2_rx, value)
283281
if m1 and m2:
284282
m = m1 if m1.start() < m2.start() else m2
285283
else:
@@ -812,6 +810,7 @@ def expand_makefile_vars(s, vars):
812810
variable expansions; if 'vars' is the output of 'parse_makefile()',
813811
you're fine. Returns a variable-expanded version of 's'.
814812
"""
813+
import re
815814

816815
# This algorithm does multiple expansion, so if vars['foo'] contains
817816
# "${bar}", it will expand ${foo} to ${bar}, and then expand
@@ -820,7 +819,7 @@ def expand_makefile_vars(s, vars):
820819
# according to make's variable expansion semantics.
821820

822821
while True:
823-
m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
822+
m = re.search(_findvar1_rx, s) or re.search(_findvar2_rx, s)
824823
if m:
825824
(beg, end) = m.span()
826825
s = s[0:beg] + vars.get(m.group(1)) + s[end:]

0 commit comments

Comments
 (0)