[Numpy-discussion] fromfile() -- help!

Pauli Virtanen pav at iki.fi
Fri Jan 8 04:22:53 EST 2010

to, 2010-01-07 kello 17:21 -0800, Christopher Barker kirjoitti:
> if I have NumPyOS_ascii_ftolf right, it should return 0 if it doesn't 
> succesfully read a number. However, this looks like it sets the data in 
> *ip, even if the return value is zero.

It may also return EOF (== -1) when encountering end-of-stream. Of
course, I don't think any code should not rely on EOF being -1, and I
doubt that relying on it is intended here.

> It does pass on that return value, but, from ctors.c:
> fromfile_next_element(FILE **fp, void *dptr, PyArray_Descr *dtype,
>                        void *NPY_UNUSED(stream_data))
> {
>      /* the NULL argument is for backwards-compatibility */
>      return dtype->f->scanfunc(*fp, dptr, NULL, dtype);
> }
> just moves it on through. This is called from here:
>          if (next(&stream, dptr, dtype, stream_data) < 0) {
>              break;
>          }
> which is checking for < 0 , so if a zero is returned, it will just go in 
> its merry way...

Yeah, this is of course wrong; for example a file containing "1,2,"
results to np.fromfile("filename.txt", sep=",") == [1, 2, -1] where the
last value is effectively undefined.

Another point to note is that `next` may also be the
fromstr_next_element function; when fixing things also its semantics
should be corrected.


More information about the NumPy-Discussion mailing list