8000 BUG: open genfromtxt file as binary; add test for filename use by matthew-brett · Pull Request #64 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: open genfromtxt file as binary; add test for filename use #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 chang 8000 es: 1 addition & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "\
Expand Down
21 changes: 20 additions & 1 deletion numpy/lib/tests/test_io.py
9535
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down
0