summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-09-16 22:30:48 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-09-16 22:30:48 (GMT)
commit48ab735ca518b6cb3aef6ea1662182aba3f89253 (patch)
treeac84d30b122552692fd22795d388fb818103cfe2
parent4712dc879a543344406a2982e2e70d1756b9263b (diff)
parentabf275af5804c5f76fbe10c5cb1dd3d2e4b04c5b (diff)
downloadcpython-48ab735ca518b6cb3aef6ea1662182aba3f89253.zip
cpython-48ab735ca518b6cb3aef6ea1662182aba3f89253.tar.gz
cpython-48ab735ca518b6cb3aef6ea1662182aba3f89253.tar.bz2
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.
-rw-r--r--Lib/sre_parse.py9
-rw-r--r--Lib/test/test_re.py17
-rw-r--r--Misc/NEWS4
3 files changed, 26 insertions, 4 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
index 4a77f0c..3d38673 100644
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -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
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index eb1aba3..6803e02 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1323,8 +1323,21 @@ class ReTests(unittest.TestCase):
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"
diff --git a/Misc/NEWS b/Misc/NEWS
index 1a9f30f..9a9bcee 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.