diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 9fbacaa226c6..e73d6fa39c52 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -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. "\ diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 5001f6bac3f8..96e92c7763e7 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -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))