diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-29 22:01:48 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-29 22:01:48 (GMT) |
commit | 07360df481adeacfc2b7a1a7f89ae9a76e86b725 (patch) | |
tree | 1d111118cafc0117d3cd68fdfa7397e68dd25191 /Lib | |
parent | 1813c1701f8e7e9287bccf66d1b4b8348c432168 (diff) | |
download | cpython-07360df481adeacfc2b7a1a7f89ae9a76e86b725.zip cpython-07360df481adeacfc2b7a1a7f89ae9a76e86b725.tar.gz cpython-07360df481adeacfc2b7a1a7f89ae9a76e86b725.tar.bz2 |
Issue #14260: The groupindex attribute of regular expression pattern object
now is non-modifiable mapping.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/csv.py | 7 | ||||
-rw-r--r-- | Lib/sre_parse.py | 3 | ||||
-rw-r--r-- | Lib/test/test_re.py | 8 |
3 files changed, 14 insertions, 4 deletions
@@ -231,20 +231,21 @@ class Sniffer: quotes = {} delims = {} spaces = 0 + groupindex = regexp.groupindex for m in matches: - n = regexp.groupindex['quote'] - 1 + n = groupindex['quote'] - 1 key = m[n] if key: quotes[key] = quotes.get(key, 0) + 1 try: - n = regexp.groupindex['delim'] - 1 + n = groupindex['delim'] - 1 key = m[n] except KeyError: continue if key and (delimiters is None or key in delimiters): delims[key] = delims.get(key, 0) + 1 try: - n = regexp.groupindex['space'] - 1 + n = groupindex['space'] - 1 except KeyError: continue if m[n]: diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 0bce71e..c0f539d 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -855,6 +855,7 @@ def parse_template(source, pattern): del literal[:] groups.append((len(literals), index)) literals.append(None) + groupindex = pattern.groupindex while True: this = sget() if this is None: @@ -869,7 +870,7 @@ def parse_template(source, pattern): name = s.getuntil(">") if name.isidentifier(): try: - index = pattern.groupindex[name] + index = groupindex[name] except KeyError: raise IndexError("unknown group name %r" % name) else: diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 020656a..5b71612 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -577,6 +577,14 @@ class ReTests(unittest.TestCase): self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) self.assertTrue(re.match("(a)", "a").re) + # Issue 14260. groupindex should be non-modifiable mapping. + p = re.compile(r'(?i)(?P<first>a)(?P<other>b)') + self.assertEqual(sorted(p.groupindex), ['first', 'other']) + self.assertEqual(p.groupindex['other'], 2) + with self.assertRaises(TypeError): + p.groupindex['other'] = 0 + self.assertEqual(p.groupindex['other'], 2) + def test_special_escapes(self): self.assertEqual(re.search(r"\b(b.)\b", "abcd abc bcd bx").group(1), "bx") |