[Python-checkins] r67105 - sandbox/trunk/io-c/io.c

amaury.forgeotdarc python-checkins at python.org
Wed Nov 5 00:38:50 CET 2008


Author: amaury.forgeotdarc
Date: Wed Nov  5 00:38:50 2008
New Revision: 67105

Log:
Properly free BufferObject instances, 
and make sure that readall() return an empty string when there is nothig to read.


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

Modified: sandbox/trunk/io-c/io.c
==============================================================================
--- sandbox/trunk/io-c/io.c	(original)
+++ sandbox/trunk/io-c/io.c	Wed Nov  5 00:38:50 2008
@@ -707,7 +707,7 @@
     if (res == NULL)
         return NULL;
     if (res != Py_True) {
-	Py_CLEAR(res);
+        Py_CLEAR(res);
         PyErr_SetString(PyExc_IOError, "File or stream is not seekable.");
     }
     return res;
@@ -732,7 +732,7 @@
     if (res == NULL)
         return NULL;
     if (res != Py_True) {
-	Py_CLEAR(res);
+        Py_CLEAR(res);
         PyErr_SetString(PyExc_IOError, "File or stream is not readable.");
     }
     return res;
@@ -757,7 +757,7 @@
     if (res == NULL)
         return NULL;
     if (res != Py_True) {
-	Py_CLEAR(res);
+        Py_CLEAR(res);
         PyErr_SetString(PyExc_IOError, "File or stream is not writable.");
     }
     return res;
@@ -768,7 +768,7 @@
 static PyObject *
 IOBase_enter(PyObject *self, PyObject *args)
 {
-	if (IOBase_checkClosed(self, NULL) == NULL)
+    if (IOBase_checkClosed(self, NULL) == NULL)
         return NULL;
 
     Py_INCREF(self);
@@ -1188,26 +1188,21 @@
 static PyObject *
 RawIOBase_readall(PyObject *self, PyObject *args)
 {
-    PyObject *b;
+    PyObject *b = NULL;
     Py_ssize_t cursize = 0;
 
-    b = PyBytes_FromStringAndSize(NULL, 1); /* Avoid shared buffer */
-    if (b == NULL)
-        return NULL;
-    assert (Py_REFCNT(b) == 1);
-
     while (1) {
         Py_ssize_t length;
         PyObject *data = PyObject_CallMethod(self, "read",
                                              "i", DEFAULT_BUFFER_SIZE);
 
         if (!data) {
-            Py_DECREF(b);
+            Py_XDECREF(b);
             return NULL;
         }
 
-        if (!PyBytes_Check(b)) {
-            Py_DECREF(b);
+        if (!PyBytes_Check(data)) {
+            Py_XDECREF(b);
             Py_DECREF(data);
             PyErr_SetString(PyExc_TypeError, "read() should return bytes");
             return NULL;
@@ -1215,13 +1210,23 @@
 
         length = Py_SIZE(data);
 
+        if (b == NULL)
+            b = data;
+        else if (length != 0) {
+
+            _PyBytes_Resize(&b, cursize + length);
+            if (b == NULL) {
+                Py_DECREF(data);
+                return NULL;
+            }
+
+            memcpy(PyBytes_AS_STRING(b) + cursize,
+                   PyBytes_AS_STRING(data), length);
+            Py_DECREF(data);
+        }
+
         if (length == 0)
             break;
-
-        _PyBytes_Resize(&b, cursize + length);
-        memcpy(PyBytes_AS_STRING(b) + cursize,
-               PyBytes_AS_STRING(data), length);
-        Py_DECREF(data);
     }
 
     return b;
@@ -1398,6 +1403,19 @@
     PyObject *dict;
     PyObject *weakreflist;
 } BufferedObject;
+
+static void
+BufferedObject_dealloc(BufferedObject *self)
+{
+    if (self->weakreflist != NULL)
+        PyObject_ClearWeakRefs((PyObject *)self);
+    Py_CLEAR(self->raw);
+    Py_CLEAR(self->name);
+    Py_CLEAR(self->mode);
+    Py_CLEAR(self->read_buf);
+    Py_CLEAR(self->write_buf);
+    Py_CLEAR(self->dict);
+}
 
 
 /*
@@ -1978,7 +1996,7 @@
     "BufferedReader",           /*tp_name*/
     sizeof(BufferedObject),     /*tp_basicsize*/
     0,                          /*tp_itemsize*/
-    0,                          /*tp_dealloc*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
     0,                          /*tp_print*/
     0,                          /*tp_getattr*/
     0,                          /*tp_setattr*/
@@ -2345,7 +2363,7 @@
     "BufferedWriter",           /*tp_name*/
     sizeof(BufferedObject),     /*tp_basicsize*/
     0,                          /*tp_itemsize*/
-    0,                          /*tp_dealloc*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
     0,                          /*tp_print*/
     0,                          /*tp_getattr*/
     0,                          /*tp_setattr*/
@@ -2419,35 +2437,35 @@
     Py_ssize_t max_buffer_size = -1;
 
     if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer,
-			  &buffer_size, &max_buffer_size)) {
+                          &buffer_size, &max_buffer_size)) {
         return -1;
     }
 
     if (IOBase_checkReadable(reader, NULL) == NULL)
-	return -1;
+        return -1;
     if (IOBase_checkWritable(writer, NULL) == NULL)
-	return -1;
+        return -1;
 
     args = Py_BuildValue("(n)", buffer_size);
     if (args == NULL) {
-	Py_CLEAR(self->reader);
-	return -1;
+        Py_CLEAR(self->reader);
+        return -1;
     }
     self->reader = (BufferedObject*)PyType_GenericNew(&BufferedReader_Type, args, NULL);
     Py_DECREF(args);
     if (self->reader == NULL)
-	return -1;
+        return -1;
 
     args = Py_BuildValue("(nn)", buffer_size, max_buffer_size);
     if (args == NULL) {
-	Py_CLEAR(self->reader);
-	return -1;
+        Py_CLEAR(self->reader);
+        return -1;
     }
     self->writer = (BufferedObject*)PyType_GenericNew(&BufferedWriter_Type, args, NULL);
     Py_DECREF(args);
     if (self->writer == NULL) {
-	Py_CLEAR(self->reader);
-	return -1;
+        Py_CLEAR(self->reader);
+        return -1;
     }
     return 0;
 }
@@ -2466,8 +2484,8 @@
     PyObject *ret;
 
     if (func == NULL) {
-	PyErr_SetString(PyExc_AttributeError, name);
-	return NULL;
+        PyErr_SetString(PyExc_AttributeError, name);
+        return NULL;
     }
 
     ret = PyObject_CallObject(func, args);
@@ -2522,7 +2540,7 @@
 {
     PyObject *ret = _forward_call(self->writer, "close", args);
     if (ret == NULL)
-	return NULL;
+        return NULL;
     Py_DECREF(ret);
 
     return _forward_call(self->reader, "close", args);
@@ -2534,8 +2552,8 @@
     PyObject *ret = _forward_call(self->writer, "isatty", args);
 
     if (ret != Py_False) {
-	/* either True or exception */
-	return ret;
+        /* either True or exception */
+        return ret;
     }
     Py_DECREF(ret);
 
@@ -2563,7 +2581,7 @@
 PyTypeObject BufferedRWPair_Type = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "BufferedRWPair",           /*tp_name*/
-    sizeof(BufferedObject),     /*tp_basicsize*/
+    sizeof(BufferedRWPairObject),     /*tp_basicsize*/
     0,                          /*tp_itemsize*/
     (destructor)BufferedRWPair_dealloc,     /*tp_dealloc*/
     0,                          /*tp_print*/
@@ -2860,7 +2878,7 @@
     "BufferedRandom",           /*tp_name*/
     sizeof(BufferedObject),     /*tp_basicsize*/
     0,                          /*tp_itemsize*/
-    0,                          /*tp_dealloc*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
     0,                          /*tp_print*/
     0,                          /*tp_getattr*/
     0,                          /*tp_setattr*/


More information about the Python-checkins mailing list