mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
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 4142961b9f
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
c1f327f30d
commit
906f1a4a95
3 changed files with 11 additions and 4 deletions
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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__)
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
A warning about inline flags not at the start of the regular expression now
|
||||
contains the position of the flag.
|
Loading…
Add table
Add a link
Reference in a new issue