summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_format.py
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2014-06-02 00:29:28 +0200
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-06-02 21:26:28 +0200
commitfc50d27b4e125052d44b0643bc344e4c5062316a (patch)
tree8a077bd36d5af1292411e219cf88f7bb4590f17a /numpy/lib/tests/test_format.py
parente9f27b9ad506a8e37565abf72c0e9f23254595de (diff)
downloadnumpy-fc50d27b4e125052d44b0643bc344e4c5062316a.tar.gz
ENH: add storage format 2.0 with 4 byte header length size
The new format only increases the header length field to 4 bytes. allows storing structured arrays with a large number of named columns. The dtype serialization for these can exceed the 2 byte header length field required by the 1.0 format. The generic functions automatically use the 2.0 format if the to be stored data requires it. To avoid unintentional incompatibilies a UserWarning is emitted when this happens. If the format is not required the more compatible 1.0 format is used. Closes gh-4690
Diffstat (limited to 'numpy/lib/tests/test_format.py')
-rw-r--r--numpy/lib/tests/test_format.py57
1 files changed, 55 insertions, 2 deletions
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index c294637ad..1034b5125 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -280,6 +280,7 @@ import sys
import os
import shutil
import tempfile
+import warnings
from io import BytesIO
import numpy as np
@@ -521,19 +522,71 @@ def test_compressed_roundtrip():
assert_array_equal(arr, arr1)
-def test_write_version_1_0():
+def test_version_2_0():
+ f = BytesIO()
+ # requires more than 2 byte for header
+ dt = [(("%d" % i) * 100, float) for i in range(500)]
+ d = np.ones(1000, dtype=dt)
+
+ format.write_array(f, d, version=(2, 0))
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', UserWarning)
+ format.write_array(f, d)
+ assert_(w[0].category is UserWarning)
+
+ f.seek(0)
+ n = format.read_array(f)
+ assert_array_equal(d, n)
+
+ # 1.0 requested but data cannot be saved this way
+ assert_raises(ValueError, format.write_array, f, d, (1, 0))
+
+
+def test_version_2_0_memmap():
+ # requires more than 2 byte for header
+ dt = [(("%d" % i) * 100, float) for i in range(500)]
+ d = np.ones(1000, dtype=dt)
+ tf = tempfile.mktemp('', 'mmap', dir=tempdir)
+
+ # 1.0 requested but data cannot be saved this way
+ assert_raises(ValueError, format.open_memmap, tf, mode='w+', dtype=d.dtype,
+ shape=d.shape, version=(1, 0))
+
+ ma = format.open_memmap(tf, mode='w+', dtype=d.dtype,
+ shape=d.shape, version=(2, 0))
+ ma[...] = d
+ del ma
+
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', UserWarning)
+ ma = format.open_memmap(tf, mode='w+', dtype=d.dtype,
+ shape=d.shape, version=None)
+ assert_(w[0].category is UserWarning)
+ ma[...] = d
+ del ma
+
+ ma = format.open_memmap(tf, mode='r')
+ assert_array_equal(ma, d)
+
+
+def test_write_version():
f = BytesIO()
arr = np.arange(1)
# These should pass.
format.write_array(f, arr, version=(1, 0))
format.write_array(f, arr)
+ format.write_array(f, arr, version=None)
+ format.write_array(f, arr)
+
+ format.write_array(f, arr, version=(2, 0))
+ format.write_array(f, arr)
+
# These should all fail.
bad_versions = [
(1, 1),
(0, 0),
(0, 1),
- (2, 0),
(2, 2),
(255, 255),
]