]> granicus.if.org Git - python/commitdiff
Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 2 Dec 2012 11:52:36 +0000 (12:52 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 2 Dec 2012 11:52:36 +0000 (12:52 +0100)
Patch by Serhiy Storchaka.

Lib/test/test_re.py
Misc/NEWS
Modules/_sre.c

index 39972d51e8928d61479d93fc81f1ecb7315b19cf..438ef26fdb05544f383c7a623ae3e9ab28bffebd 100644 (file)
@@ -1,4 +1,4 @@
-from test.support import verbose, run_unittest, gc_collect
+from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G
 import io
 import re
 from re import Scanner
@@ -854,6 +854,21 @@ class ReTests(unittest.TestCase):
         # Test behaviour when not given a string or pattern as parameter
         self.assertRaises(TypeError, re.compile, 0)
 
+    # The huge memuse is because of re.sub() using a list and a join()
+    # to create the replacement result.
+    @bigmemtest(size=_2G, memuse=20)
+    def test_large(self, size):
+        # Issue #10182: indices were 32-bit-truncated.
+        s = 'a' * size
+        m = re.search('$', s)
+        self.assertIsNotNone(m)
+        self.assertEqual(m.start(), size)
+        self.assertEqual(m.end(), size)
+        r, n = re.subn('', '', s)
+        self.assertEqual(r, s)
+        self.assertEqual(n, size + 1)
+
+
 def run_re_tests():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
     if verbose:
index d930f3215449506bf41b775ca0f34483ae330b7c..9d8db7513db192b5f38da4a3107d02f0070c6e07 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -169,6 +169,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
+  Patch by Serhiy Storchaka.
+
 - Issue #16573: In 2to3, treat enumerate() like a consuming call, so superfluous
   list() calls aren't added to filter(), map(), and zip() which are directly
   passed enumerate().
index 9600a080ecda7c0bd741e5c53b60c8967dae4fad..38d0127c99bbcb6316fa1b11aaa2f38c4d59f69c 100644 (file)
@@ -1629,7 +1629,7 @@ static PyObject*pattern_scanner(PatternObject*, PyObject*);
 static PyObject *
 sre_codesize(PyObject* self, PyObject *unused)
 {
-    return Py_BuildValue("l", sizeof(SRE_CODE));
+    return PyLong_FromSize_t(sizeof(SRE_CODE));
 }
 
 static PyObject *
@@ -2467,7 +2467,7 @@ next:
         return NULL;
 
     if (subn)
-        return Py_BuildValue("Ni", item, n);
+        return Py_BuildValue("Nn", item, n);
 
     return item;
 
@@ -3423,7 +3423,7 @@ match_start(MatchObject* self, PyObject* args)
     }
 
     /* mark is -1 if group is undefined */
-    return Py_BuildValue("i", self->mark[index*2]);
+    return PyLong_FromSsize_t(self->mark[index*2]);
 }
 
 static PyObject*
@@ -3446,7 +3446,7 @@ match_end(MatchObject* self, PyObject* args)
     }
 
     /* mark is -1 if group is undefined */
-    return Py_BuildValue("i", self->mark[index*2+1]);
+    return PyLong_FromSsize_t(self->mark[index*2+1]);
 }
 
 LOCAL(PyObject*)
@@ -3596,7 +3596,7 @@ static PyObject *
 match_lastindex_get(MatchObject *self)
 {
     if (self->lastindex >= 0)
-       return Py_BuildValue("i", self->lastindex);
+        return PyLong_FromSsize_t(self->lastindex);
     Py_INCREF(Py_None);
     return Py_None;
 }