473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

--
Steven
Sep 27 '08 #1
1 1141
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(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\x 00\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-fil...s.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
Sep 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
9
by: Alfonso Morra | last post by:
I have the following data types: typedef union { long l ; double f; char* s ; void* p ; } Value ; typedef struct {
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
6
by: dee | last post by:
Here is a a list of my hyperlinks in my home page: <A class="theclass" href="Default.aspx">Home</A> <A class="theclass" href="Search.aspx">Search</A> <A class="theclass"...
5
by: BACON | last post by:
I'm just starting the process of reorganising my modest little website and cleaning up all the HTML, and the logical place to begin was with the homepage. I made a simple little ASP.NET control...
25
by: gamehack | last post by:
Hi all, I'm writing an application which will be opening binary files with definite sizes for integers(2 byte integers and 4 byte longs). Is there a portable way in being sure that each integer...
3
by: axlq | last post by:
I created an experimental page at: http://sunbeam.rahul.net/~unicorn/csstest.html ....which is a standard header + nav bar + 2-column content + footer CSS layout. The left column is 10em wide...
15
by: Ivan Novick | last post by:
Hi, Is it possible to have negative integer literal or only positive? As far as I understand, the code below would be a positive integer literal and the unary negative operator. x = -3.2; ...
2
by: dave8421 | last post by:
Hi, I am reading CSS 2.1 draft specification: http://www.w3.org/TR/2006/WD-CSS21-20061106/ Part of section 10.3.3 (Block-level, non-replaced elements in normal flow) reads: If there is...
0
by: devil81 | last post by:
Hi: I would be extremely grateful if you could help me with what is quickly becoming a rather annoying code issue. I have coded a recursive emulation of ls -l and am completely stuck on coding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.