X Tutup
Skip to content

Commit 07360df

Browse files
Issue python#14260: The groupindex attribute of regular expression pattern object
now is non-modifiable mapping.
1 parent 1813c17 commit 07360df

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

Lib/csv.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,21 @@ def _guess_quote_and_delimiter(self, data, delimiters):
231231
quotes = {}
232232
delims = {}
233233
spaces = 0
234+
groupindex = regexp.groupindex
234235
for m in matches:
235-
n = regexp.groupindex['quote'] - 1
236+
n = groupindex['quote'] - 1
236237
key = m[n]
237238
if key:
238239
quotes[key] = quotes.get(key, 0) + 1
239240
try:
240-
n = regexp.groupindex['delim'] - 1
241+
n = groupindex['delim'] - 1
241242
key = m[n]
242243
except KeyError:
243244
continue
244245
if key and (delimiters is None or key in delimiters):
245246
delims[key] = delims.get(key, 0) + 1
246247
try:
247-
n = regexp.groupindex['space'] - 1
248+
n = groupindex['space'] - 1
248249
except KeyError:
249250
continue
250251
if m[n]:

Lib/sre_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ def addgroup(index):
855855
del literal[:]
856856
groups.append((len(literals), index))
857857
literals.append(None)
858+
groupindex = pattern.groupindex
858859
while True:
859860
this = sget()
860861
if this is None:
@@ -869,7 +870,7 @@ def addgroup(index):
869870
name = s.getuntil(">")
870871
if name.isidentifier():
871872
try:
872-
index = pattern.groupindex[name]
873+
index = groupindex[name]
873874
except KeyError:
874875
raise IndexError("unknown group name %r" % name)
875876
else:

Lib/test/test_re.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ def test_getattr(self):
577577
self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
578578
self.assertTrue(re.match("(a)", "a").re)
579579

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+
580588
def test_special_escapes(self):
581589
self.assertEqual(re.search(r"\b(b.)\b",
582590
"abcd abc bcd bx").group(1), "bx")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- Issue #14260: The groupindex attribute of regular expression pattern object
34+
now is non-modifiable mapping.
35+
3336
- Issue #23792: Ignore KeyboardInterrupt when the pydoc pager is active.
3437
This mimics the behavior of the standard unix pagers, and prevents
3538
pipepager from shutting down while the pager itself is still running.

Modules/_sre.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,12 +1384,24 @@ static PyMethodDef pattern_methods[] = {
13841384
{NULL, NULL}
13851385
};
13861386

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+
13871400
#define PAT_OFF(x) offsetof(PatternObject, x)
13881401
static PyMemberDef pattern_members[] = {
13891402
{"pattern", T_OBJECT, PAT_OFF(pattern), READONLY},
13901403
{"flags", T_INT, PAT_OFF(flags), READONLY},
13911404
{"groups", T_PYSSIZET, PAT_OFF(groups), READONLY},
1392-
{"groupindex", T_OBJECT, PAT_OFF(groupindex), READONLY},
13931405
{NULL} /* Sentinel */
13941406
};
13951407

@@ -1422,6 +1434,7 @@ static PyTypeObject Pattern_Type = {
14221434
0, /* tp_iternext */
14231435
pattern_methods, /* tp_methods */
14241436
pattern_members, /* tp_members */
1437+
pattern_getset, /* tp_getset */
14251438
};
14261439

14271440
static int _validate(PatternObject *self); /* Forward */

0 commit comments

Comments
 (0)
X Tutup