Issue 8397: BZ2File doesn't protect against mixed iterator and read usage (original) (raw)

Issue8397

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

Files
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() 8192L >>> f.readline() Traceback (most recent call last): File "", line 1, in 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() 8192L >>> f.readline() 'lose\n' 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).
History
Date User Action Args
2022-04-11 14:56:59 admin set github: 52644
2010-08-01 20:17:42 pitrou set status: open -> closedresolution: fixedmessages: + stage: resolved
2010-08-01 19:33:07 pitrou set files: + bziter.patchnosy: + georg.brandlmessages: + keywords: + patch
2010-08-01 13:57:27 pitrou set messages: +
2010-08-01 07:37:18 georg.brandl set priority: normal -> highassignee: pitrounosy: + pitrouversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.5
2010-04-14 13:01:14 Alex.Stapleton create