示例#1
0
    def test_add_data_field(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Old {
                x @0 :Int64;
                y @1 :Int64;
            }

            struct New {
                x @0 :Int64;
                y @1 :Int64;
                z @2 :Int64 = 42;
            }
        """
        mod = self.compile(schema)
        # 1. read an old object with a newer schema
        s = dumps(mod.Old(x=1, y=2))
        obj = loads(s, mod.New)
        assert obj.x == 1
        assert obj.y == 2
        assert obj.z == 42
        #
        # 2. read a new object with an older schema
        s = dumps(mod.New(x=1, y=2, z=3))
        obj = loads(s, mod.Old)
        assert obj.x == 1
        assert obj.y == 2
        assert obj._data_size == 3
        py.test.raises(AttributeError, "obj.z")
示例#2
0
    def test_add_ptr_field(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Point {
                x @0 :Int64;
                y @1 :Int64;
            }

            struct Old {
                p1 @0 :Point;
            }

            struct New {
                p1 @0 :Point;
                p2 @1 :Point;
            }
        """
        mod = self.compile(schema)
        # 1. read an old object with a newer schema
        s = dumps(mod.Old(p1=mod.Point(x=1, y=2)))
        obj = loads(s, mod.New)
        assert obj.p1.x == 1
        assert obj.p1.y == 2
        assert obj.p2 is None
        # 2. read a new object with an older schema
        s = dumps(mod.New(p1=mod.Point(x=1, y=2), p2=mod.Point(x=3, y=4)))
        obj = loads(s, mod.Old)
        assert obj.p1.x == 1
        assert obj.p1.y == 2
        assert obj._data_size == 0
        assert obj._ptrs_size == 2
        py.test.raises(AttributeError, "obj.p2")
示例#3
0
def test_wrong_size():
    buf = b('\x00\x00\x00\x00\x04\x00\x00\x00'   # message header: 1 segment, size 4 words
            '\x00\x00\x00\x00\x02\x00\x01\x00'   # ptr to payload (Point {x, y})
            '\x01\x00\x00\x00\x00\x00\x00\x00'   # x == 1
            '\x02\x00\x00\x00\x00\x00\x00\x00')  # y == 2
    with pytest.raises(ValueError) as exc:
        loads(buf, Struct)
    assert str(exc.value) == ("Unexpected EOF: expected 32 bytes, got only 24. "
                              "Segment size: 4")
示例#4
0
def test_wrong_size_multiple_segments():
    buf = b('\x01\x00\x00\x00\x04\x00\x00\x00'   # message header: 2 segments: (4, 5)
            '\x05\x00\x00\x00\x00\x00\x00\x00'
            '\x00\x00\x00\x00\x02\x00\x01\x00'   # ptr to payload (Point {x, y})
            '\x01\x00\x00\x00\x00\x00\x00\x00'   # x == 1
            '\x02\x00\x00\x00\x00\x00\x00\x00')  # y == 2
    with pytest.raises(ValueError) as exc:
        loads(buf, Struct)
    assert str(exc.value) == ("Unexpected EOF: expected 72 bytes, got only 24. "
                              "Segments size: [4, 5]")
示例#5
0
 def generate_py_source(self, filename, convert_case, pyx):
     pyx = self.getpyx(pyx)
     data = self._capnp_compile(filename)
     request = loads(data, schema.CodeGeneratorRequest)
     m = ModuleGenerator(request, convert_case, pyx, self.standalone)
     src = m.generate()
     return m, py.code.Source(src)
示例#6
0
def test_truncated_header_2():
    buf = b('\x03\x00\x00\x00'  # 3+1 segments, but only two are specified
            '\x10\x00\x00\x00'  # size0: 16
            '\x20\x00\x00\x00') # size1: 32
    with pytest.raises(ValueError) as exc:
        p = loads(buf, Struct)
    assert str(exc.value) == 'Unexpected EOF when reading the header'
示例#7
0
def test_loads_not_whole_string():
    buf = b('\x00\x00\x00\x00\x03\x00\x00\x00'   # message header: 1 segment, size 3 words
            '\x00\x00\x00\x00\x02\x00\x00\x00'   # ptr to payload (Point {x, y})
            '\x01\x00\x00\x00\x00\x00\x00\x00'   # x == 1
            '\x02\x00\x00\x00\x00\x00\x00\x00'   # y == 2
            'garbage0')
    with pytest.raises(ValueError) as exc:
        p = loads(buf, Struct)
    assert str(exc.value) == 'Not all bytes were consumed: 8 bytes left'
示例#8
0
def test_loads():
    buf = b('\x00\x00\x00\x00\x03\x00\x00\x00'   # message header: 1 segment, size 3 words
            '\x00\x00\x00\x00\x02\x00\x00\x00'   # ptr to payload (Point {x, y})
            '\x01\x00\x00\x00\x00\x00\x00\x00'   # x == 1
            '\x02\x00\x00\x00\x00\x00\x00\x00')  # y == 2

    p = loads(buf, Struct)
    assert isinstance(p, Struct)
    assert p._read_primitive(0, Types.int64.ifmt) == 1
    assert p._read_primitive(8, Types.int64.ifmt) == 2
示例#9
0
 def _parse_schema_file(self, filename):
     data = self._capnp_compile(filename)
     request = loads(data, schema.CodeGeneratorRequest)
     return request
示例#10
0
def test_huge_number_of_segments():
    buf = b'hello' # this corresponds to 1819043177 segments
    with pytest.raises(ValueError) as exc:
        loads(buf, Struct)
    assert str(exc.value) == 'Unexpected EOF when reading the header'
示例#11
0
def test_truncated_header_1():
    buf = b('\x03\x00\x00')  # we expect at least 4 bytes
    with pytest.raises(ValueError) as exc:
        p = loads(buf, Struct)
    assert str(exc.value) == 'Malformed header: expected 4 bytes, got 3'
示例#12
0
def test_eof():
    buf = b''
    with pytest.raises(EOFError):
        loads(buf, Struct)