]> granicus.if.org Git - python/commitdiff
Issue #22493: Warning message emitted by using inline flags in the middle of
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 16 Sep 2016 22:29:58 +0000 (01:29 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 16 Sep 2016 22:29:58 +0000 (01:29 +0300)
regular expression now contains a (truncated) regex pattern.
Patch by Tim Graham.

Lib/sre_parse.py
Lib/test/test_re.py
Misc/NEWS

index 4a77f0c9a7d221d32e15e97b4fb23f6b6956a835..3d38673c322e635b0d5904c1bd2b6ecec443cfb6 100644 (file)
@@ -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
index eb1aba39c31fd041c7a071ab9b2df9d9b7134010..6803e02c0e2c83dd62bc839d5b9a9247577174c3 100644 (file)
@@ -1323,8 +1323,21 @@ 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):
-            self.assertTrue(re.match(upper_char + '(?i)', lower_char))
+        p = upper_char + '(?i)'
+        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):
         "$ matches the end of string, and just before the terminating \n"
index c76502853a82822d44256790a845b79c47d3a9c8..8fdeb79c79a01f4503aa49fd8db7b3586f902d6e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,10 @@ Core and Builtins
 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
   an empty bytestring is passed.