Commit abf275af authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22493: Warning message emitted by using inline flags in the middle of

regular expression now contains a (truncated) regex pattern.
Patch by Tim Graham.
parent 8761e59f
...@@ -735,8 +735,13 @@ def _parse(source, state, verbose): ...@@ -735,8 +735,13 @@ def _parse(source, state, verbose):
if flags is None: # global flags if flags is None: # global flags
if pos != 3: # "(?x" if pos != 3: # "(?x"
import warnings import warnings
warnings.warn('Flags not at the start of the expression', warnings.warn(
DeprecationWarning, stacklevel=7) 'Flags not at the start of the expression %s%s' % (
source.string[:20], # truncate long regexes
' (truncated)' if len(source.string) > 20 else '',
),
DeprecationWarning, stacklevel=7
)
continue continue
add_flags, del_flags = flags add_flags, del_flags = flags
group = None group = None
......
...@@ -1323,8 +1323,21 @@ class ReTests(unittest.TestCase): ...@@ -1323,8 +1323,21 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char)) self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char))
self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char)) self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char))
with self.assertWarns(DeprecationWarning): p = upper_char + '(?i)'
self.assertTrue(re.match(upper_char + '(?i)', lower_char)) with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.match(p, lower_char))
self.assertEqual(
str(warns.warnings[0].message),
'Flags not at the start of the expression %s' % p
)
p = upper_char + '(?i)%s' % ('.?' * 100)
with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.match(p, lower_char))
self.assertEqual(
str(warns.warnings[0].message),
'Flags not at the start of the expression %s (truncated)' % p[:20]
)
def test_dollar_matches_twice(self): def test_dollar_matches_twice(self):
"$ matches the end of string, and just before the terminating \n" "$ matches the end of string, and just before the terminating \n"
......
...@@ -27,6 +27,10 @@ Core and Builtins ...@@ -27,6 +27,10 @@ Core and Builtins
Library Library
------- -------
- Issue #22493: Warning message emitted by using inline flags in the middle of
regular expression now contains a (truncated) regex pattern.
Patch by Tim Graham.
- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when - Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
an empty bytestring is passed. an empty bytestring is passed.
......
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