]> 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:08:50 +0000 (13:08 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sun, 14 Apr 2013 17:08:50 +0000 (13:08 -0400)
Patch by Jason Michalski.

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

index a0cf34414b6335d2747bcc319a410bce1d18a702..75f8c9601df6ef7d6889392e2a68aef275417a70 100644 (file)
@@ -549,7 +549,8 @@ def _parse(source, state):
                         if not name:
                             raise error("missing group name")
                         if not isname(name):
-                            raise error, "bad character in group name"
+                            raise error("bad character in group name %r" %
+                                        name)
                     elif sourcematch("="):
                         # named backreference
                         name = ""
@@ -563,7 +564,8 @@ def _parse(source, state):
                         if not name:
                             raise error("missing group name")
                         if not isname(name):
-                            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 6ddddda9e1bd2eb0161a7edb287c49544cc90756..18a81a21418bec3d42f0f44c207c5ae4fe060e8f 100644 (file)
@@ -2,6 +2,7 @@ from test.test_support import verbose, run_unittest, import_module
 from test.test_support import precisionbigmemtest, _2G, cpython_only
 import re
 from re import Scanner
+import sre_constants
 import sys
 import string
 import traceback
@@ -886,6 +887,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.assertRaisesRegexp(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.assertRaisesRegexp(sre_constants.error, '\?foo'):
+            re.compile('(?P<?foo>)')
+
 
 def run_re_tests():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
index 5779332ce35867286e9545fed22a518cca4af086..78f074c76db52c680a8f01e62bfd956208fc7929 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -674,6 +674,7 @@ Carl Meyer
 Mike Meyer
 Piotr Meyer
 Steven Miale
+Jason Michalski
 Trent Mick
 Tom Middleton
 Stan Mihai
index eda841fd9928877c44647a1af68022f730beaa5f..d723cb59919a8b2c3526359adc03518c0b68a7b9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17341: Include the invalid name in the error messages from re about
+  invalid group names.
+
 - Issue #17016: Get rid of possible pointer wraparounds and integer overflows
   in the re module.  Patch by Nickolai Zeldovich.