| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -231,20 +231,21 @@ def _guess_quote_and_delimiter(self, data, delimiters): | |||
| 231 | 231 | quotes = {} | |
| 232 | 232 | delims = {} | |
| 233 | 233 | spaces = 0 | |
| 234 | + groupindex = regexp.groupindex | ||
| 234 | 235 | for m in matches: | |
| 235 | - n = regexp.groupindex['quote'] - 1 | ||
| 236 | + n = groupindex['quote'] - 1 | ||
| 236 | 237 | key = m[n] | |
| 237 | 238 | if key: | |
| 238 | 239 | quotes[key] = quotes.get(key, 0) + 1 | |
| 239 | 240 | try: | |
| 240 | - n = regexp.groupindex['delim'] - 1 | ||
| 241 | + n = groupindex['delim'] - 1 | ||
| 241 | 242 | key = m[n] | |
| 242 | 243 | except KeyError: | |
| 243 | 244 | continue | |
| 244 | 245 | if key and (delimiters is None or key in delimiters): | |
| 245 | 246 | delims[key] = delims.get(key, 0) + 1 | |
| 246 | 247 | try: | |
| 247 | - n = regexp.groupindex['space'] - 1 | ||
| 248 | + n = groupindex['space'] - 1 | ||
| 248 | 249 | except KeyError: | |
| 249 | 250 | continue | |
| 250 | 251 | if m[n]: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -855,6 +855,7 @@ def addgroup(index): | |||
| 855 | 855 | del literal[:] | |
| 856 | 856 | groups.append((len(literals), index)) | |
| 857 | 857 | literals.append(None) | |
| 858 | + groupindex = pattern.groupindex | ||
| 858 | 859 | while True: | |
| 859 | 860 | this = sget() | |
| 860 | 861 | if this is None: | |
@@ -869,7 +870,7 @@ def addgroup(index): | |||
| 869 | 870 | name = s.getuntil(">") | |
| 870 | 871 | if name.isidentifier(): | |
| 871 | 872 | try: | |
| 872 | - index = pattern.groupindex[name] | ||
| 873 | + index = groupindex[name] | ||
| 873 | 874 | except KeyError: | |
| 874 | 875 | raise IndexError("unknown group name %r" % name) | |
| 875 | 876 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -577,6 +577,14 @@ def test_getattr(self): | |||
| 577 | 577 | self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) | |
| 578 | 578 | self.assertTrue(re.match("(a)", "a").re) | |
| 579 | 579 | ||
| 580 | + # Issue 14260. groupindex should be non-modifiable mapping. | ||
| 581 | + p = re.compile(r'(?i)(?P<first>a)(?P<other>b)') | ||
| 582 | + self.assertEqual(sorted(p.groupindex), ['first', 'other']) | ||
| 583 | + self.assertEqual(p.groupindex['other'], 2) | ||
| 584 | + with self.assertRaises(TypeError): | ||
| 585 | + p.groupindex['other'] = 0 | ||
| 586 | + self.assertEqual(p.groupindex['other'], 2) | ||
| 587 | + | ||
| 580 | 588 | def test_special_escapes(self): | |
| 581 | 589 | self.assertEqual(re.search(r"\b(b.)\b", | |
| 582 | 590 | "abcd abc bcd bx").group(1), "bx") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,9 @@ Core and Builtins | |||
| 30 | 30 | Library | |
| 31 | 31 | ------- | |
| 32 | 32 | ||
| 33 | + - Issue #14260: The groupindex attribute of regular expression pattern object | ||
| 34 | + now is non-modifiable mapping. | ||
| 35 | + | ||
| 33 | 36 | - Issue #23792: Ignore KeyboardInterrupt when the pydoc pager is active. | |
| 34 | 37 | This mimics the behavior of the standard unix pagers, and prevents | |
| 35 | 38 | pipepager from shutting down while the pager itself is still running. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1384,12 +1384,24 @@ static PyMethodDef pattern_methods[] = { | |||
| 1384 | 1384 | {NULL, NULL} | |
| 1385 | 1385 | }; | |
| 1386 | 1386 | ||
| 1387 | + /* PatternObject's 'groupindex' method. */ | ||
| 1388 | + static PyObject * | ||
| 1389 | + pattern_groupindex(PatternObject *self) | ||
| 1390 | + { | ||
| 1391 | + return PyDictProxy_New(self->groupindex); | ||
| 1392 | + } | ||
| 1393 | + | ||
| 1394 | + static PyGetSetDef pattern_getset[] = { | ||
| 1395 | + {"groupindex", (getter)pattern_groupindex, (setter)NULL, | ||
| 1396 | + "A dictionary mapping group names to group numbers."}, | ||
| 1397 | + {NULL} /* Sentinel */ | ||
| 1398 | + }; | ||
| 1399 | + | ||
| 1387 | 1400 | #define PAT_OFF(x) offsetof(PatternObject, x) | |
| 1388 | 1401 | static PyMemberDef pattern_members[] = { | |
| 1389 | 1402 | {"pattern", T_OBJECT, PAT_OFF(pattern), READONLY}, | |
| 1390 | 1403 | {"flags", T_INT, PAT_OFF(flags), READONLY}, | |
| 1391 | 1404 | {"groups", T_PYSSIZET, PAT_OFF(groups), READONLY}, | |
| 1392 | - {"groupindex", T_OBJECT, PAT_OFF(groupindex), READONLY}, | ||
| 1393 | 1405 | {NULL} /* Sentinel */ | |
| 1394 | 1406 | }; | |
| 1395 | 1407 | ||
@@ -1422,6 +1434,7 @@ static PyTypeObject Pattern_Type = { | |||
| 1422 | 1434 | 0, /* tp_iternext */ | |
| 1423 | 1435 | pattern_methods, /* tp_methods */ | |
| 1424 | 1436 | pattern_members, /* tp_members */ | |
| 1437 | + pattern_getset, /* tp_getset */ | ||
| 1425 | 1438 | }; | |
| 1426 | 1439 | ||
| 1427 | 1440 | static int _validate(PatternObject *self); /* Forward */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments