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

Skip to content

Commit 43fb54c

Browse files
committed
Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
Patch by Serhiy Storchaka.
1 parent 56379c0 commit 43fb54c

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
@@ -854,6 +854,21 @@ def test_compile(self):
854854
# Test behaviour when not given a string or pattern as parameter
855855
self.assertRaises(TypeError, re.compile, 0)
856856

857+
# The huge memuse is because of re.sub() using a list and a join()
858+
# to create the replacement result.
859+
@bigmemtest(size=_2G, memuse=20)
860+
def test_large(self, size):
861+
# Issue #10182: indices were 32-bit-truncated.
862+
s = 'a' * size
863+
m = re.search('$', s)
864+
self.assertIsNotNone(m)
865+
self.assertEqual(m.start(), size)
866+
self.assertEqual(m.end(), size)
867+
r, n = re.subn('', '', s)
868+
self.assertEqual(r, s)
869+
self.assertEqual(n, size + 1)
870+
871+
857872
def run_re_tests():
858873
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
859874
if verbose:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ Core and Builtins
169169
Library
170170
-------
171171

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

Modules/_sre.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ static PyObject*pattern_scanner(PatternObject*, PyObject*);
16291629
static PyObject *
16301630
sre_codesize(PyObject* self, PyObject *unused)
16311631
{
1632-
return Py_BuildValue("l", sizeof(SRE_CODE));
1632+
return PyLong_FromSize_t(sizeof(SRE_CODE));
16331633
}
16341634

16351635
static PyObject *
@@ -2467,7 +2467,7 @@ pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string,
24672467
return NULL;
24682468

24692469
if (subn)
2470-
return Py_BuildValue("Ni", item, n);
2470+
return Py_BuildValue("Nn", item, n);
24712471

24722472
return item;
24732473

@@ -3423,7 +3423,7 @@ match_start(MatchObject* self, PyObject* args)
34233423
}
34243424

34253425
/* mark is -1 if group is undefined */
3426-
return Py_BuildValue("i", self->mark[index*2]);
3426+
return PyLong_FromSsize_t(self->mark[index*2]);
34273427
}
34283428

34293429
static PyObject*
@@ -3446,7 +3446,7 @@ match_end(MatchObject* self, PyObject* args)
34463446
}
34473447

34483448
/* mark is -1 if group is undefined */
3449-
return Py_BuildValue("i", self->mark[index*2+1]);
3449+
return PyLong_FromSsize_t(self->mark[index*2+1]);
34503450
}
34513451

34523452
LOCAL(PyObject*)
@@ -3596,7 +3596,7 @@ static PyObject *
35963596
match_lastindex_get(MatchObject *self)
35973597
{
35983598
if (self->lastindex >= 0)
3599-
return Py_BuildValue("i", self->lastindex);
3599+
return PyLong_FromSsize_t(self->lastindex);
36003600
Py_INCREF(Py_None);
36013601
return Py_None;
36023602
}

0 commit comments

Comments
 (0)