In the BZ2File object of bz2 module the writelines() method does not
check its closed state before doing the actual work so its behavior
it's different from write()'s behavior. See:

from bz2 import BZ2File
f = BZ2File("foo", "w")
f.close()
f.closed
1
f.write("foobar")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
f.closed
1
f.writelines(["foobar"])
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IOError: unknown IO error

It also doesn't check if it can write for real:

from bz2 import BZ2File
f = BZ2File("foo", "r")
f.write("foobar")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IOError: file is not ready for writing
f.writelines(['foobar'])
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
RuntimeError: wrong sequence of bz2 library commands used

The patch is attached. If you think it's ok to fix this I'll post it
to the bug tracker

--
Lawrence
http://www.oluyede.org/blog
Index: Modules/bz2module.c
===================================================================
--- Modules/bz2module.c (revision 51128)
+++ Modules/bz2module.c (working copy)
@@ -861,6 +861,20 @@
        int bzerror;
 
        ACQUIRE_LOCK(self);
+       switch (self->mode) {
+               case MODE_WRITE:
+                       break;
+
+               case MODE_CLOSED:
+                       PyErr_SetString(PyExc_ValueError,
+                                       "I/O operation on closed file");
+                       goto cleanup;
+
+               default:
+                       PyErr_SetString(PyExc_IOError,
+                                       "file is not ready for writing");
+                       goto cleanup;
+       }
        islist = PyList_Check(seq);
        if  (!islist) {
                iter = PyObject_GetIter(seq);
@@ -951,8 +965,12 @@
        Py_INCREF(Py_None);
        ret = Py_None;
 
-  error:
+cleanup:
        RELEASE_LOCK(self);
+       return ret;
+
+error:
+       RELEASE_LOCK(self);
        Py_XDECREF(list);
        Py_XDECREF(iter);
        return ret;
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to