Kaydet (Commit) 26dfaac9 authored tarafından R David Murray's avatar R David Murray

#17341: Include name in re error message about invalid group name.

Patch by Jason Michalski.
üst f2fa5fc7
...@@ -600,7 +600,7 @@ def _parse(source, state): ...@@ -600,7 +600,7 @@ def _parse(source, state):
if not name: if not name:
raise error("missing group name") raise error("missing group name")
if not name.isidentifier(): if not name.isidentifier():
raise error("bad character in group name") raise error("bad character in group name %r" % name)
elif sourcematch("="): elif sourcematch("="):
# named backreference # named backreference
name = "" name = ""
...@@ -614,7 +614,8 @@ def _parse(source, state): ...@@ -614,7 +614,8 @@ def _parse(source, state):
if not name: if not name:
raise error("missing group name") raise error("missing group name")
if not name.isidentifier(): 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) gid = state.groupdict.get(name)
if gid is None: if gid is None:
raise error("unknown group name") raise error("unknown group name")
......
...@@ -3,6 +3,7 @@ from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \ ...@@ -3,6 +3,7 @@ from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
import io import io
import re import re
from re import Scanner from re import Scanner
import sre_constants
import sys import sys
import string import string
import traceback import traceback
...@@ -1029,6 +1030,16 @@ class ReTests(unittest.TestCase): ...@@ -1029,6 +1030,16 @@ class ReTests(unittest.TestCase):
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT) self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
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(): def run_re_tests():
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
......
...@@ -805,6 +805,7 @@ Piotr Meyer ...@@ -805,6 +805,7 @@ Piotr Meyer
Alexis Métaireau Alexis Métaireau
Steven Miale Steven Miale
Trent Mick Trent Mick
Jason Michalski
Franck Michea Franck Michea
Tom Middleton Tom Middleton
Stan Mihai Stan Mihai
......
...@@ -29,6 +29,9 @@ Core and Builtins ...@@ -29,6 +29,9 @@ Core and Builtins
Library 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 - Issue #17702: os.environ now raises KeyError with the original environment
variable name (str on UNIX), instead of using the encoded name (bytes on variable name (str on UNIX), instead of using the encoded name (bytes on
UNIX). UNIX).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment