]> granicus.if.org Git - python/commitdiff
Patch #1535500: fix segfault in BZ2File.writelines and make sure it
authorGeorg Brandl <georg@python.org>
Mon, 14 Aug 2006 21:42:55 +0000 (21:42 +0000)
committerGeorg Brandl <georg@python.org>
Mon, 14 Aug 2006 21:42:55 +0000 (21:42 +0000)
raises the correct exceptions.

Lib/test/test_bz2.py
Misc/NEWS
Modules/bz2module.c

index 56b5ffa0ad53d5724ccfcb949d8bcb146f92f019..35eae1e71fbd3dcdfba86a46fff98135074b2afa 100644 (file)
@@ -166,6 +166,8 @@ class BZ2FileTest(BaseTest):
         sio = StringIO(self.TEXT)
         bz2f.writelines(sio.readlines())
         bz2f.close()
+        # patch #1535500
+        self.assertRaises(ValueError, bz2f.writelines, ["a"])
         f = open(self.filename, 'rb')
         self.assertEqual(self.decompress(f.read()), self.TEXT)
         f.close()
index 2d1a5e4d38b1ac7ce05409a20c2a62c2e79f8b7b..06dde4aeec764e135b4a855ff7860b29bf8943eb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -79,6 +79,9 @@ Library
 Extension Modules
 -----------------
 
+- Patch #1535500: fix segfault in BZ2File.writelines and make sure it
+  raises the correct exceptions.
+
 - Patch # 1536908: enable building ctypes on OpenBSD/AMD64.  The
   '-no-stack-protector' compiler flag for OpenBSD has been removed.
 
index 5e5a801907d8362f97bd2bee7d12dc05fc54246a..24b24891518c335080d0ec353b48dda4b4975cf7 100644 (file)
@@ -812,12 +812,12 @@ BZ2File_write(BZ2FileObject *self, PyObject *args)
                case MODE_CLOSED:
                        PyErr_SetString(PyExc_ValueError,
                                        "I/O operation on closed file");
-                       goto cleanup;;
+                       goto cleanup;
 
                default:
                        PyErr_SetString(PyExc_IOError,
                                        "file is not ready for writing");
-                       goto cleanup;;
+                       goto cleanup;
        }
 
        self->f_softspace = 0;
@@ -861,6 +861,21 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq)
        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 error;
+
+               default:
+                       PyErr_SetString(PyExc_IOError,
+                                       "file is not ready for writing");
+                       goto error;
+       }
+
        islist = PyList_Check(seq);
        if  (!islist) {
                iter = PyObject_GetIter(seq);