]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39394: Improve warning message in the re module (GH-31988)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 19 Mar 2022 12:13:31 +0000 (14:13 +0200)
committerGitHub <noreply@github.com>
Sat, 19 Mar 2022 12:13:31 +0000 (14:13 +0200)
A warning about inline flags not at the start of the regular
expression now contains the position of the flag.

Lib/sre_parse.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2022-03-19-13-38-29.bpo-39394.7j6WL6.rst [new file with mode: 0644]

index 83119168e6376ee83bb41f7b75d2c9a0cd3058d7..53706676e9f7b8ae2dff7ebce28e226d328fd37b 100644 (file)
@@ -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
                             )
index 59575962eb4f3d42bd71c418f62db556e091ab05..48dcb16f8fa573337c9958c06c8a23c3a8f66e0a 100644 (file)
@@ -1444,7 +1444,8 @@ 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 %r' % p
+            'Flags not at the start of the expression %r'
+            ' but at position 1' % p
         )
         self.assertEqual(warns.warnings[0].filename, __file__)
 
@@ -1453,7 +1454,8 @@ 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 %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__)
 
@@ -1465,7 +1467,8 @@ class ReTests(unittest.TestCase):
                 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 (file)
index 0000000..9285179
--- /dev/null
@@ -0,0 +1,2 @@
+A warning about inline flags not at the start of the regular expression now
+contains the position of the flag.