[Numpy-svn] r5797 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Tue Sep 9 08:38:50 CDT 2008


Author: stefan
Date: 2008-09-09 08:38:34 -0500 (Tue, 09 Sep 2008)
New Revision: 5797

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
FIX: Loadtxt raises on empty input (closes #908).


Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-09-09 10:55:30 UTC (rev 5796)
+++ trunk/numpy/lib/io.py	2008-09-09 13:38:34 UTC (rev 5797)
@@ -312,10 +312,10 @@
 
     """
     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 @@
     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)
 

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-09-09 10:55:30 UTC (rev 5796)
+++ trunk/numpy/lib/tests/test_io.py	2008-09-09 13:38:34 UTC (rev 5797)
@@ -140,7 +140,6 @@
         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 @@
             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 @@
         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 @@
         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):



More information about the Numpy-svn mailing list