Esempio n. 1
0
def test_read_str():
    buf = ('garbage0' 'hello capnproto\0')  # string
    p = ptr.new_list(0, ptr.LIST_SIZE_8, 16)
    b = Segment(buf)
    s = b.read_str(p, 0, "", additional_size=-1)
    assert s == "hello capnproto"
    s = b.read_str(p, 0, "", additional_size=0)
    assert s == "hello capnproto\0"
Esempio n. 2
0
def test_hash_str():
    buf = ('garbage0' 'hello capnproto\0')  # string
    p = ptr.new_list(0, ptr.LIST_SIZE_8, 16)
    b = Segment(buf)
    h = b.hash_str(p, 0, 0, additional_size=-1)
    assert h == hash("hello capnproto")
    h = b.hash_str(p, 0, 0, additional_size=0)
    assert h == hash("hello capnproto\0")
Esempio n. 3
0
 def test_write_slice(self):
     src = Segment(b'1234foobar1234')
     buf = SegmentBuilder()
     buf.allocate(8)
     pos = buf.allocate(8)
     buf.write_slice(pos, src, start=4, n=6)
     s = buf.as_string()
     assert s == b('\x00\x00\x00\x00\x00\x00\x00\x00'
                   'foobar\x00\x00')
Esempio n. 4
0
def test_Segment_pickle():
    import cPickle as pickle
    buf = Segment('hello')
    #
    buf2 = pickle.loads(pickle.dumps(buf))
    assert buf2.buf == 'hello'
    #
    buf2 = pickle.loads(pickle.dumps(buf, pickle.HIGHEST_PROTOCOL))
    assert buf2.buf == 'hello'
Esempio n. 5
0
 def copy_struct(self, src, offset, data_size, ptrs_size, bufsize=None):
     src_seg = Segment(src)
     if bufsize is None:
         bufsize = len(src)+8
     dst = SegmentBuilder(bufsize)
     dst_pos = dst.allocate(8) # allocate the space to store the pointer p
     p = ptr.new_struct(0, data_size, ptrs_size)
     dst.copy_from_pointer(dst_pos, src_seg, p, offset-8)
     return dst.as_string()
Esempio n. 6
0
 def is_compact(self, buf, offset, kind, **kwds):
     buf = Segment(buf)
     if kind == ptr.STRUCT:
         p = ptr.new_struct(0, **kwds)
     elif kind == ptr.LIST:
         p = ptr.new_list(0, **kwds)
     else:
         assert False
     return is_compact(buf, p, offset - 8)
Esempio n. 7
0
def _load_buffer_single_segment(f):
    # fast path for the single-segment case. In this scenario, we don't
    # even need to compute the padding as we know that we read exactly 4+4
    # bytes
    buf = f.read(4)
    #
    # The cost of the len(buf) checks is ~7-8% on CPython, but I don't know
    # how to avoid it. However, it's negligible on PyPy
    if len(buf) < 4:
        raise ValueError("Unexpected EOF when reading the header")
    message_size = unpack_uint32(buf, 0)
    message_lenght = message_size * 8
    buf = f.read(message_lenght)
    if len(buf) < message_lenght:
        raise ValueError("Unexpected EOF: expected %d bytes, got only %s. " 
                         "Segment size: %s" % (message_lenght, len(buf), message_size))
    return Segment(buf)
Esempio n. 8
0
 def _raw_loads(cls, s):
     """
     Load an object which was saved by _raw_dumps
     """
     args = marshal.loads(s)
     header, clsname, buf, segment_offsets, offset, data_size, ptrs_size = args
     assert header == 'capnpy raw dump'
     if clsname != cls.__name__:
         warnings.warn("The raw dump says it's a %s, but we are loading it "
                       "as a %s" % (clsname, cls.__name__))
     #
     if segment_offsets is None:
         seg = Segment(buf)
     else:
         seg = MultiSegment(buf, segment_offsets)
     self = cls.__new__(cls)
     self._init_from_buffer(seg, offset, data_size, ptrs_size)
     return self
Esempio n. 9
0
def test_hash_str_exception():
    buf = ''
    p = ptr.new_struct(0, 1, 1)  # this is the wrong type of pointer
    b = Segment(buf)
    py.test.raises(AssertionError, "b.hash_str(p, 0, 0, 0)")
Esempio n. 10
0
 def end_of(self, buf, offset, data_size, ptrs_size):
     buf = Segment(buf)
     p = ptr.new_struct(0, data_size, ptrs_size)
     return end_of(buf, p, offset - 8)
Esempio n. 11
0
def test_hash_str_exception():
    buf = b''
    p = ptr.new_struct(0, 1, 1) # this is the wrong type of pointer
    bb = Segment(buf)
    with pytest.raises(AssertionError):
        bb.hash_str(p, 0, 0, 0)
Esempio n. 12
0
 def _init_blob(self, seg):
     assert seg is not None
     if isinstance(seg, str):
         seg = Segment(seg)
     self._seg = seg
Esempio n. 13
0
 def endof(self, seg, offset, data_size, ptrs_size):
     if isinstance(seg, bytes):
         seg = Segment(seg)
     p = ptr.new_struct(0, data_size, ptrs_size)
     return endof(seg, p, offset - 8)
Esempio n. 14
0
 def copy_struct(self, src, offset, data_size, ptrs_size, bufsize=None):
     src_seg = Segment(src)
     if bufsize is None:
         bufsize = len(src) + 8
     return self.copy_struct_segment(src_seg, offset, data_size, ptrs_size,
                                     bufsize)