Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a2b267

Browse files
committed
Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
Patch by Serhiy Storchaka.
2 parents 17485bf + 43fb54c commit 9a2b267

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

Lib/test/test_re.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from test.support import verbose, run_unittest, gc_collect
1+
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G
22
import io
33
import re
44
from re import Scanner
@@ -949,6 +949,21 @@ def test_compile(self):
949949
# Test behaviour when not given a string or pattern as parameter
950950
self.assertRaises(TypeError, re.compile, 0)
951951

952+
# The huge memuse is because of re.sub() using a list and a join()
953+
# to create the replacement result.
954+
@bigmemtest(size=_2G, memuse=20)
955+
def test_large(self, size):
956+
# Issue #10182: indices were 32-bit-truncated.
957+
s = 'a' * size
958+
m = re.search('$', s)
959+
self.assertIsNotNone(m)
960+
self.assertEqual(m.start(), size)
961+
self.assertEqual(m.end(), size)
962+
r, n = re.subn('', '', s)
963+
self.assertEqual(r, s)
964+
self.assertEqual(n, size + 1)
965+
966+
952967
def run_re_tests():
953968
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
954969
if verbose:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Core and Builtins
9898
Library
9999
-------
100100

101+
- Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
102+
Patch by Serhiy Storchaka.
103+
101104
- Issue #16573: In 2to3, treat enumerate() like a consuming call, so superfluous
102105
list() calls aren't added to filter(), map(), and zip() which are directly
103106
passed enumerate().

Modules/_sre.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ sre_literal_template(int charsize, char* ptr, Py_ssize_t len)
16181618
static PyObject *
16191619
sre_codesize(PyObject* self, PyObject *unused)
16201620
{
1621-
return Py_BuildValue("l", sizeof(SRE_CODE));
1621+
return PyLong_FromSize_t(sizeof(SRE_CODE));
16221622
}
16231623

16241624
static PyObject *
@@ -2435,7 +2435,7 @@ pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string,
24352435
return NULL;
24362436

24372437
if (subn)
2438-
return Py_BuildValue("Ni", item, n);
2438+
return Py_BuildValue("Nn", item, n);
24392439

24402440
return item;
24412441

@@ -3387,7 +3387,7 @@ match_start(MatchObject* self, PyObject* args)
33873387
}
33883388

33893389
/* mark is -1 if group is undefined */
3390-
return Py_BuildValue("i", self->mark[index*2]);
3390+
return PyLong_FromSsize_t(self->mark[index*2]);
33913391
}
33923392

33933393
static PyObject*
@@ -3410,7 +3410,7 @@ match_end(MatchObject* self, PyObject* args)
34103410
}
34113411

34123412
/* mark is -1 if group is undefined */
3413-
return Py_BuildValue("i", self->mark[index*2+1]);
3413+
return PyLong_FromSsize_t(self->mark[index*2+1]);
34143414
}
34153415

34163416
LOCAL(PyObject*)
@@ -3560,7 +3560,7 @@ static PyObject *
35603560
match_lastindex_get(MatchObject *self)
35613561
{
35623562
if (self->lastindex >= 0)
3563-
return Py_BuildValue("i", self->lastindex);
3563+
return PyLong_FromSsize_t(self->lastindex);
35643564
Py_INCREF(Py_None);
35653565
return Py_None;
35663566
}

0 commit comments

Comments
 (0)