[Numpy-discussion] fromfile and ticket #1152

Jed Ludlow jed.ludlow at gmail.com
Sat Jun 27 14:26:04 EDT 2009


Charles R Harris <charlesr.harris <at> gmail.com> writes:

> 
> The question is: what should happen when fewer items are read than
> requested. The current behaviour is
> 
> 1) Error message written to stderr (needs to be fixed)
> 2) If 0 items are read then nomemory error is raised ;)
> 
> So, should a warning be raised and an array returned with however many
> items were read? Meaning an empty array if nothing was read. Or should
> and error be raised in this circumstance? The behaviour is currently
> undocumented.
> 
> Chuck
> 

Of course, this discussion isn't new, and I don't know that it needs to be
completely rehashed.  See

http://mail.scipy.org/pipermail/numpy-discussion/2009-May/042668.html

and the discussion that followed.  I also noticed that it was suggested that a
ticket should be filed on the issue in a separate discussion:

http://mail.scipy.org/pipermail/numpy-discussion/2009-May/042638.html

I noticed the inconsistency myself recently and noticed that a ticket didn't
exist and felt inclined to create one.  Incidentally, I was reading from two
binary files using fromfile() for one (flat uint32s) and fid.read() for another
(full of packed C structures) and found myself having to handle exceptions AND
check return lengths in the first case and only having to check return lengths
in the second.

I would vote toward harmonizing the behavior with the python built-in
fid.read(bytes) as the default, which simply returns as many items as could be
read before the EOF was reached, including zero.  An additional strict interface
could be added (by keyword) for those who want an exception raised whenever the
requested read count could not be completed.

Jed





More information about the NumPy-Discussion mailing list