Negative block sizes with file-like objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    #1

    Negative block sizes with file-like objects

    I have a proxy class that wraps an arbitrary file-like object fp and
    reads blocks of data from it. Is it safe to assume that fp.read(-1) will
    read until EOF? I know that's true for file.read() and StringIO.read() ,
    but is it a reasonable assumption to make for arbitrary file-like objects?

    To put it in more concrete terms, I have a class like this:

    class C(object):
    # Much simplified version.
    def __init__(self, fp):
    self.fp = fp
    def read(self, size=-1):
    return self.fp.read(si ze)


    Should I re-write the read() method like this?

    def read(self, size=-1):
    if size < 0:
    return self.fp.read()
    else:
    return self.fp.read(si ze)



    --
    Steven
  • Peter Otten

    #2
    Re: Negative block sizes with file-like objects

    Steven D'Aprano wrote:
    I have a proxy class that wraps an arbitrary file-like object fp and
    reads blocks of data from it. Is it safe to assume that fp.read(-1) will
    read until EOF? I know that's true for file.read() and StringIO.read() ,
    but is it a reasonable assumption to make for arbitrary file-like objects?
    >
    To put it in more concrete terms, I have a class like this:
    >
    class C(object):
    # Much simplified version.
    def __init__(self, fp):
    self.fp = fp
    def read(self, size=-1):
    return self.fp.read(si ze)
    >
    >
    Should I re-write the read() method like this?
    >
    def read(self, size=-1):
    if size < 0:
    return self.fp.read()
    else:
    return self.fp.read(si ze)
    Grepping through the python source shows that both -1 and None are used as
    the default size.

    # python2.5.2
    >>import tarfile
    >>open("sample" , "w").write("con tents-of-sample")
    >>t = tarfile.open("s ample.tar", "w")
    >>t.add("sample ")
    >>t.close()
    >>t = tarfile.open("s ample.tar")
    >>t.extractfile ("sample").read ()
    'contents-of-sample'
    >>t.extractfile ("sample").read (-1)[:50]
    'contents-of-sample\x00\x00\ x00\x00\x00\x00 \x00\x00\x00\x0 0\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00\x00 \x00\x00\x00\x0 0\x00\x00\x00\x 00\x00\x00\x00'

    So you may fare better with the rewrite. But even that may fail:



    """
    read( [size])
    [...] If the size argument is negative or omitted, read all data until EOF
    is reached. [...]
    Also note that when in non-blocking mode, less data than what was requested
    may be returned, even if no size parameter was given.
    """

    Peter

    Comment

    Working...