Issue #27177: Match objects in the re module now support index-like objects
as group indices. Based on patches by Jeroen Demeyer and Xiang Zhang.
This commit is contained in:
parent
3583c3bd1d
commit
977b3ac1c1
|
@ -414,19 +414,33 @@ class ReTests(unittest.TestCase):
|
||||||
self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
|
self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
|
||||||
self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
|
self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
|
||||||
|
|
||||||
# A single group
|
|
||||||
m = re.match('(a)', 'a')
|
|
||||||
self.assertEqual(m.group(0), 'a')
|
|
||||||
self.assertEqual(m.group(0), 'a')
|
|
||||||
self.assertEqual(m.group(1), 'a')
|
|
||||||
self.assertEqual(m.group(1, 1), ('a', 'a'))
|
|
||||||
|
|
||||||
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
|
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
|
||||||
self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
|
self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
|
||||||
self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
|
self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
|
||||||
(None, 'b', None))
|
(None, 'b', None))
|
||||||
self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
|
self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
|
||||||
|
|
||||||
|
def test_group(self):
|
||||||
|
class Index:
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
def __index__(self):
|
||||||
|
return self.value
|
||||||
|
# A single group
|
||||||
|
m = re.match('(a)(b)', 'ab')
|
||||||
|
self.assertEqual(m.group(), 'ab')
|
||||||
|
self.assertEqual(m.group(0), 'ab')
|
||||||
|
self.assertEqual(m.group(1), 'a')
|
||||||
|
self.assertEqual(m.group(Index(1)), 'a')
|
||||||
|
self.assertRaises(IndexError, m.group, -1)
|
||||||
|
self.assertRaises(IndexError, m.group, 3)
|
||||||
|
self.assertRaises(IndexError, m.group, 1<<1000)
|
||||||
|
self.assertRaises(IndexError, m.group, Index(1<<1000))
|
||||||
|
self.assertRaises(IndexError, m.group, 'x')
|
||||||
|
# Multiple groups
|
||||||
|
self.assertEqual(m.group(2, 1), ('b', 'a'))
|
||||||
|
self.assertEqual(m.group(Index(2), Index(1)), ('b', 'a'))
|
||||||
|
|
||||||
def test_re_fullmatch(self):
|
def test_re_fullmatch(self):
|
||||||
# Issue 16203: Proposal: add re.fullmatch() method.
|
# Issue 16203: Proposal: add re.fullmatch() method.
|
||||||
self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))
|
self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #27177: Match objects in the re module now support index-like objects
|
||||||
|
as group indices. Based on patches by Jeroen Demeyer and Xiang Zhang.
|
||||||
|
|
||||||
- Issue #26754: Some functions (compile() etc) accepted a filename argument
|
- Issue #26754: Some functions (compile() etc) accepted a filename argument
|
||||||
encoded as an iterable of integers. Now only strings and byte-like objects
|
encoded as an iterable of integers. Now only strings and byte-like objects
|
||||||
are accepted.
|
are accepted.
|
||||||
|
|
|
@ -2049,8 +2049,9 @@ match_getindex(MatchObject* self, PyObject* index)
|
||||||
/* Default value */
|
/* Default value */
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (PyLong_Check(index))
|
if (PyIndex_Check(index)) {
|
||||||
return PyLong_AsSsize_t(index);
|
return PyNumber_AsSsize_t(index, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
i = -1;
|
i = -1;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue