Negative block sizes with file-like objects

S

Steven D'Aprano

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(size)


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(size)
 
P

Peter Otten

Steven said:
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(size)


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(size)

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("contents-of-sample")
t = tarfile.open("sample.tar", "w")
t.add("sample")
t.close()
t = tarfile.open("sample.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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

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

http://docs.python.org/lib/bltin-file-objects.html#l2h-295

"""
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top