[Python-checkins] cpython (merge 3.6 -> default): Issue #22493: Warning message emitted by using inline flags in the middle of

serhiy.storchaka python-checkins at python.org
Fri Sep 16 18:31:07 EDT 2016


https://hg.python.org/cpython/rev/9d0f4da4d531
changeset:   103870:9d0f4da4d531
parent:      103868:ec856bd0b009
parent:      103869:c35a528268fd
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Sep 17 01:30:48 2016 +0300
summary:
  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.

files:
  Lib/sre_parse.py    |   9 +++++++--
  Lib/test/test_re.py |  17 +++++++++++++++--
  Misc/NEWS           |   4 ++++
  3 files changed, 26 insertions(+), 4 deletions(-)


diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -735,8 +735,13 @@
                     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
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1323,8 +1323,21 @@
         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"
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,10 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list