#2834: Change re module semantics, so that str and bytes mixing is fo… · pythoncapi/cpython@fd03645 · GitHub
Skip to content

Commit fd03645

Browse files
committed
python#2834: Change re module semantics, so that str and bytes mixing is forbidden,
and str (unicode) patterns get full unicode matching by default. The re.ASCII flag is also introduced to ask for ASCII matching instead.
1 parent 3ad7ba1 commit fd03645

37 files changed

Lines changed: 280 additions & 163 deletions

Doc/library/re.rst

Lines changed: 77 additions & 50 deletions

Lib/_strptime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import locale
1515
import calendar
1616
from re import compile as re_compile
17-
from re import IGNORECASE
17+
from re import IGNORECASE, ASCII
1818
from re import escape as re_escape
1919
from datetime import date as datetime_date
2020
try:
@@ -262,7 +262,7 @@ def pattern(self, format):
262262

263263
def compile(self, format):
264264
"""Return a compiled re object for the format string."""
265-
return re_compile(self.pattern(format), IGNORECASE)
265+
return re_compile(self.pattern(format), IGNORECASE | ASCII)
266266

267267
_cache_lock = _thread_allocate_lock()
268268
# DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock

Lib/base64.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _translate(s, altchars):
3939
return s.translate(translation)
4040

4141

42-
42+
4343
# Base64 encoding/decoding uses binascii
4444

4545
def b64encode(s, altchars=None):
@@ -126,7 +126,7 @@ def urlsafe_b64decode(s):
126126
return b64decode(s, b'-_')
127127

128128

129-
129+
130130
# Base32 encoding/decoding must be done in Python
131131
_b32alphabet = {
132132
0: b'A', 9: b'J', 18: b'S', 27: b'3',
@@ -225,7 +225,7 @@ def b32decode(s, casefold=False, map01=None):
225225
# characters because this will tell us how many null bytes to remove from
226226
# the end of the decoded string.
227227
padchars = 0
228-
mo = re.search('(?P<pad>[=]*)$', s)
228+
mo = re.search(b'(?P<pad>[=]*)$', s)
229229
if mo:
230230
padchars = len(mo.group('pad'))
231231
if padchars > 0:
@@ -262,7 +262,7 @@ def b32decode(s, casefold=False, map01=None):
262262
return b''.join(parts)
263263

264264

265-
265+
266266
# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
267267
# lowercase. The RFC also recommends against accepting input case
268268
# insensitively.
@@ -291,12 +291,12 @@ def b16decode(s, casefold=False):
291291
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
292292
if casefold:
293293
s = s.upper()
294-
if re.search('[^0-9A-F]', s):
294+
if re.search(b'[^0-9A-F]', s):
295295
raise binascii.Error('Non-base16 digit found')
296296
return binascii.unhexlify(s)
297297

298298

299-
299+
300300
# Legacy interface. This code could be cleaned up since I don't believe
301301
# binascii has any line length limitations. It just doesn't seem worth it
302302
# though. The files should be opened in binary mode.
@@ -353,7 +353,7 @@ def decodestring(s):
353353
return binascii.a2b_base64(s)
354354

355355

356-
356+
357357
# Usable as a script...
358358
def main():
359359
"""Small main program"""

Lib/decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5415,7 +5415,7 @@ def _convert_other(other, raiseit=False):
54155415
# 2. For finite numbers (not infinities and NaNs) the body of the
54165416
# number between the optional sign and the optional exponent must have
54175417
# at least one decimal digit, possibly after the decimal point. The
5418-
# lookahead expression '(?=\d|\.\d)' checks this.
5418+
# lookahead expression '(?=[0-9]|\.[0-9])' checks this.
54195419
#
54205420
# As the flag UNICODE is not enabled here, we're explicitly avoiding any
54215421
# other meaning for \d than the numbers [0-9].

Lib/distutils/cygwinccompiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def get_versions():
409409
out = os.popen(gcc_exe + ' -dumpversion','r')
410410
out_string = out.read()
411411
out.close()
412-
result = re.search('(\d+\.\d+(\.\d+)*)',out_string)
412+
result = re.search('(\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
413413
if result:
414414
gcc_version = StrictVersion(result.group(1))
415415
else:
@@ -421,7 +421,7 @@ def get_versions():
421421
out = os.popen(ld_exe + ' -v','r')
422422
out_string = out.read()
423423
out.close()
424-
result = re.search('(\d+\.\d+(\.\d+)*)',out_string)
424+
result = re.search('(\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
425425
if result:
426426
ld_version = StrictVersion(result.group(1))
427427
else:
@@ -433,7 +433,7 @@ def get_versions():
433433
out = os.popen(dllwrap_exe + ' --version','r')
434434
out_string = out.read()
435435
out.close()
436-
result = re.search(' (\d+\.\d+(\.\d+)*)',out_string)
436+
result = re.search(' (\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
437437
if result:
438438
dllwrap_version = StrictVersion(result.group(1))
439439
else:

Lib/distutils/emxccompiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def get_versions():
300300
out = os.popen(gcc_exe + ' -dumpversion','r')
301301
out_string = out.read()
302302
out.close()
303-
result = re.search('(\d+\.\d+\.\d+)',out_string)
303+
result = re.search('(\d+\.\d+\.\d+)', out_string, re.ASCII)
304304
if result:
305305
gcc_version = StrictVersion(result.group(1))
306306
else:

Lib/distutils/sysconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def get_config_vars(*args):
512512
# patched up as well.
513513
'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
514514
flags = _config_vars[key]
515-
flags = re.sub('-arch\s+\w+\s', ' ', flags)
515+
flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
516516
flags = re.sub('-isysroot [^ \t]*', ' ', flags)
517517
_config_vars[key] = flags
518518

Lib/distutils/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_platform ():
8181
return "%s-%s.%s" % (osname, version, release)
8282
elif osname[:6] == "cygwin":
8383
osname = "cygwin"
84-
rel_re = re.compile (r'[\d.]+')
84+
rel_re = re.compile (r'[\d.]+', re.ASCII)
8585
m = rel_re.match(release)
8686
if m:
8787
release = m.group()

Lib/distutils/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class StrictVersion (Version):
134134
"""
135135

136136
version_re = re.compile(r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$',
137-
re.VERBOSE)
137+
re.VERBOSE | re.ASCII)
138138

139139

140140
def parse (self, vstring):

Lib/distutils/versionpredicate.py

Lines changed: 4 additions & 2 deletions

0 commit comments

Comments
 (0)