예제 #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_dumps_not_compact():
    class Person(Struct):
        pass

    buf = b('\x20\x00\x00\x00\x00\x00\x00\x00'  # age=32
            '\x05\x00\x00\x00\x2a\x00\x00\x00'  # name=ptr
            'garbage1'
            'J'
            'o'
            'h'
            'n'
            '\x00\x00\x00\x00')  # John

    p = Person.from_buffer(buf, 0, data_size=1, ptrs_size=1)
    msg = dumps(p)
    exp = b(
        '\x00\x00\x00\x00\x04\x00\x00\x00'  # message header: 1 segment, size 3 words
        '\x00\x00\x00\x00\x01\x00\x01\x00'  # ptr to payload
        '\x20\x00\x00\x00\x00\x00\x00\x00'  # age=32
        '\x01\x00\x00\x00\x2a\x00\x00\x00'  # name=ptr
        'J'
        'o'
        'h'
        'n'
        '\x00\x00\x00\x00')  # John
    assert msg == exp
예제 #4
0
 def decode(self, obj):
     from capnpy.message import dumps
     from subprocess import Popen, PIPE
     cmd = ['capnp', 'decode', '--short', self.mod.__schema__, obj.__class__.__name__]
     proc = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
     proc.stdin.write(dumps(obj))
     stdout, stderr = proc.communicate()
     ret = proc.wait()
     if ret != 0:
         raise ValueError(stderr)
     return ensure_unicode(stdout.strip())
예제 #5
0
def test_dumps():
    class Point(Struct):
        pass
    
    buf = b('\x01\x00\x00\x00\x00\x00\x00\x00'   # x == 1
            '\x02\x00\x00\x00\x00\x00\x00\x00')  # y == 2
    p = Point.from_buffer(buf, 0, data_size=2, ptrs_size=0)
    msg = dumps(p)
    exp = 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
    assert msg == exp