diff options
-rw-r--r-- | numpy/lib/npyio.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 12 |
2 files changed, 13 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 7d561a393..6873a4785 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1002,7 +1002,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', fh = open(fname, 'wb') else: fh = open(fname, 'w') - elif hasattr(fname, 'seek'): + elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index a4a76f952..4095dd813 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -401,6 +401,18 @@ class TestSaveTxt(TestCase): [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n']) + def test_custom_writer(self): + + class CustomWriter(list): + def write(self, text): + self.extend(text.split(b'\n')) + + w = CustomWriter() + a = np.array([(1, 2), (3, 4)]) + np.savetxt(w, a) + b = np.loadtxt(w) + assert_array_equal(a, b) + def _assert_floatstr_lines_equal(actual_lines, expected_lines): """A string comparison function that also works on Windows + Python 2.5. |