This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: BZ2File doesn't protect against mixed iterator and read usage
Type: Stage: resolved
Components: None Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pitrou Nosy List: Alex.Stapleton, georg.brandl, pitrou
Priority: high Keywords: patch

Created on 2010-04-14 13:01 by Alex.Stapleton, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
bziter.patch pitrou, 2010-08-01 19:33
Messages (4)
msg103126 - (view) Author: Alex Stapleton (Alex.Stapleton) Date: 2010-04-14 13:01
Normal files throw exceptions if you mix methods.

>>> f = open("words")
>>> for l in f:
...     break
>>> f.tell()
>>> f.readline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Mixing iteration and read methods would lose data

BZ2Files silently do the wrong thing. (Output is a coincidence. Honest!)

>>> import bz2
>>> f = bz2.BZ2File("words.bz2")
>>> for l in f:
...     break
>>> f.tell()
>>> f.readline()

Expected behaviour is for it to throw a ValueError like normal file objects.
msg112327 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-01 13:57
Well I'm not the bz2 maintainer but I could take a look. The BZ2File implementation is generally a straight ripoff of the 2.x file object, with (de)compression calls added where necessary. I guess the ripoff was a one-shot effort and subsequent maintenance wasn't done.

It makes it quite a bit alien in the 3.x IO landscape, but until someone decides to rewrite it we'll have to live with that.
msg112372 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-01 19:33
Here is a patch for py3k.
msg112378 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-01 20:17
Fixed in r83440 (py3k), r83441 (3.1), r83442 (2.7), r83443 (2.6).
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52644
2010-08-01 20:17:42pitrousetstatus: open -> closed
resolution: fixed
messages: + msg112378

stage: resolved
2010-08-01 19:33:07pitrousetfiles: + bziter.patch

nosy: + georg.brandl
messages: + msg112372

keywords: + patch
2010-08-01 13:57:27pitrousetmessages: + msg112327
2010-08-01 07:37:18georg.brandlsetpriority: normal -> high
assignee: pitrou

nosy: + pitrou
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.5
2010-04-14 13:01:14Alex.Stapletoncreate