summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_format.py
diff options
context:
space:
mode:
authorMax Sperlich <max.sperlich@livingsocial.com>2013-12-09 19:44:36 -0500
committerMax Sperlich <max.sperlich@livingsocial.com>2013-12-09 20:07:51 -0500
commitbdb6f8cabf755d4d7b18a3d7e7475480ce6fc008 (patch)
tree837f32711e73a856adc2b89feccf967bb97a411a /numpy/lib/tests/test_format.py
parent161ca8ff732ed58613b9bdcb2f1dcad7178a76c5 (diff)
downloadnumpy-bdb6f8cabf755d4d7b18a3d7e7475480ce6fc008.tar.gz
MAINT: Better handling of very small chunks
Issue 4093: Improved the handling of the case when the amount of data read by fp.read is smaller than dtype.itemsize. Also changed the test code to make sure this case is tested.
Diffstat (limited to 'numpy/lib/tests/test_format.py')
-rw-r--r--numpy/lib/tests/test_format.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index b5b6ef885..abb93fbd5 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -411,12 +411,16 @@ record_arrays = [
]
-#BytesIO that returns fewer bytes than requested
-class BytesIORandomSize(BytesIO):
+#BytesIO that reads a single byte at a time
+class BytesIOSingleByte(BytesIO):
def read(self, size=None):
- from random import randint
- size = randint(min(512, size), size)
- return super(BytesIORandomSize, self).read(size)
+ #Return first 512 bytes normally, otherwise checking magic bytes
+ #and reading header fails
+ if self.tell() > 512:
+ size = 1
+ else:
+ size = min(size, 512)
+ return super(BytesIOSingleByte, self).read(size)
def roundtrip(arr):
@@ -427,10 +431,10 @@ def roundtrip(arr):
return arr2
-def roundtrip_randsize(arr):
+def roundtrip_onebyte(arr):
f = BytesIO()
format.write_array(f, arr)
- f2 = BytesIORandomSize(f.getvalue())
+ f2 = BytesIOSingleByte(f.getvalue())
arr2 = format.read_array(f2)
return arr2
@@ -454,10 +458,11 @@ def test_roundtrip():
yield assert_array_equal, arr, arr2
-def test_roundtrip_randsize():
- for arr in basic_arrays[2:] + record_arrays:
- arr2 = roundtrip_randsize(arr)
- yield assert_array_equal, arr, arr2
+def test_roundtrip_onebyte():
+ for arr in basic_arrays + record_arrays:
+ if arr.dtype != object:
+ arr2 = roundtrip_onebyte(arr)
+ yield assert_array_equal, arr, arr2
def test_roundtrip_truncated():