diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-11-03 18:34:16 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-11-03 18:34:16 (GMT) |
commit | 4bf474a8033af442fe666a87733d137f0a8af722 (patch) | |
tree | b8acf936834d153c752e1c1040a184fd35db88f2 | |
parent | d2685260c4411c8f3e49c69f7613fe7c9fe5f008 (diff) | |
parent | 68600aff3a841c1d901758c0e4e3fb3aafc7354e (diff) | |
download | cpython-4bf474a8033af442fe666a87733d137f0a8af722.zip cpython-4bf474a8033af442fe666a87733d137f0a8af722.tar.gz cpython-4bf474a8033af442fe666a87733d137f0a8af722.tar.bz2 |
#12759: merge with 3.3.
-rw-r--r-- | Lib/sre_parse.py | 8 | ||||
-rw-r--r-- | Lib/test/test_re.py | 20 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 30 insertions, 1 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index d358646..6411475 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -580,6 +580,8 @@ def _parse(source, state): break name = name + char group = 1 + if not name: + raise error("missing group name") if not isname(name): raise error("bad character in group name") elif sourcematch("="): @@ -592,6 +594,8 @@ def _parse(source, state): if char == ")": break name = name + char + if not name: + raise error("missing group name") if not isname(name): raise error("bad character in group name") gid = state.groupdict.get(name) @@ -644,6 +648,8 @@ def _parse(source, state): break condname = condname + char group = 2 + if not condname: + raise error("missing group name") if isname(condname): condgroup = state.groupdict.get(condname) if condgroup is None: @@ -775,7 +781,7 @@ def parse_template(source, pattern): break name = name + char if not name: - raise error("bad group name") + raise error("missing group name") try: index = int(name) if index < 0: diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 9b0aa75..e7acbe1 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -161,11 +161,31 @@ class ReTests(unittest.TestCase): self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-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): 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<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(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx') self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx') @@ -91,6 +91,9 @@ Core and Builtins 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 no newline is found. Patch by Ned Batchelder. |