Skip to content

Commit 523a243

Browse files
[3.6] bpo-30605: Fix compiling binary regexs with BytesWarnings enabled. (GH-2016) (#2214)
Running our unit tests with `-bb` enabled triggered this failure.. (cherry picked from commit 171b9a3)
1 parent 86b9537 commit 523a243

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

Lib/sre_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def _parse(source, state, verbose, nested, first=False):
736736
if not first or subpattern:
737737
import warnings
738738
warnings.warn(
739-
'Flags not at the start of the expression %s%s' % (
739+
'Flags not at the start of the expression %r%s' % (
740740
source.string[:20], # truncate long regexes
741741
' (truncated)' if len(source.string) > 20 else '',
742742
),

Lib/test/test_re.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ def test_inline_flags(self):
13461346
self.assertTrue(re.match(p, lower_char))
13471347
self.assertEqual(
13481348
str(warns.warnings[0].message),
1349-
'Flags not at the start of the expression %s' % p
1349+
'Flags not at the start of the expression %r' % p
13501350
)
13511351
self.assertEqual(warns.warnings[0].filename, __file__)
13521352

@@ -1355,10 +1355,22 @@ def test_inline_flags(self):
13551355
self.assertTrue(re.match(p, lower_char))
13561356
self.assertEqual(
13571357
str(warns.warnings[0].message),
1358-
'Flags not at the start of the expression %s (truncated)' % p[:20]
1358+
'Flags not at the start of the expression %r (truncated)' % p[:20]
13591359
)
13601360
self.assertEqual(warns.warnings[0].filename, __file__)
13611361

1362+
# bpo-30605: Compiling a bytes instance regex was throwing a BytesWarning
1363+
with warnings.catch_warnings():
1364+
warnings.simplefilter('error', BytesWarning)
1365+
p = b'A(?i)'
1366+
with self.assertWarns(DeprecationWarning) as warns:
1367+
self.assertTrue(re.match(p, b'a'))
1368+
self.assertEqual(
1369+
str(warns.warnings[0].message),
1370+
'Flags not at the start of the expression %r' % p
1371+
)
1372+
self.assertEqual(warns.warnings[0].filename, __file__)
1373+
13621374
with self.assertWarns(DeprecationWarning):
13631375
self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char))
13641376
with self.assertWarns(DeprecationWarning):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,7 @@ Jakub Wilk
16631663
Gerald S. Williams
16641664
Jason Williams
16651665
John Williams
1666+
Roy Williams
16661667
Sue Williams
16671668
Carol Willing
16681669
Steven Willis

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Core and Builtins
5151
Library
5252
-------
5353

54+
- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a
55+
bytes instance with misplaced inline modifier. Patch by Roy Williams.
56+
5457
- [Security] bpo-29591: Update expat copy from 2.1.1 to 2.2.0 to get fixes
5558
of CVE-2016-0718 and CVE-2016-4472. See
5659
https://sourceforge.net/p/expat/bugs/537/ for more information.

0 commit comments

Comments
 (0)