Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-39394: Improve warning message in the re module #31988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/sre_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
9 changes: 6 additions & 3 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,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__)

Expand All @@ -1453,7 +1454,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__)

Expand All @@ -1465,7 +1467,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__)

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