[Python-checkins] bpo-39394: Improve warning message in the re module (GH-31988)
miss-islington
webhook-mailer at python.org
Sat Mar 19 10:10:03 EDT 2022
https://github.com/python/cpython/commit/cbcd2e36d6cbb1d8b6a2b30a2cf1484b7857e7d6
commit: cbcd2e36d6cbb1d8b6a2b30a2cf1484b7857e7d6
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-03-19T07:09:59-07:00
summary:
bpo-39394: Improve warning message in the re module (GH-31988)
A warning about inline flags not at the start of the regular
expression now contains the position of the flag.
(cherry picked from commit 4142961b9f5ad3bf93976a6a7162f8049e354018)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Library/2022-03-19-13-38-29.bpo-39394.7j6WL6.rst
M Lib/sre_parse.py
M Lib/test/test_re.py
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
index 83119168e6376..53706676e9f7b 100644
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -807,9 +807,11 @@ 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 %r%s' % (
+ 'Flags not at the start of the expression %r%s'
+ ' but at position %d' % (
source.string[:20], # truncate long regexes
' (truncated)' if len(source.string) > 20 else '',
+ start,
),
DeprecationWarning, stacklevel=nested + 6
)
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 1bfbcb853c4ed..48a609b5a0031 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1443,7 +1443,8 @@ 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 %r' % p
+ 'Flags not at the start of the expression %r'
+ ' but at position 1' % p
)
self.assertEqual(warns.warnings[0].filename, __file__)
@@ -1452,7 +1453,8 @@ 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 %r (truncated)' % p[:20]
+ 'Flags not at the start of the expression %r (truncated)'
+ ' but at position 1' % p[:20]
)
self.assertEqual(warns.warnings[0].filename, __file__)
@@ -1464,7 +1466,8 @@ def test_inline_flags(self):
self.assertTrue(re.match(p, b'a'))
self.assertEqual(
str(warns.warnings[0].message),
- 'Flags not at the start of the expression %r' % p
+ 'Flags not at the start of the expression %r'
+ ' but at position 1' % p
)
self.assertEqual(warns.warnings[0].filename, __file__)
diff --git a/Misc/NEWS.d/next/Library/2022-03-19-13-38-29.bpo-39394.7j6WL6.rst b/Misc/NEWS.d/next/Library/2022-03-19-13-38-29.bpo-39394.7j6WL6.rst
new file mode 100644
index 0000000000000..9285179c9fdca
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-19-13-38-29.bpo-39394.7j6WL6.rst
@@ -0,0 +1,2 @@
+A warning about inline flags not at the start of the regular expression now
+contains the position of the flag.
More information about the Python-checkins
mailing list