10000 Issue #22493: Warning message emitted by using inline flags in the mi… · python/cpython@48ab735 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48ab735

Browse files
Issue #22493: Warning message emitted by using inline flags in the middle of
regular expression now contains a (truncated) regex pattern. Patch by Tim Graham.
2 parents 4712dc8 + abf275a commit 48ab735

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Lib/sre_parse.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,13 @@ def _parse(source, state, verbose):
735735
if flags is None: # global flags
736736
if pos != 3: # "(?x"
737737
import warnings
738-
warnings.warn('Flags not at the start of the expression',
739-
DeprecationWarning, stacklevel=7)
738+
warnings.warn(
739+
'Flags not at the start of the expression %s%s' % (
740+
source.string[:20], # truncate long regexes
741+
' (truncated)' if len(source.string) > 20 else '',
742+
),
743+
DeprecationWarning, stacklevel=7
744+
)
740745
continue
741746
add_flags, del_flags = flags
742747
group = None

Lib/test/test_re.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,8 +1323,21 @@ def test_inline_flags(self):
13231323
self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char))
13241324
self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char))
13251325

1326-
with self.assertWarns(DeprecationWarning):
1327-
self.assertTrue(re.match(upper_char + '(?i)', lower_char))
1326+
p = upper_char + '(?i)'
1327+
with self.assertWarns(DeprecationWarning) as warns:
1328+
self.assertTrue(re.match(p, lower_char))
1329+
self.assertEqual(
1330+
str(warns.warnings[0].message),
1331+
'Flags not at the start of the expression %s' % p
1332+
)
1333+
1334+
p = upper_char + '(?i)%s' % ('.?' * 100)
1335+
with self.assertWarns(DeprecationWarning) as warns:
1336+
self.assertTrue(re.match(p, lower_char))
1337+
self.assertEqual(
1338+
str(warns.warnings[0].message),
1339+
'Flags not at the start of the expression %s (truncated)' % p[:20]
1340+
)
13281341

13291342
def test_dollar_matches_twice(self):
13301343
"$ matches the end of string, and just before the terminating \n"

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #22493: Warning message emitted by using inline flags in the middle of
31+
regular expression now contains a (truncated) regex pattern.
32+
Patch by Tim Graham.
33+
3034
- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
3135
an empty bytestring is passed.
3236

0 commit comments

Comments
 (0)
0