MCPcopy Create free account
hub / github.com/numpy/numpy / _wrap_header

Function _wrap_header

numpy/lib/format.py:366–387  ·  view source on GitHub ↗

Takes a stringified header, and attaches the prefix and padding to it

(header, version)

Source from the content-addressed store, hash-verified

364
365
366def _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
390def _wrap_header_guess_version(header):

Callers 2

_write_array_headerFunction · 0.85

Calls 2

magicFunction · 0.85
encodeMethod · 0.80

Tested by

no test coverage detected