Permalink
Browse files

BUG: open genfromtxt file as binary; add test for filename use. Fixes

ticket #1607.
  • Loading branch information...
1 parent 8024c27 commit aec64eeaea350cd4dca2ddb024b695b3dec29b16 @matthew-brett matthew-brett committed with charris Mar 30, 2011
Showing with 21 additions and 2 deletions.
  1. +1 −1 numpy/lib/npyio.py
  2. +20 −1 numpy/lib/tests/test_io.py
View
@@ -1202,7 +1202,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
# Initialize the filehandle, the LineSplitter and the NameValidator
own_fhd = False
if isinstance(fname, basestring):
- fhd = np.lib._datasource.open(fname, 'U')
+ fhd = np.lib._datasource.open(fname, 'Ub')
own_fhd = True
elif not hasattr(fname, 'read'):
raise TypeError("The input should be a string or a filehandle. "\
View
@@ -1,6 +1,7 @@
import numpy as np
import numpy.ma as ma
-from numpy.ma.testutils import *
+from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal,
+ assert_raises, run_module_suite)
from numpy.testing import assert_warns
import sys
@@ -1248,6 +1249,24 @@ def test_recfromcsv(self):
self.assertTrue(isinstance(test, np.recarray))
assert_equal(test, control)
+ def test_gft_filename(self):
+ # Test that we can load data from a filename as well as a file object
+ data = '0 1 2\n3 4 5'
+ exp_res = np.arange(6).reshape((2,3))
+ assert_array_equal(np.genfromtxt(StringIO(data)), exp_res)
+ f, name = mkstemp()
+ # Thanks to another windows brokeness, we can't use
+ # NamedTemporaryFile: a file created from this function cannot be
+ # reopened by another open call. So we first put the string
+ # of the test reference array, write it to a securely opened file,
+ # which is then read from by the loadtxt function
+ try:
+ os.write(f, asbytes(data))
+ assert_array_equal(np.genfromtxt(name), exp_res)
+ finally:
+ os.close(f)
+ os.unlink(name)
+
def test_gzip_load():
a = np.random.random((5, 5))

0 comments on commit aec64ee

Please sign in to comment.