diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 4a77f0c..3d38673 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -735,8 +735,13 @@ def _parse(source, state, verbose): if flags is None: # global flags if pos != 3: # "(?x" import warnings - warnings.warn('Flags not at the start of the expression', - DeprecationWarning, stacklevel=7) + warnings.warn( + '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 add_flags, del_flags = flags group = None diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index eb1aba3..9ffbb4d 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1323,8 +1323,19 @@ class ReTests(unittest.TestCase): self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char)) self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char)) - with self.assertWarns(DeprecationWarning): + with self.assertWarns(DeprecationWarning) as warns: self.assertTrue(re.match(upper_char + '(?i)', lower_char)) + self.assertEqual( + str(warns.warnings[0].message), + 'Flags not at the start of the expression Ạ(?i)' + ) + + with self.assertWarns(DeprecationWarning) as warns: + self.assertTrue(re.match(upper_char + '(?i)%s' % ('.?' * 100), lower_char)) + self.assertEqual( + str(warns.warnings[0].message), + 'Flags not at the start of the expression Ạ(?i).?.?.?.?.?.?.?. (truncated)' + ) def test_dollar_matches_twice(self): "$ matches the end of string, and just before the terminating \n"