summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2010-02-20 18:16:05 +0000
committerPauli Virtanen <pav@iki.fi>2010-02-20 18:16:05 +0000
commit2cd9854ecd3b5428744399d05b7c450fa0603a81 (patch)
treea8f1de84eab448a48a7dfe5d913bc4a4b24c9b72 /numpy/lib
parente247b1d3d3a93b5119524ce6306239415e89ae74 (diff)
downloadnumpy-2cd9854ecd3b5428744399d05b7c450fa0603a81.tar.gz
ENH: lib: write fortran-contiguous data to files using arr.T.tofile instead of arr.data (required for Py3 compatibility)
The issue is that when passing a buffer object to Python's io.BufferedWriter.write, it will try to obtain the buffer using PyBUF_ND | PyBUF_C_CONTIGUOUS. This will fail for strided arrays -- seems to be an issue in Python, as it probably should try to obtain a SIMPLE buffer.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/format.py9
1 files changed, 5 insertions, 4 deletions
diff --git a/numpy/lib/format.py b/numpy/lib/format.py
index 520c6bd25..aeed48950 100644
--- a/numpy/lib/format.py
+++ b/numpy/lib/format.py
@@ -393,9 +393,10 @@ def write_array(fp, array, version=(1,0)):
# Instead, we will pickle it out with version 2 of the pickle protocol.
cPickle.dump(array, fp, protocol=2)
elif array.flags.f_contiguous and not array.flags.c_contiguous:
- # Use a suboptimal, possibly memory-intensive, but correct way to
- # handle Fortran-contiguous arrays.
- fp.write(array.data)
+ if isfileobj(fp):
+ array.T.tofile(fp)
+ else:
+ fp.write(array.T.tostring('C'))
else:
if isfileobj(fp):
array.tofile(fp)
@@ -447,7 +448,7 @@ def read_array(fp):
# This is not a real file. We have to read it the memory-intensive
# way.
# XXX: we can probably chunk this to avoid the memory hit.
- data = fp.read(count * dtype.itemsize)
+ data = fp.read(int(count * dtype.itemsize))
array = numpy.fromstring(data, dtype=dtype, count=count)
if fortran_order: