[Python-checkins] cpython (merge 3.3 -> default): Issue #10182: The re module doesn't truncate indices to 32 bits anymore.

antoine.pitrou python-checkins at python.org
Sun Dec 2 12:56:40 CET 2012


http://hg.python.org/cpython/rev/e7104cc09d02
changeset:   80682:e7104cc09d02
parent:      80679:33b070ef0bad
parent:      80681:21ceee08a375
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Dec 02 12:55:12 2012 +0100
summary:
  Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
Patch by Serhiy Storchaka.

files:
  Lib/test/test_re.py |  17 ++++++++++++++++-
  Misc/NEWS           |   3 +++
  Modules/_sre.c      |  10 +++++-----
  3 files changed, 24 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -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
@@ -949,6 +949,21 @@
         # 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:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -145,6 +145,9 @@
 Library
 -------
 
+- Issue #10182: The re module doesn't truncate indices to 32 bits anymore.
+  Patch by Serhiy Storchaka.
+
 - Issue #16333: use (",", ": ") as default separator when indent is specified
   to avoid trailing whitespace.  Patch by Serhiy Storchaka.
 
diff --git a/Modules/_sre.c b/Modules/_sre.c
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1618,7 +1618,7 @@
 static PyObject *
 sre_codesize(PyObject* self, PyObject *unused)
 {
-    return Py_BuildValue("l", sizeof(SRE_CODE));
+    return PyLong_FromSize_t(sizeof(SRE_CODE));
 }
 
 static PyObject *
@@ -2435,7 +2435,7 @@
         return NULL;
 
     if (subn)
-        return Py_BuildValue("Ni", item, n);
+        return Py_BuildValue("Nn", item, n);
 
     return item;
 
@@ -3387,7 +3387,7 @@
     }
 
     /* 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*
@@ -3410,7 +3410,7 @@
     }
 
     /* 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*)
@@ -3560,7 +3560,7 @@
 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;
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list