bpo-30605: Fix compiling binary regexs with BytesWarnings enabled. (#2016)
authorRoy Williams <roy.williams.iii@gmail.com>
Sat, 10 Jun 2017 05:01:16 +0000 (22:01 -0700)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 10 Jun 2017 05:01:16 +0000 (08:01 +0300)
Running our unit tests with `-bb` enabled triggered this failure.

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

index d59d642e644b56ff1e6cd76bcd06a504dc6073be..545252074f63d1101ff53897b5d6789e7766f35e 100644 (file)
@@ -765,7 +765,7 @@ def _parse(source, state, verbose, nested, first=False):
                         if not first or subpattern:
                             import warnings
                             warnings.warn(
-                                'Flags not at the start of the expression %s%s' % (
+                                'Flags not at the start of the expression %r%s' % (
                                     source.string[:20],  # truncate long regexes
                                     ' (truncated)' if len(source.string) > 20 else '',
                                 ),
index 027df4092da8d8dd4e187afd6027c99766fc8886..0ea5a20469646b181025b3a39d0967c01a9ae168 100644 (file)
@@ -1368,7 +1368,7 @@ class ReTests(unittest.TestCase):
             self.assertTrue(re.match(p, lower_char))
         self.assertEqual(
             str(warns.warnings[0].message),
-            'Flags not at the start of the expression %s' % p
+            'Flags not at the start of the expression %r' % p
         )
         self.assertEqual(warns.warnings[0].filename, __file__)
 
@@ -1377,10 +1377,22 @@ class ReTests(unittest.TestCase):
             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]
+            'Flags not at the start of the expression %r (truncated)' % p[:20]
         )
         self.assertEqual(warns.warnings[0].filename, __file__)
 
+        # bpo-30605: Compiling a bytes instance regex was throwing a BytesWarning
+        with warnings.catch_warnings():
+            warnings.simplefilter('error', BytesWarning)
+            p = b'A(?i)'
+            with self.assertWarns(DeprecationWarning) as warns:
+                self.assertTrue(re.match(p, b'a'))
+            self.assertEqual(
+                str(warns.warnings[0].message),
+                'Flags not at the start of the expression %r' % p
+            )
+            self.assertEqual(warns.warnings[0].filename, __file__)
+
         with self.assertWarns(DeprecationWarning):
             self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char))
         with self.assertWarns(DeprecationWarning):
index 135491d764d8565102a6c5088d4fea928983a25b..74ac15b89ece6ba22a9db3c60eb8f4d66b573887 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1685,6 +1685,7 @@ Jakub Wilk
 Gerald S. Williams
 Jason Williams
 John Williams
+Roy Williams
 Sue Williams
 Carol Willing
 Steven Willis
index 36ce9ce59af801cad343e95790aaad97947a8fa9..6f56fe0583a65f2af9e58137eda25e04dd876dc1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -350,6 +350,9 @@ Extension Modules
 Library
 -------
 
+- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a
+  bytes instance with misplaced inline modifier.  Patch by Roy Williams.
+
 - bpo-29870: Fix ssl sockets leaks when connection is aborted in asyncio/ssl 
   implementation. Patch by Michaël Sghaïer.