Takes a stringified header, and attaches the prefix and padding to it
(header, version)
| 364 | |
| 365 | |
| 366 | def _wrap_header(header, version): |
| 367 | """ |
| 368 | Takes a stringified header, and attaches the prefix and padding to it |
| 369 | """ |
| 370 | import struct |
| 371 | assert version is not None |
| 372 | fmt, encoding = _header_size_info[version] |
| 373 | header = header.encode(encoding) |
| 374 | hlen = len(header) + 1 |
| 375 | padlen = ARRAY_ALIGN - ((MAGIC_LEN + struct.calcsize(fmt) + hlen) % ARRAY_ALIGN) |
| 376 | try: |
| 377 | header_prefix = magic(*version) + struct.pack(fmt, hlen + padlen) |
| 378 | except struct.error: |
| 379 | msg = "Header length {} too big for version={}".format(hlen, version) |
| 380 | raise ValueError(msg) from None |
| 381 | |
| 382 | # Pad the header with spaces and a final newline such that the magic |
| 383 | # string, the header-length short and the header are aligned on a |
| 384 | # ARRAY_ALIGN byte boundary. This supports memory mapping of dtypes |
| 385 | # aligned up to ARRAY_ALIGN on systems like Linux where mmap() |
| 386 | # offset must be page-aligned (i.e. the beginning of the file). |
| 387 | return header_prefix + header + b' '*padlen + b'\n' |
| 388 | |
| 389 | |
| 390 | def _wrap_header_guess_version(header): |
no test coverage detected