From 26dfaac9ac0cf9a34006ab52a0e0cc4e39b772fa Mon Sep 17 00:00:00 2001 From: R David Murray Date: Sun, 14 Apr 2013 13:00:54 -0400 Subject: #17341: Include name in re error message about invalid group name. Patch by Jason Michalski. --- Lib/sre_parse.py | 5 +++-- Lib/test/test_re.py | 11 +++++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 2ebce89..9e0501f 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -600,7 +600,7 @@ def _parse(source, state): if not name: raise error("missing group name") if not name.isidentifier(): - raise error("bad character in group name") + raise error("bad character in group name %r" % name) elif sourcematch("="): # named backreference name = "" @@ -614,7 +614,8 @@ def _parse(source, state): if not name: raise error("missing group name") if not name.isidentifier(): - raise error("bad character in group name") + raise error("bad character in backref group name " + "%r" % name) gid = state.groupdict.get(name) if gid is None: raise error("unknown group name") diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index e90c770..8bc74a2 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -3,6 +3,7 @@ from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \ import io import re from re import Scanner +import sre_constants import sys import string import traceback @@ -1029,6 +1030,16 @@ class ReTests(unittest.TestCase): self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT) self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT) + def test_backref_group_name_in_exception(self): + # Issue 17341: Poor error message when compiling invalid regex + with self.assertRaisesRegex(sre_constants.error, ''): + re.compile('(?P=)') + + def test_group_name_in_exception(self): + # Issue 17341: Poor error message when compiling invalid regex + with self.assertRaisesRegex(sre_constants.error, '\?foo'): + re.compile('(?P)') + def run_re_tests(): from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR diff --git a/Misc/ACKS b/Misc/ACKS index 12c2172..56d140e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -805,6 +805,7 @@ Piotr Meyer Alexis Métaireau Steven Miale Trent Mick +Jason Michalski Franck Michea Tom Middleton Stan Mihai diff --git a/Misc/NEWS b/Misc/NEWS index 5ab616e..ade041e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -29,6 +29,9 @@ Core and Builtins Library ------- +- Issue #17341: Include the invalid name in the error messages from re about + invalid group names. + - Issue #17702: os.environ now raises KeyError with the original environment variable name (str on UNIX), instead of using the encoded name (bytes on UNIX). -- cgit v0.12