[Python-checkins] cpython (2.7): #17341: Include name in re error message about invalid group name.

r.david.murray python-checkins at python.org
Sun Apr 14 19:09:54 CEST 2013


http://hg.python.org/cpython/rev/bbb3aa45b4ea
changeset:   83381:bbb3aa45b4ea
branch:      2.7
parent:      83373:dbb943399c9b
user:        R David Murray <rdmurray at bitdance.com>
date:        Sun Apr 14 13:08:50 2013 -0400
summary:
  #17341: Include name in re error message about invalid group name.

Patch by Jason Michalski.

files:
  Lib/sre_parse.py    |   6 ++++--
  Lib/test/test_re.py |  11 +++++++++++
  Misc/ACKS           |   1 +
  Misc/NEWS           |   3 +++
  4 files changed, 19 insertions(+), 2 deletions(-)


diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -549,7 +549,8 @@
                         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 @@
                         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"
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -2,6 +2,7 @@
 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 @@
         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
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -674,6 +674,7 @@
 Mike Meyer
 Piotr Meyer
 Steven Miale
+Jason Michalski
 Trent Mick
 Tom Middleton
 Stan Mihai
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list