[issue18430] gzip, bz2, lzma: peek advances file position of existing file object

Nick Bargnesi report at bugs.python.org
Thu Jul 11 21:34:01 CEST 2013


New submission from Nick Bargnesi:

Using existing file objects as arguments to the open functions in the gzip, bz2, and lzma libraries can cause the underlying fileobj position to get changed - and not quite in ways one would expect.

Calling peek against the returned file objects -- gzip.GzipFile, bz2.BZ2File, and lzma.LZMAFile will in one scenario advance the position of the supplied file object:

    >>> import bz2
    >>> fileobj = open('test.bz2', mode='rb')
    >>> bzfile = bz2.open(fileobj)
    >>>
    >>> # file positions at 0
    >>> assert fileobj.tell() == 0, bzfile.tell() == 0
    >>>
    >>> bzfile.peek()
    b'Test file.\n'
    >>> fileobj.tell()
    52

If after the initial peek, we rewind the underlying fileobj and peek again, the behavior changes:

    >>> fileobj.seek(0)
    0
    >>> bzfile.peek()
    b'Test file.\n'
    >>> fileobj.tell()
    0

The second scenario serves to complicate things a bit with the change in behavior.

I would be less surprised if the module documentation simply stated the affect on file position of the file object being used. Though it would be beautiful if the underlying file object didn't change at all. The latter seems possible since the three modules know whether the file object is seekable.

The gzip and lzma modules exhibit similar behavior - gzip for example:

    >>> import gzip
    >>> fileobj = open('test.gz', mode='rb')
    >>> gzfile = gzip.open(fileobj)
    >>>
    >>> # file positions at 0
    >>> assert fileobj.tell() == 0 and gzfile.tell() == 0
    >>>
    >>> gzfile.peek(1)
    b'Test file.\n'
    >>> fileobj.tell()
    36
    >>>
    >>> # rewind, and do it again
    >>> fileobj.seek(0)
    >>>
    >>> gzfile.peek(1)
    b'Test file.\n'
    >>> fileobj.tell()
    0

----------
components: Library (Lib)
messages: 192890
nosy: nbargnesi
priority: normal
severity: normal
status: open
title: gzip, bz2, lzma: peek advances file position of existing file object
type: enhancement
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18430>
_______________________________________


More information about the Python-bugs-list mailing list