From bc4ece5775acfd56e09ee074a68f1fd379a1d238 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 30 Sep 2014 22:04:28 -0400 Subject: [PATCH] allow longs as indexes to group() (closes #22530) --- Lib/test/test_re.py | 4 ++++ Misc/NEWS | 3 +++ Modules/_sre.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 7bdf3534ae..3c77948632 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -971,6 +971,10 @@ subpattern None pat.split(string='abracadabra', maxsplit=1), ['', 'ab', 'racadabra']) + def test_match_group_takes_long(self): + self.assertEqual(re.match("(foo)", "foo").group(1L), "foo") + self.assertRaises(IndexError, re.match("", "").group, sys.maxint + 1) + def run_re_tests(): from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR diff --git a/Misc/NEWS b/Misc/NEWS index 07e8855882..2681b9efaf 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -31,6 +31,9 @@ Core and Builtins Library ------- +- Issue #22530: Allow the ``group()`` method of regular expression match objects + to take a ``long`` as an index. + - Issue #22517: When a io.BufferedRWPair object is deallocated, clear its weakrefs. diff --git a/Modules/_sre.c b/Modules/_sre.c index d88c13f3fe..ec98182b23 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -3301,7 +3301,7 @@ match_getindex(MatchObject* self, PyObject* index) { Py_ssize_t i; - if (PyInt_Check(index)) + if (PyInt_Check(index) || PyLong_Check(index)) return PyInt_AsSsize_t(index); i = -1; -- 2.50.1