Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 171b9a3

Browse files
rowilliaserhiy-storchaka
authored andcommitted
bpo-30605: Fix compiling binary regexs with BytesWarnings enabled. (#2016)
Running our unit tests with `-bb` enabled triggered this failure.
1 parent 7445381 commit 171b9a3

4 files changed

Lines changed: 19 additions & 3 deletions

File tree

Lib/sre_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ def _parse(source, state, verbose, nested, first=False):
765765
if not first or subpattern:
766766
import warnings
767767
warnings.warn(
768-
'Flags not at the start of the expression %s%s' % (
768+
'Flags not at the start of the expression %r%s' % (
769769
source.string[:20], # truncate long regexes
770770
' (truncated)' if len(source.string) > 20 else '',
771771
),

Lib/test/test_re.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ def test_inline_flags(self):
13681368
self.assertTrue(re.match(p, lower_char))
13691369
self.assertEqual(
13701370
str(warns.warnings[0].message),
1371-
'Flags not at the start of the expression %s' % p
1371+
'Flags not at the start of the expression %r' % p
13721372
)
13731373
self.assertEqual(warns.warnings[0].filename, __file__)
13741374

@@ -1377,10 +1377,22 @@ def test_inline_flags(self):
13771377
self.assertTrue(re.match(p, lower_char))
13781378
self.assertEqual(
13791379
str(warns.warnings[0].message),
1380-
'Flags not at the start of the expression %s (truncated)' % p[:20]
1380+
'Flags not at the start of the expression %r (truncated)' % p[:20]
13811381
)
13821382
self.assertEqual(warns.warnings[0].filename, __file__)
13831383

1384+
# bpo-30605: Compiling a bytes instance regex was throwing a BytesWarning
1385+
with warnings.catch_warnings():
1386+
warnings.simplefilter('error', BytesWarning)
1387+
p = b'A(?i)'
1388+
with self.assertWarns(DeprecationWarning) as warns:
1389+
self.assertTrue(re.match(p, b'a'))
1390+
self.assertEqual(
1391+
str(warns.warnings[0].message),
1392+
'Flags not at the start of the expression %r' % p
1393+
)
1394+
self.assertEqual(warns.warnings[0].filename, __file__)
1395+
13841396
with self.assertWarns(DeprecationWarning):
13851397
self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char))
13861398
with self.assertWarns(DeprecationWarning):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ Jakub Wilk
16851685
Gerald S. Williams
16861686
Jason Williams
16871687
John Williams
1688+
Roy Williams
16881689
Sue Williams
16891690
Carol Willing
16901691
Steven Willis

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ Extension Modules
350350
Library
351351
-------
352352

353+
- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a
354+
bytes instance with misplaced inline modifier. Patch by Roy Williams.
355+
353356
- bpo-29870: Fix ssl sockets leaks when connection is aborted in asyncio/ssl
354357
implementation. Patch by Michaël Sghaïer.
355358

0 commit comments

Comments
 (0)