From 340cf9875b911ba858fe6e99147ed29f5e7f0df3 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 7 May 2019 00:39:56 -0700 Subject: MAINT: Simplify computation of npy format headers This works towards adding a third format that supports utf8 --- numpy/lib/format.py | 101 +++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 49 deletions(-) (limited to 'numpy/lib/format.py') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 553c9371d..271bc4a19 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -173,6 +173,11 @@ BUFFER_SIZE = 2**18 # size of buffer for reading npz files in bytes # difference between version 1.0 and 2.0 is a 4 byte (I) header length # instead of 2 bytes (H) allowing storage of large structured arrays +_header_size_formats = { + (1, 0): '= 1.9", UserWarning, stacklevel=2) + return header + + def _write_array_header(fp, d, version=None): """ Write the header for an array and returns the version used @@ -335,48 +379,19 @@ def _write_array_header(fp, d, version=None): None means use oldest that works explicit version will raise a ValueError if the format does not allow saving this data. Default: None - Returns - ------- - version : tuple of int - the file version which needs to be used to store the data """ - import struct header = ["{"] for key, value in sorted(d.items()): # Need to use repr here, since we eval these when reading header.append("'%s': %s, " % (key, repr(value))) header.append("}") header = "".join(header) - header = asbytes(_filter_header(header)) - - hlen = len(header) + 1 # 1 for newline - padlen_v1 = ARRAY_ALIGN - ((MAGIC_LEN + struct.calcsize('= 1.9", UserWarning, stacklevel=2) + _write_array_header(fp, header_data_from_array_1_0(array), version) if array.itemsize == 0: buffersize = 0 @@ -811,11 +818,7 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None, # If we got here, then it should be safe to create the file. fp = open(os_fspath(filename), mode+'b') try: - used_ver = _write_array_header(fp, d, version) - # this warning can be removed when 1.9 has aged enough - if version != (2, 0) and used_ver == (2, 0): - warnings.warn("Stored array in format 2.0. It can only be" - "read by NumPy >= 1.9", UserWarning, stacklevel=2) + _write_array_header(fp, d, version) offset = fp.tell() finally: fp.close() -- cgit v1.2.1