[Python-checkins] r68646 - sandbox/trunk/io-c/_iobase.c

antoine.pitrou python-checkins at python.org
Sat Jan 17 03:57:24 CET 2009


Author: antoine.pitrou
Date: Sat Jan 17 03:57:24 2009
New Revision: 68646

Log:
Make IOBase.readline() much more robust



Modified:
   sandbox/trunk/io-c/_iobase.c

Modified: sandbox/trunk/io-c/_iobase.c
==============================================================================
--- sandbox/trunk/io-c/_iobase.c	(original)
+++ sandbox/trunk/io-c/_iobase.c	Sat Jan 17 03:57:24 2009
@@ -363,9 +363,15 @@
 
         if (has_peek) {
             PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);
-            assert(PyBytes_Check(readahead));
             if (readahead == NULL)
                 goto fail;
+            if (!PyBytes_Check(readahead)) {
+                PyErr_Format(PyExc_IOError,
+                             "peek() should have returned a bytes object, "
+                             "not '%.200s'", Py_TYPE(readahead)->tp_name);
+                Py_DECREF(readahead);
+                goto fail;
+            }
             if (PyBytes_GET_SIZE(readahead) > 0) {
                 Py_ssize_t n = 0;
                 const char *buf = PyBytes_AS_STRING(readahead);
@@ -393,15 +399,22 @@
         b = PyObject_CallMethod(self, "read", "n", nreadahead);
         if (b == NULL)
             goto fail;
-        assert(PyBytes_Check(b));
-        if (Py_SIZE(b) == 0) {
+        if (!PyBytes_Check(b)) {
+            PyErr_Format(PyExc_IOError,
+                         "read() should have returned a bytes object, "
+                         "not '%.200s'", Py_TYPE(b)->tp_name);
+            Py_DECREF(b);
+            goto fail;
+        }
+        if (PyBytes_GET_SIZE(b) == 0) {
             Py_DECREF(b);
             break;
         }
 
-        old_size = Py_SIZE(buffer);
-        PyByteArray_Resize(buffer, old_size + Py_SIZE(b));
-        memcpy(PyByteArray_AS_STRING(buffer) + old_size, PyBytes_AS_STRING(b), Py_SIZE(b));
+        old_size = PyByteArray_GET_SIZE(buffer);
+        PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));
+        memcpy(PyByteArray_AS_STRING(buffer) + old_size,
+               PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
 
         Py_DECREF(b);
 
@@ -414,6 +427,7 @@
     Py_DECREF(buffer);
     return result;
   fail:
+    printf("ERROR %d\n", !!PyErr_Occurred());
     Py_DECREF(buffer);
     return NULL;
 }


More information about the Python-checkins mailing list