summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2011-03-30 19:52:03 -0600
committerCharles Harris <charlesr.harris@gmail.com>2011-03-30 19:52:03 -0600
commit706c9b6553f0f9143a9854db7f25d7de8bf2598d (patch)
treefa96bd0f4a68f33a0642956ec5db19d2e497ee3b /numpy/lib
parent51a1895234b25b8bf0ace24b8ee881a91c39601c (diff)
parent354d7a311ef9a6dfd4695147c644ff0294c8c4ee (diff)
downloadnumpy-706c9b6553f0f9143a9854db7f25d7de8bf2598d.tar.gz
Merge branch 'test-genfromtxt-fname' of git://github.com/matthew-brett/numpy into test-genfromtxt-fname
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py2
-rw-r--r--numpy/lib/tests/test_io.py21
2 files changed, 21 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 9fbacaa22..e73d6fa39 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 5001f6bac..96e92c776 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 @@ M 33 21.99
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))