Skip to content

Commit

Permalink
Issue #22493: Warning message emitted by using inline flags in the mi…
Browse files Browse the repository at this point in the history
…ddle of

regular expression now contains a (truncated) regex pattern.
Patch by Tim Graham.
  • Loading branch information
serhiy-storchaka committed Sep 16, 2016
2 parents 4712dc8 + abf275a commit 48ab735
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Lib/sre_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,13 @@ def _parse(source, state, verbose):
if flags is None: # global flags
if pos != 3: # "(?x"
import warnings
warnings.warn('Flags not at the start of the expression',
DeprecationWarning, stacklevel=7)
warnings.warn(
'Flags not at the start of the expression %s%s' % (
source.string[:20], # truncate long regexes
' (truncated)' if len(source.string) > 20 else '',
),
DeprecationWarning, stacklevel=7
)
continue
add_flags, del_flags = flags
group = None
Expand Down
17 changes: 15 additions & 2 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,21 @@ def test_inline_flags(self):
self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char))
self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char))

with self.assertWarns(DeprecationWarning):
self.assertTrue(re.match(upper_char + '(?i)', lower_char))
p = upper_char + '(?i)'
with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.match(p, lower_char))
self.assertEqual(
str(warns.warnings[0].message),
'Flags not at the start of the expression %s' % p
)

p = upper_char + '(?i)%s' % ('.?' * 100)
with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.match(p, lower_char))
self.assertEqual(
str(warns.warnings[0].message),
'Flags not at the start of the expression %s (truncated)' % p[:20]
)

def test_dollar_matches_twice(self):
"$ matches the end of string, and just before the terminating \n"
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Core and Builtins
Library
-------

- 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.

- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
an empty bytestring is passed.

Expand Down

0 comments on commit 48ab735

Please sign in to comment.