[Python-Dev] BZ2File.writelines should raise more meaningful exceptions

Lawrence Oluyede l.oluyede at gmail.com
Sun Aug 6 10:41:39 CEST 2006


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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: bz2module.diff.txt
Url: http://mail.python.org/pipermail/python-dev/attachments/20060806/bd63dc42/attachment.txt 


More information about the Python-Dev mailing list