[Python-checkins] r69104 - in python/branches/io-c: Lib/test/test_io.py Modules/io.c

antoine.pitrou python-checkins at python.org
Thu Jan 29 22:23:42 CET 2009


Author: antoine.pitrou
Date: Thu Jan 29 22:23:42 2009
New Revision: 69104

Log:
Fix some crashers found by Victor



Modified:
   python/branches/io-c/Lib/test/test_io.py
   python/branches/io-c/Modules/io.c

Modified: python/branches/io-c/Lib/test/test_io.py
==============================================================================
--- python/branches/io-c/Lib/test/test_io.py	(original)
+++ python/branches/io-c/Lib/test/test_io.py	Thu Jan 29 22:23:42 2009
@@ -1905,16 +1905,32 @@
                               b"" if "b" in kwargs['mode'] else "")
             self.assertRaises(ValueError, f.writelines, [])
 
+    def test_blockingioerror(self):
+        # Various BlockingIOError issues
+        self.assertRaises(TypeError, io.BlockingIOError)
+        self.assertRaises(TypeError, io.BlockingIOError, 1)
+        self.assertRaises(TypeError, io.BlockingIOError, 1, 2, 3, 4)
+        self.assertRaises(TypeError, io.BlockingIOError, 1, "", None)
+        b = io.BlockingIOError(1, "")
+        self.assertEqual(b.characters_written, 0)
+        class C(str):
+            pass
+        c = C("")
+        b = io.BlockingIOError(1, c)
+        c.b = b
+        b.c = c
+        wr = weakref.ref(c)
+        del c, b
+        gc.collect()
+        self.assert_(wr() is None, wr)
 
 def test_main():
-    support.run_unittest(
-        IOTest, BytesIOTest, StringIOTest,
-                              BufferedReaderTest, BufferedWriterTest,
-                              BufferedRWPairTest, BufferedRandomTest,
-                              StatefulIncrementalDecoderTest,
-                              TextIOWrapperTest, MiscIOTest
-                              )
+    support.run_unittest(IOTest, BytesIOTest, StringIOTest,
+                         BufferedReaderTest, BufferedWriterTest,
+                         BufferedRWPairTest, BufferedRandomTest,
+                         StatefulIncrementalDecoderTest,
+                         TextIOWrapperTest, MiscIOTest
+                         )
 
 if __name__ == "__main__":
     test_main()
-    #unittest.main()

Modified: python/branches/io-c/Modules/io.c
==============================================================================
--- python/branches/io-c/Modules/io.c	(original)
+++ python/branches/io-c/Modules/io.c	Thu Jan 29 22:23:42 2009
@@ -92,41 +92,29 @@
 BlockingIOError_init(PyBlockingIOErrorObject *self, PyObject *args,
                      PyObject *kwds)
 {
-    PyObject *myerrno = NULL, *strerror = NULL, *written;
+    PyObject *myerrno = NULL, *strerror = NULL;
     PyObject *baseargs = NULL;
+    Py_ssize_t written = 0;
 
     assert(PyTuple_Check(args));
 
-    if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3)
-        return 0;
+    self->written = 0;
+    if (!PyArg_ParseTuple(args, "OO|n:BlockingIOError",
+                          &myerrno, &strerror, &written))
+        return -1;
 
-    baseargs = PyTuple_GetSlice(args, 0, 2);
+    baseargs = PyTuple_Pack(2, myerrno, strerror);
     if (baseargs == NULL)
         return -1;
-
+    /* This will take care of initializing of myerrno and strerror members */
     if (((PyTypeObject *)PyExc_IOError)->tp_init(
                 (PyObject *)self, baseargs, kwds) == -1) {
         Py_DECREF(baseargs);
         return -1;
     }
-
     Py_DECREF(baseargs);
 
-    if (!PyArg_UnpackTuple(args, "BlockingIOError", 2, 3,
-                           &myerrno, &strerror, &written)) {
-        return -1;
-    }
-
-    Py_INCREF(myerrno);
-    self->myerrno = myerrno;
-
-    Py_INCREF(strerror);
-    self->strerror = strerror;
-
-    self->written = PyNumber_AsSsize_t(written, PyExc_ValueError);
-    if(self->written == -1 && PyErr_Occurred())
-        return -1;
-
+    self->written = written;
     return 0;
 }
 
@@ -135,7 +123,6 @@
     {NULL}  /* Sentinel */
 };
 
-
 static PyTypeObject _PyExc_BlockingIOError = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "BlockingIOError", /*tp_name*/
@@ -156,8 +143,9 @@
     0,                          /*tp_getattro*/
     0,                          /*tp_setattro*/
     0,                          /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,  /*tp_flags*/
-    PyDoc_STR("Exception raised when I/O would block on a non-blocking I/O stream"), /* tp_doc */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+    PyDoc_STR("Exception raised when I/O would block "
+              "on a non-blocking I/O stream"), /* tp_doc */
     0,                          /* tp_traverse */
     0,                          /* tp_clear */
     0,                          /* tp_richcompare */


More information about the Python-checkins mailing list