From 66bc7a9d10cd4ca96d09f8d1a4c7d0e1b716e7ba Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Thu, 8 Jun 2017 18:01:37 -0700 Subject: [PATCH 1/3] bpo-30605: Fix compiling binary regexs with BytesWarnings enabled. Running our unit tests with `-bb` enabled triggered this failure. --- Lib/sre_parse.py | 2 +- Lib/test/test_re.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index d59d642e644b56..545252074f63d1 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -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 '', ), diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 027df4092da8d8..3b079cffc59a89 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1368,7 +1368,7 @@ def test_inline_flags(self): 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 '%s'" % p ) self.assertEqual(warns.warnings[0].filename, __file__) @@ -1377,7 +1377,7 @@ def test_inline_flags(self): 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 '%s' (truncated)" % p[:20] ) self.assertEqual(warns.warnings[0].filename, __file__) @@ -1933,16 +1933,16 @@ def test_pattern_compare_bytes(self): # equal: test bytes patterns re.purge() - pattern2 = re.compile(b'abc') - self.assertEqual(hash(pattern2), hash(pattern1)) - self.assertEqual(pattern2, pattern1) - - # not equal: pattern of a different types (str vs bytes), - # comparison must not raise a BytesWarning - re.purge() - pattern3 = re.compile('abc') with warnings.catch_warnings(): warnings.simplefilter('error', BytesWarning) + pattern2 = re.compile(b'abc') + self.assertEqual(hash(pattern2), hash(pattern1)) + self.assertEqual(pattern2, pattern1) + + # not equal: pattern of a different types (str vs bytes), + # comparison must not raise a BytesWarning + re.purge() + pattern3 = re.compile('abc') self.assertNotEqual(pattern3, pattern1) def test_bug_29444(self): From 4f59f662681cf4f5f27adf779387dcb8f30fbfe5 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Fri, 9 Jun 2017 00:04:40 -0700 Subject: [PATCH 2/3] Address feedback from serhiy-storchaka --- Lib/test/test_re.py | 32 ++++++++++++++++++++++---------- Misc/ACKS | 1 + Misc/NEWS | 3 +++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 3b079cffc59a89..0ea5a20469646b 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1368,7 +1368,7 @@ def test_inline_flags(self): 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 @@ def test_inline_flags(self): 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): @@ -1933,16 +1945,16 @@ def test_pattern_compare_bytes(self): # equal: test bytes patterns re.purge() + pattern2 = re.compile(b'abc') + self.assertEqual(hash(pattern2), hash(pattern1)) + self.assertEqual(pattern2, pattern1) + + # not equal: pattern of a different types (str vs bytes), + # comparison must not raise a BytesWarning + re.purge() + pattern3 = re.compile('abc') with warnings.catch_warnings(): warnings.simplefilter('error', BytesWarning) - pattern2 = re.compile(b'abc') - self.assertEqual(hash(pattern2), hash(pattern1)) - self.assertEqual(pattern2, pattern1) - - # not equal: pattern of a different types (str vs bytes), - # comparison must not raise a BytesWarning - re.purge() - pattern3 = re.compile('abc') self.assertNotEqual(pattern3, pattern1) def test_bug_29444(self): diff --git a/Misc/ACKS b/Misc/ACKS index 135491d764d856..e23f28d170b418 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1685,6 +1685,7 @@ Jakub Wilk Gerald S. Williams Jason Williams John Williams +Roy Willams Sue Williams Carol Willing Steven Willis diff --git a/Misc/NEWS b/Misc/NEWS index 7d31839e6734d8..995643fa43ab22 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -646,6 +646,9 @@ Library - bpo-29851: importlib.reload() now raises ModuleNotFoundError if the module lacks a spec. +- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a + bytes instance. + - Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap, improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi, Manuel Krebber, and Łukasz Langa. From 75c94a7c1375c1003b367d2c6c5cde2cb3fffb03 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Fri, 9 Jun 2017 17:01:44 -0700 Subject: [PATCH 3/3] Fix up ACKS and NEWS --- Misc/ACKS | 2 +- Misc/NEWS | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Misc/ACKS b/Misc/ACKS index e23f28d170b418..74ac15b89ece6b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1685,7 +1685,7 @@ Jakub Wilk Gerald S. Williams Jason Williams John Williams -Roy Willams +Roy Williams Sue Williams Carol Willing Steven Willis diff --git a/Misc/NEWS b/Misc/NEWS index 995643fa43ab22..140760015afdc6 100644 --- 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-30418: On Windows, subprocess.Popen.communicate() now also ignore EINVAL on stdin.write() if the child process is still running but closed the pipe. @@ -646,9 +649,6 @@ Library - bpo-29851: importlib.reload() now raises ModuleNotFoundError if the module lacks a spec. -- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a - bytes instance. - - Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap, improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi, Manuel Krebber, and Łukasz Langa.