[Python-3000-checkins] r62808 - in python/branches/py3k: Lib/test/test_memoryio.py Modules/_bytesio.c

alexandre.vassalotti python-3000-checkins at python.org
Wed May 7 03:44:31 CEST 2008


Author: alexandre.vassalotti
Date: Wed May  7 03:44:31 2008
New Revision: 62808

Log:
Changed _bytesio.c to avoid comparing a signed with an unsigned value.
Added tests for overflow checks.


Modified:
   python/branches/py3k/Lib/test/test_memoryio.py
   python/branches/py3k/Modules/_bytesio.c

Modified: python/branches/py3k/Lib/test/test_memoryio.py
==============================================================================
--- python/branches/py3k/Lib/test/test_memoryio.py	(original)
+++ python/branches/py3k/Lib/test/test_memoryio.py	Wed May  7 03:44:31 2008
@@ -7,6 +7,7 @@
 from test import test_support
 
 import io
+import sys
 
 try:
     import _bytesio
@@ -403,6 +404,19 @@
     class CBytesIOTest(PyBytesIOTest):
         ioclass = io.BytesIO
 
+        def test_overflow(self):
+            buf = self.buftype("a")
+            memio = self.ioclass()
+
+            memio.seek(sys.maxsize)
+            self.assertRaises(OverflowError, memio.seek, 1, 1)
+            # Ensure that the position has not been changed
+            self.assertEqual(memio.tell(), sys.maxsize)
+            self.assertEqual(memio.write(self.EOF), 0)
+            self.assertRaises(OverflowError, memio.write, buf)
+            self.assertEqual(memio.tell(), sys.maxsize)
+
+
 def test_main():
     tests = [PyBytesIOTest, PyStringIOTest]
     if has_c_implementation:

Modified: python/branches/py3k/Modules/_bytesio.c
==============================================================================
--- python/branches/py3k/Modules/_bytesio.c	(original)
+++ python/branches/py3k/Modules/_bytesio.c	Wed May  7 03:44:31 2008
@@ -110,16 +110,8 @@
     assert(self->pos >= 0);
     assert(len >= 0);
 
-    /* This overflow check is not strictly necessary. However, it avoids us to
-       deal with funky things like comparing an unsigned and a signed
-       integer. */
-    if (self->pos > PY_SSIZE_T_MAX - len) {
-        PyErr_SetString(PyExc_OverflowError,
-                        "new position too large");
-        return -1;
-    }
-    if (self->pos + len > self->buf_size) {
-        if (resize_buffer(self, self->pos + len) < 0)
+    if ((size_t)self->pos + len > self->buf_size) {
+        if (resize_buffer(self, (size_t)self->pos + len) < 0)
             return -1;
     }
 


More information about the Python-3000-checkins mailing list