Commit ba17bded authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22493: Inline flags now should be used only at the start of the

regular expression.  Deprecation warning is emitted if uses them in the
middle of the regular expression.
parent d0f84684
...@@ -302,21 +302,26 @@ def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0): ...@@ -302,21 +302,26 @@ def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0):
else: else:
return pattern return pattern
# ditch start and end characters
start, _, end = glob_to_re('_').partition('_')
if pattern: if pattern:
pattern_re = glob_to_re(pattern) pattern_re = glob_to_re(pattern)
assert pattern_re.startswith(start) and pattern_re.endswith(end)
else: else:
pattern_re = '' pattern_re = ''
if prefix is not None: if prefix is not None:
# ditch end of pattern character prefix_re = glob_to_re(prefix)
empty_pattern = glob_to_re('') assert prefix_re.startswith(start) and prefix_re.endswith(end)
prefix_re = glob_to_re(prefix)[:-len(empty_pattern)] prefix_re = prefix_re[len(start): len(prefix_re) - len(end)]
sep = os.sep sep = os.sep
if os.sep == '\\': if os.sep == '\\':
sep = r'\\' sep = r'\\'
pattern_re = "^" + sep.join((prefix_re, ".*" + pattern_re)) pattern_re = pattern_re[len(start): len(pattern_re) - len(end)]
pattern_re = r'%s\A%s%s.*%s%s' % (start, prefix_re, sep, pattern_re, end)
else: # no prefix -- respect anchor flag else: # no prefix -- respect anchor flag
if anchor: if anchor:
pattern_re = "^" + pattern_re pattern_re = r'%s\A%s' % (start, pattern_re[len(start):])
return re.compile(pattern_re) return re.compile(pattern_re)
...@@ -51,14 +51,14 @@ class FileListTestCase(support.LoggingSilencer, ...@@ -51,14 +51,14 @@ class FileListTestCase(support.LoggingSilencer,
for glob, regex in ( for glob, regex in (
# simple cases # simple cases
('foo*', r'foo[^%(sep)s]*\Z(?ms)'), ('foo*', r'(?s:foo[^%(sep)s]*)\Z'),
('foo?', r'foo[^%(sep)s]\Z(?ms)'), ('foo?', r'(?s:foo[^%(sep)s])\Z'),
('foo??', r'foo[^%(sep)s][^%(sep)s]\Z(?ms)'), ('foo??', r'(?s:foo[^%(sep)s][^%(sep)s])\Z'),
# special cases # special cases
(r'foo\\*', r'foo\\\\[^%(sep)s]*\Z(?ms)'), (r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'),
(r'foo\\\*', r'foo\\\\\\[^%(sep)s]*\Z(?ms)'), (r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'),
('foo????', r'foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s]\Z(?ms)'), ('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'),
(r'foo\\??', r'foo\\\\[^%(sep)s][^%(sep)s]\Z(?ms)')): (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')):
regex = regex % {'sep': sep} regex = regex % {'sep': sep}
self.assertEqual(glob_to_re(glob), regex) self.assertEqual(glob_to_re(glob), regex)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment