cpython: 4d5826fa77a1 (original) (raw)
Mercurial > cpython
changeset 95252:4d5826fa77a1
Issue #14260: The groupindex attribute of regular expression pattern object now is non-modifiable mapping. [#14260]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Mon, 30 Mar 2015 01:01:48 +0300 |
parents | 09f22b5d6cea |
children | bed806c9eb4c ed40da1bcdad |
files | Lib/csv.py Lib/sre_parse.py Lib/test/test_re.py Misc/NEWS Modules/_sre.c |
diffstat | 5 files changed, 31 insertions(+), 5 deletions(-)[+] [-] Lib/csv.py 7 Lib/sre_parse.py 3 Lib/test/test_re.py 8 Misc/NEWS 3 Modules/_sre.c 15 |
line wrap: on
line diff
--- a/Lib/csv.py +++ b/Lib/csv.py @@ -231,20 +231,21 @@ class Sniffer: quotes = {} delims = {} spaces = 0
groupindex = regexp.groupindex[](#l1.7) for m in matches:[](#l1.8)
n = regexp.groupindex['quote'] - 1[](#l1.9)
n = groupindex['quote'] - 1[](#l1.10) key = m[n][](#l1.11) if key:[](#l1.12) quotes[key] = quotes.get(key, 0) + 1[](#l1.13) try:[](#l1.14)
n = regexp.groupindex['delim'] - 1[](#l1.15)
n = groupindex['delim'] - 1[](#l1.16) key = m[n][](#l1.17) except KeyError:[](#l1.18) continue[](#l1.19) if key and (delimiters is None or key in delimiters):[](#l1.20) delims[key] = delims.get(key, 0) + 1[](#l1.21) try:[](#l1.22)
n = regexp.groupindex['space'] - 1[](#l1.23)
n = groupindex['space'] - 1[](#l1.24) except KeyError:[](#l1.25) continue[](#l1.26) if m[n]:[](#l1.27)
--- 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)
@@ -869,7 +870,7 @@ def parse_template(source, pattern): name = s.getuntil(">") if name.isidentifier(): try:
index = pattern.groupindex[name][](#l2.15)
index = groupindex[name][](#l2.16) except KeyError:[](#l2.17) raise IndexError("unknown group name %r" % name)[](#l2.18) else:[](#l2.19)
--- 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.[](#l3.7)
p = re.compile(r'(?i)(?P<first>a)(?P<other>b)')[](#l3.8)
self.assertEqual(sorted(p.groupindex), ['first', 'other'])[](#l3.9)
self.assertEqual(p.groupindex['other'], 2)[](#l3.10)
with self.assertRaises(TypeError):[](#l3.11)
p.groupindex['other'] = 0[](#l3.12)
self.assertEqual(p.groupindex['other'], 2)[](#l3.13)
+ def test_special_escapes(self): self.assertEqual(re.search(r"\b(b.)\b", "abcd abc bcd bx").group(1), "bx")
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -30,6 +30,9 @@ Core and Builtins Library ------- +- Issue #14260: The groupindex attribute of regular expression pattern object
- Issue #23792: Ignore KeyboardInterrupt when the pydoc pager is active. This mimics the behavior of the standard unix pagers, and prevents pipepager from shutting down while the pager itself is still running.
--- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1384,12 +1384,24 @@ static PyMethodDef pattern_methods[] = { {NULL, NULL} }; +/* PatternObject's 'groupindex' method. */ +static PyObject * +pattern_groupindex(PatternObject *self) +{
+} + +static PyGetSetDef pattern_getset[] = {
- {"groupindex", (getter)pattern_groupindex, (setter)NULL,
"A dictionary mapping group names to group numbers."},[](#l5.16)
- {NULL} /* Sentinel */
+}; + #define PAT_OFF(x) offsetof(PatternObject, x) static PyMemberDef pattern_members[] = { {"pattern", T_OBJECT, PAT_OFF(pattern), READONLY}, {"flags", T_INT, PAT_OFF(flags), READONLY}, {"groups", T_PYSSIZET, PAT_OFF(groups), READONLY},