Skip to content

Commit 4ece7d1

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-39394: Improve warning message in the re module (pythonGH-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 4142961) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 00d36db commit 4ece7d1

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Lib/sre_parse.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,11 @@ def _parse(source, state, verbose, nested, first=False):
807807
if not first or subpattern:
808808
import warnings
809809
warnings.warn(
810-
'Flags not at the start of the expression %r%s' % (
810+
'Flags not at the start of the expression %r%s'
811+
' but at position %d' % (
811812
source.string[:20], # truncate long regexes
812813
' (truncated)' if len(source.string) > 20 else '',
814+
start,
813815
),
814816
DeprecationWarning, stacklevel=nested + 6
815817
)

Lib/test/test_re.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,8 @@ def test_inline_flags(self):
14431443
self.assertTrue(re.match(p, lower_char))
14441444
self.assertEqual(
14451445
str(warns.warnings[0].message),
1446-
'Flags not at the start of the expression %r' % p
1446+
'Flags not at the start of the expression %r'
1447+
' but at position 1' % p
14471448
)
14481449
self.assertEqual(warns.warnings[0].filename, __file__)
14491450

@@ -1452,7 +1453,8 @@ def test_inline_flags(self):
14521453
self.assertTrue(re.match(p, lower_char))
14531454
self.assertEqual(
14541455
str(warns.warnings[0].message),
1455-
'Flags not at the start of the expression %r (truncated)' % p[:20]
1456+
'Flags not at the start of the expression %r (truncated)'
1457+
' but at position 1' % p[:20]
14561458
)
14571459
self.assertEqual(warns.warnings[0].filename, __file__)
14581460

@@ -1464,7 +1466,8 @@ def test_inline_flags(self):
14641466
self.assertTrue(re.match(p, b'a'))
14651467
self.assertEqual(
14661468
str(warns.warnings[0].message),
1467-
'Flags not at the start of the expression %r' % p
1469+
'Flags not at the start of the expression %r'
1470+
' but at position 1' % p
14681471
)
14691472
self.assertEqual(warns.warnings[0].filename, __file__)
14701473

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A warning about inline flags not at the start of the regular expression now
2+
contains the position of the flag.

0 commit comments

Comments
 (0)