#12759: sre_parse now raises a proper error when the name of the group is missing. Initial patch by Serhiy Storchaka.

This commit is contained in:
Ezio Melotti 2012-11-03 20:33:08 +02:00
parent dedfa9bfae
commit 0941d9fc64
3 changed files with 30 additions and 1 deletions

View File

@ -548,6 +548,8 @@ def _parse(source, state):
break break
name = name + char name = name + char
group = 1 group = 1
if not name:
raise error("missing group name")
if not isname(name): if not isname(name):
raise error("bad character in group name") raise error("bad character in group name")
elif sourcematch("="): elif sourcematch("="):
@ -560,6 +562,8 @@ def _parse(source, state):
if char == ")": if char == ")":
break break
name = name + char name = name + char
if not name:
raise error("missing group name")
if not isname(name): if not isname(name):
raise error("bad character in group name") raise error("bad character in group name")
gid = state.groupdict.get(name) gid = state.groupdict.get(name)
@ -612,6 +616,8 @@ def _parse(source, state):
break break
condname = condname + char condname = condname + char
group = 2 group = 2
if not condname:
raise error("missing group name")
if isname(condname): if isname(condname):
condgroup = state.groupdict.get(condname) condgroup = state.groupdict.get(condname)
if condgroup is None: if condgroup is None:
@ -743,7 +749,7 @@ def parse_template(source, pattern):
break break
name = name + char name = name + char
if not name: if not name:
raise error("bad group name") raise error("missing group name")
try: try:
index = int(name) index = int(name)
if index < 0: if index < 0:

View File

@ -161,11 +161,31 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-') self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d') self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
def test_symbolic_groups(self):
re.compile('(?P<a>x)(?P=a)(?(a)y)')
re.compile('(?P<a1>x)(?P=a1)(?(a1)y)')
self.assertRaises(re.error, re.compile, '(?P<a>)(?P<a>)')
self.assertRaises(re.error, re.compile, '(?Px)')
self.assertRaises(re.error, re.compile, '(?P=)')
self.assertRaises(re.error, re.compile, '(?P=1)')
self.assertRaises(re.error, re.compile, '(?P=a)')
self.assertRaises(re.error, re.compile, '(?P=a1)')
self.assertRaises(re.error, re.compile, '(?P=a.)')
self.assertRaises(re.error, re.compile, '(?P<)')
self.assertRaises(re.error, re.compile, '(?P<>)')
self.assertRaises(re.error, re.compile, '(?P<1>)')
self.assertRaises(re.error, re.compile, '(?P<a.>)')
self.assertRaises(re.error, re.compile, '(?())')
self.assertRaises(re.error, re.compile, '(?(a))')
self.assertRaises(re.error, re.compile, '(?(1a))')
self.assertRaises(re.error, re.compile, '(?(a.))')
def test_symbolic_refs(self): def test_symbolic_refs(self):
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<>', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx') self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')

View File

@ -143,6 +143,9 @@ Core and Builtins
Library Library
------- -------
- Issue #12759: sre_parse now raises a proper error when the name of the group
is missing. Initial patch by Serhiy Storchaka.
- Issue #16152: fix tokenize to ignore whitespace at the end of the code when - Issue #16152: fix tokenize to ignore whitespace at the end of the code when
no newline is found. Patch by Ned Batchelder. no newline is found. Patch by Ned Batchelder.