def from_file(cls, fp, size=None):
"""Construct a Frame Descriptor object from binary data.
Parameters
----------
fp : file-like
File-like object to read the Frame Descriptor from.
The cursor must be at the position where the Frame Descriptor starts.
size : int, optional
the number of bytes to be read.
If not given, the value inspected from the fp itself will be used.
Returns
-------
FrameDescriptor
.. note:: A number of bytes equal to `size` will be consumed from the fp.
.. warning:
If the number of requested bytes is smaller than the size of the
Frame Descriptor, some fields might be left unfilled.
"""
out = FrameDescriptor()
if size is None:
size = np.fromfile(fp, dtype=np.uint32, count=1)
if size.size == 0:
raise ValueError('Unexpected EOF')
fp.seek(-size.nbytes, io.SEEK_CUR)
size = size[0]
if size > ct.sizeof(FrameDescriptor):
raise ValueError('Number of requested bytes (%d) do not fit '
'in the data structure (%d)' % (size, ct.sizeof(out)))
buf = fp.read(size)
ct.memmove(ct.addressof(out), buf, size)
return out
评论列表
文章目录