diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2008-09-09 13:38:34 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2008-09-09 13:38:34 +0000 |
commit | 27d5aff700a2841cc0a30a4f774dcc67363fa287 (patch) | |
tree | acd5e41bb946b90b8e4de50b219cdb806ee97277 /numpy/lib | |
parent | b01bf6ffea880fc28daaa6877e2d4653aeab3d7f (diff) | |
download | numpy-27d5aff700a2841cc0a30a4f774dcc67363fa287.tar.gz |
FIX: Loadtxt raises on empty input (closes #908).
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/io.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 8 |
2 files changed, 11 insertions, 7 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 5348866c4..93c955942 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -312,10 +312,10 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, """ user_converters = converters - - if usecols is not None: - usecols = list(usecols) - + + if usecols is not None: + usecols = list(usecols) + if _string_like(fname): if fname.endswith('.gz'): import gzip @@ -361,6 +361,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, first_vals = None while not first_vals: first_line = fh.readline() + if first_line == '': # EOF reached + raise IOError('End-of-file reached before encountering data.') first_vals = split_line(first_line) N = len(usecols or first_vals) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 4e6412e09..9cccbe041 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -140,7 +140,6 @@ class TestLoadTxt(TestCase): y = np.loadtxt(d, dtype=mydescriptor) assert_array_equal(y, b) - def test_array(self): c = StringIO.StringIO() c.write('1 2\n3 4') @@ -188,7 +187,7 @@ class TestLoadTxt(TestCase): usecols=(1, 3, )) a = np.array([[2, -999],[7, 9]], int) assert_array_equal(x, a) - + def test_comments(self): c = StringIO.StringIO() c.write('# comment\n1,2,3,5\n') @@ -230,7 +229,7 @@ class TestLoadTxt(TestCase): x = np.loadtxt(c, dtype=float, usecols=(1,2)) assert_array_equal(x, a[:,1:]) - # Testing with arrays instead of tuples. + # Testing with arrays instead of tuples. c.seek(0) x = np.loadtxt(c, dtype=float, usecols=np.array([1,2])) assert_array_equal(x, a[:,1:]) @@ -255,6 +254,9 @@ class TestLoadTxt(TestCase): a = np.array([(1,(2,3.0)),(4,(5,6.0))], dt) assert_array_equal(x, a) + def test_empty_file(self): + c = StringIO.StringIO() + assert_raises(IOError, np.loadtxt, c) class Testfromregex(TestCase): def test_record(self): |