]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#17341: Include name in re error message about invalid group name.
authorR David Murray <rdmurray@bitdance.com>
Sun, 14 Apr 2013 17:00:54 +0000 (13:00 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sun, 14 Apr 2013 17:00:54 +0000 (13:00 -0400)
Patch by Jason Michalski.

Lib/sre_parse.py
Lib/test/test_re.py
Misc/ACKS
Misc/NEWS

index 2ebce8947e01fa0bfbd87da7761124a9fca46881..9e0501f70a1fbf2e3820c6c980fde1d92309099e 100644 (file)
@@ -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")
index e90c770940ff4b143830c8e14d64dbefdeeb6aa9..8bc74a2dfe1b2a2e95b02e2be2afd7336fbe5311 100644 (file)
@@ -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, '<foo>'):
+            re.compile('(?P=<foo>)')
+
+    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<?foo>)')
+
 
 def run_re_tests():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
index 12c217261dc9f3a77ddab8fb4b71cc3f50bd9faa..56d140e9b5438816fd603df67355f187be2c4263 100644 (file)
--- 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
index 5ab616e7bc9033b86674c7e55ac894e2de4610d1..ade041e96558b6ce27de0261e0d60669deaf9087 100644 (file)
--- 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).