コード例 #1
0
    def test_raise_from_object_hook(self):
        def hook(obj):
            raise DummyException

        self.assertRaises(DummyException, unpackb, packb({}), object_hook=hook)
        self.assertRaises(DummyException,
                          unpackb,
                          packb({'fizz': 'buzz'}),
                          object_hook=hook)
        self.assertRaises(DummyException,
                          unpackb,
                          packb({'fizz': 'buzz'}),
                          object_pairs_hook=hook)
        self.assertRaises(DummyException,
                          unpackb,
                          packb({'fizz': {
                              'buzz': 'spam'
                          }}),
                          object_hook=hook)
        self.assertRaises(DummyException,
                          unpackb,
                          packb({'fizz': {
                              'buzz': 'spam'
                          }}),
                          object_pairs_hook=hook)
コード例 #2
0
 def test_odict(self):
     seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
     od = OrderedDict(seq)
     assert unpackb(packb(od), use_list=1) == dict(seq)
     def pair_hook(seq):
         return list(seq)
     assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
コード例 #3
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
 def test_integer(self):
     x = -(2 ** 63)
     assert unpackb(packb(x)) == x
     self.assertRaises((OverflowError, ValueError), packb, x - 1)
     x = 2 ** 64 - 1
     assert unpackb(packb(x)) == x
     self.assertRaises((OverflowError, ValueError), packb, x + 1)
コード例 #4
0
 def test_integer(self):
     x = -(2**63)
     assert unpackb(packb(x)) == x
     self.assertRaises((OverflowError, ValueError), packb, x - 1)
     x = 2**64 - 1
     assert unpackb(packb(x)) == x
     self.assertRaises((OverflowError, ValueError), packb, x + 1)
コード例 #5
0
ファイル: test_except.py プロジェクト: 5i7788/pandas
 def test_raise_from_object_hook(self):
     def hook(obj):
         raise DummyException
     self.assertRaises(DummyException, unpackb, packb({}), object_hook=hook)
     self.assertRaises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_hook=hook)
     self.assertRaises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_pairs_hook=hook)
     self.assertRaises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_hook=hook)
     self.assertRaises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
コード例 #6
0
    def test_odict(self):
        seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
        od = OrderedDict(seq)
        assert unpackb(packb(od), use_list=1) == dict(seq)

        def pair_hook(seq):
            return list(seq)

        assert unpackb(packb(od), object_pairs_hook=pair_hook,
                       use_list=1) == seq
コード例 #7
0
ファイル: test_newspec.py プロジェクト: 8ballbb/ProjectRothar
def test_str8():
    header = b'\xd9'
    data = b'x' * 32
    b = packb(data.decode(), use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\x20'
    assert b[2:] == data
    assert unpackb(b) == data

    data = b'x' * 255
    b = packb(data.decode(), use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b) == data
コード例 #8
0
ファイル: test_newspec.py プロジェクト: 8ballbb/ProjectRothar
def test_bin8():
    header = b'\xc4'
    data = b''
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\x00'
    assert b[2:] == data
    assert unpackb(b) == data

    data = b'x' * 255
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b) == data
コード例 #9
0
ファイル: test_newspec.py プロジェクト: BRGM/Pic-EAU
def test_bin8():
    header = b'\xc4'
    data = b''
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\x00'
    assert b[2:] == data
    assert unpackb(b) == data

    data = b'x' * 255
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b) == data
コード例 #10
0
ファイル: test_newspec.py プロジェクト: BRGM/Pic-EAU
def test_str8():
    header = b'\xd9'
    data = b'x' * 32
    b = packb(data.decode(), use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\x20'
    assert b[2:] == data
    assert unpackb(b) == data

    data = b'x' * 255
    b = packb(data.decode(), use_bin_type=True)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b) == data
コード例 #11
0
 def test_decode_pairs_hook(self):
     packed = packb([3, {1: 2, 3: 4}])
     prod_sum = 1 * 2 + 3 * 4
     unpacked = unpackb(
         packed, object_pairs_hook=lambda l: sum(k * v for k, v in l),
         use_list=1)
     assert unpacked[1] == prod_sum
コード例 #12
0
ファイル: numpy.py プロジェクト: CaptainAL/Spyder
def serialize(x):
    if x.dtype == 'O':
        with ignoring(Exception):  # Try msgpack (faster on strings)
            return frame(msgpack.packb(x.tolist()))
        return frame(pickle.dumps(x.tolist(), protocol=pickle.HIGHEST_PROTOCOL))
    else:
        return x.tobytes()
コード例 #13
0
 def testIgnoreErrorsPack(self):
     re = unpackb(packb(compat.u("abcФФФdef"),
                        encoding='ascii',
                        unicode_errors='ignore'),
                  encoding='utf-8',
                  use_list=1)
     assert re == compat.u("abcdef")
コード例 #14
0
def test_incorrect_type_array():
    unpacker = Unpacker()
    unpacker.feed(packb(1))
    try:
        unpacker.read_array_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #15
0
def test_correct_type_nested_array():
    unpacker = Unpacker()
    unpacker.feed(packb({'a': ['b', 'c', 'd']}))
    try:
        unpacker.read_array_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #16
0
ファイル: numpy.py プロジェクト: mindw/partd
def serialize(x):
    if x.dtype == 'O':
        with ignoring(Exception):  # Try msgpack (faster on strings)
            return frame(msgpack.packb(x.tolist()))
        return frame(pickle.dumps(x.tolist(),
                                  protocol=pickle.HIGHEST_PROTOCOL))
    else:
        return x.tobytes()
コード例 #17
0
def test_incorrect_type_nested_map():
    unpacker = Unpacker()
    unpacker.feed(packb([{'a': 'b'}]))
    try:
        unpacker.read_map_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #18
0
ファイル: numpy.py プロジェクト: dask/partd
def serialize(x):
    if x.dtype == 'O':
        l = x.flatten().tolist()
        with ignoring(Exception):  # Try msgpack (faster on strings)
            return frame(msgpack.packb(l, use_bin_type=True))
        return frame(pickle.dumps(l, protocol=pickle.HIGHEST_PROTOCOL))
    else:
        return x.tobytes()
コード例 #19
0
def serialize(x):
    if x.dtype == 'O':
        l = x.flatten().tolist()
        with ignoring(Exception):  # Try msgpack (faster on strings)
            return frame(msgpack.packb(l, use_bin_type=True))
        return frame(pickle.dumps(l, protocol=pickle.HIGHEST_PROTOCOL))
    else:
        return x.tobytes()
コード例 #20
0
ファイル: test_read_size.py プロジェクト: 5i7788/pandas
def test_correct_type_nested_array():
    unpacker = Unpacker()
    unpacker.feed(packb({'a': ['b', 'c', 'd']}))
    try:
        unpacker.read_array_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #21
0
ファイル: test_read_size.py プロジェクト: 5i7788/pandas
def test_incorrect_type_nested_map():
    unpacker = Unpacker()
    unpacker.feed(packb([{'a': 'b'}]))
    try:
        unpacker.read_map_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #22
0
ファイル: test_newspec.py プロジェクト: 8ballbb/ProjectRothar
def test_bin16():
    header = b'\xc5'
    data = b'x' * 256
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 3
    assert b[0:1] == header
    assert b[1:3] == b'\x01\x00'
    assert b[3:] == data
    assert unpackb(b) == data

    data = b'x' * 65535
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 3
    assert b[0:1] == header
    assert b[1:3] == b'\xff\xff'
    assert b[3:] == data
    assert unpackb(b) == data
コード例 #23
0
ファイル: test_newspec.py プロジェクト: BRGM/Pic-EAU
def test_bin16():
    header = b'\xc5'
    data = b'x' * 256
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 3
    assert b[0:1] == header
    assert b[1:3] == b'\x01\x00'
    assert b[3:] == data
    assert unpackb(b) == data

    data = b'x' * 65535
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 3
    assert b[0:1] == header
    assert b[1:3] == b'\xff\xff'
    assert b[3:] == data
    assert unpackb(b) == data
コード例 #24
0
ファイル: test_read_size.py プロジェクト: 5i7788/pandas
def test_incorrect_type_array():
    unpacker = Unpacker()
    unpacker.feed(packb(1))
    try:
        unpacker.read_array_header()
        assert 0, 'should raise exception'
    except UnexpectedTypeException:
        assert 1, 'okay'
コード例 #25
0
ファイル: test_unpack.py プロジェクト: BRGM/Pic-EAU
 def test_unpack_array_header_from_file(self):
     f = BytesIO(packb([1, 2, 3, 4]))
     unpacker = Unpacker(f)
     assert unpacker.read_array_header() == 4
     assert unpacker.unpack() == 1
     assert unpacker.unpack() == 2
     assert unpacker.unpack() == 3
     assert unpacker.unpack() == 4
     self.assertRaises(OutOfData, unpacker.unpack)
コード例 #26
0
ファイル: test_unpack.py プロジェクト: BrenBarn/pandas
 def test_unpack_array_header_from_file(self):
     f = BytesIO(packb([1,2,3,4]))
     unpacker = Unpacker(f)
     assert unpacker.read_array_header() == 4
     assert unpacker.unpack() == 1
     assert unpacker.unpack() == 2
     assert unpacker.unpack() == 3
     assert unpacker.unpack() == 4
     self.assertRaises(OutOfData, unpacker.unpack)
コード例 #27
0
ファイル: test_newspec.py プロジェクト: 8ballbb/ProjectRothar
def test_bin32():
    header = b'\xc6'
    data = b'x' * 65536
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 5
    assert b[0:1] == header
    assert b[1:5] == b'\x00\x01\x00\x00'
    assert b[5:] == data
    assert unpackb(b) == data
コード例 #28
0
ファイル: test_newspec.py プロジェクト: BRGM/Pic-EAU
def test_bin32():
    header = b'\xc6'
    data = b'x' * 65536
    b = packb(data, use_bin_type=True)
    assert len(b) == len(data) + 5
    assert b[0:1] == header
    assert b[1:5] == b'\x00\x01\x00\x00'
    assert b[5:] == data
    assert unpackb(b) == data
コード例 #29
0
def test_write_bytes_multi_buffer():
    long_val = (5) * 100
    expected = packb(long_val)
    unpacker = Unpacker(io.BytesIO(expected), read_size=3, max_buffer_size=3)

    f = io.BytesIO()
    unpacked = unpacker.unpack(f.write)
    assert unpacked == long_val
    assert f.getvalue() == expected
コード例 #30
0
ファイル: test_unpack.py プロジェクト: BRGM/Pic-EAU
    def test_unpacker_ext_hook(self):
        class MyUnpacker(Unpacker):
            def __init__(self):
                super(MyUnpacker, self).__init__(ext_hook=self._hook,
                                                 encoding='utf-8')

            def _hook(self, code, data):
                if code == 1:
                    return int(data)
                else:
                    return ExtType(code, data)

        unpacker = MyUnpacker()
        unpacker.feed(packb({'a': 1}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': 1}
        unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': 123}
        unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': ExtType(2, b'321')}
コード例 #31
0
 def testPackUTF32(self):
     test_data = [
         compat.u(""),
         compat.u("abcd"),
         [compat.u("defgh")],
         compat.u("Русский текст"),
         ]
     for td in test_data:
         re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
         assert re == td
コード例 #32
0
    def test_max_bin_len(self):
        d = b'x' * 3
        packed = packb(d, use_bin_type=True)

        unpacker = Unpacker(max_bin_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_bin_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #33
0
    def test_max_array_len(self):
        d = [1, 2, 3]
        packed = packb(d)

        unpacker = Unpacker(max_array_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_array_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #34
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
    def test_max_ext_len(self):
        d = ExtType(42, b"abc")
        packed = packb(d)

        unpacker = Unpacker(max_ext_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_ext_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #35
0
ファイル: test_read_size.py プロジェクト: 5i7788/pandas
def test_read_map_header():
    unpacker = Unpacker()
    unpacker.feed(packb({'a': 'A'}))
    assert unpacker.read_map_header() == 1
    assert unpacker.unpack() == B'a'
    assert unpacker.unpack() == B'A'
    try:
        unpacker.unpack()
        assert 0, 'should raise exception'
    except OutOfData:
        assert 1, 'okay'
コード例 #36
0
    def test_max_ext_len(self):
        d = ExtType(42, b"abc")
        packed = packb(d)

        unpacker = Unpacker(max_ext_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_ext_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #37
0
 def testPackUnicode(self):
     test_data = [u(""), u("abcd"), [u("defgh")], u("Русский текст"), ]
     for td in test_data:
         re = unpackb(
             packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
         assert re == td
         packer = Packer(encoding='utf-8')
         data = packer.pack(td)
         re = Unpacker(
             compat.BytesIO(data), encoding='utf-8', use_list=1).unpack()
         assert re == td
コード例 #38
0
ファイル: test_unpack.py プロジェクト: 8ballbb/ProjectRothar
    def test_unpacker_ext_hook(self):
        class MyUnpacker(Unpacker):

            def __init__(self):
                super(MyUnpacker, self).__init__(ext_hook=self._hook,
                                                 encoding='utf-8')

            def _hook(self, code, data):
                if code == 1:
                    return int(data)
                else:
                    return ExtType(code, data)

        unpacker = MyUnpacker()
        unpacker.feed(packb({'a': 1}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': 1}
        unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': 123}
        unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
        assert unpacker.unpack() == {'a': ExtType(2, b'321')}
コード例 #39
0
    def test_max_str_len(self):
        d = 'x' * 3
        packed = packb(d)

        unpacker = Unpacker(max_str_len=3, encoding='utf-8')
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_str_len=2, encoding='utf-8')
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #40
0
    def test_max_map_len(self):
        d = {1: 2, 3: 4, 5: 6}
        packed = packb(d)

        unpacker = Unpacker(max_map_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_map_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #41
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
    def test_max_map_len(self):
        d = {1: 2, 3: 4, 5: 6}
        packed = packb(d)

        unpacker = Unpacker(max_map_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_map_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #42
0
 def testPackUTF32(self):
     test_data = [
         compat.u(""),
         compat.u("abcd"),
         [compat.u("defgh")],
         compat.u("Русский текст"),
     ]
     for td in test_data:
         re = unpackb(
             packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
         assert re == td
コード例 #43
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
    def test_max_array_len(self):
        d = [1, 2, 3]
        packed = packb(d)

        unpacker = Unpacker(max_array_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_array_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #44
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
    def test_max_bin_len(self):
        d = b'x' * 3
        packed = packb(d, use_bin_type=True)

        unpacker = Unpacker(max_bin_len=3)
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_bin_len=2)
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #45
0
ファイル: test_limits.py プロジェクト: cgrin/pandas
    def test_max_str_len(self):
        d = 'x' * 3
        packed = packb(d)

        unpacker = Unpacker(max_str_len=3, encoding='utf-8')
        unpacker.feed(packed)
        assert unpacker.unpack() == d

        unpacker = Unpacker(max_str_len=2, encoding='utf-8')
        unpacker.feed(packed)
        self.assertRaises(ValueError, unpacker.unpack)
コード例 #46
0
def test_read_map_header():
    unpacker = Unpacker()
    unpacker.feed(packb({'a': 'A'}))
    assert unpacker.read_map_header() == 1
    assert unpacker.unpack() == B'a'
    assert unpacker.unpack() == B'A'
    try:
        unpacker.unpack()
        assert 0, 'should raise exception'
    except OutOfData:
        assert 1, 'okay'
コード例 #47
0
ファイル: test_read_size.py プロジェクト: 5i7788/pandas
def test_read_array_header():
    unpacker = Unpacker()
    unpacker.feed(packb(['a', 'b', 'c']))
    assert unpacker.read_array_header() == 3
    assert unpacker.unpack() == b'a'
    assert unpacker.unpack() == b'b'
    assert unpacker.unpack() == b'c'
    try:
        unpacker.unpack()
        assert 0, 'should raise exception'
    except OutOfData:
        assert 1, 'okay'
コード例 #48
0
def test_read_array_header():
    unpacker = Unpacker()
    unpacker.feed(packb(['a', 'b', 'c']))
    assert unpacker.read_array_header() == 3
    assert unpacker.unpack() == b'a'
    assert unpacker.unpack() == b'b'
    assert unpacker.unpack() == b'c'
    try:
        unpacker.unpack()
        assert 0, 'should raise exception'
    except OutOfData:
        assert 1, 'okay'
コード例 #49
0
ファイル: test_unpack.py プロジェクト: BrenBarn/pandas
    def test_unpacker_hook_refcnt(self):
        if not hasattr(sys, 'getrefcount'):
            raise nose.SkipTest('no sys.getrefcount()')
        result = []

        def hook(x):
            result.append(x)
            return x

        basecnt = sys.getrefcount(hook)

        up = Unpacker(object_hook=hook, list_hook=hook)

        assert sys.getrefcount(hook) >= basecnt + 2

        up.feed(packb([{}]))
        up.feed(packb([{}]))
        assert up.unpack() == [{}]
        assert up.unpack() == [{}]
        assert result == [{}, [{}], {}, [{}]]

        del up

        assert sys.getrefcount(hook) == basecnt
コード例 #50
0
ファイル: test_unpack.py プロジェクト: BRGM/Pic-EAU
    def test_unpacker_hook_refcnt(self):
        if not hasattr(sys, 'getrefcount'):
            raise nose.SkipTest('no sys.getrefcount()')
        result = []

        def hook(x):
            result.append(x)
            return x

        basecnt = sys.getrefcount(hook)

        up = Unpacker(object_hook=hook, list_hook=hook)

        assert sys.getrefcount(hook) >= basecnt + 2

        up.feed(packb([{}]))
        up.feed(packb([{}]))
        assert up.unpack() == [{}]
        assert up.unpack() == [{}]
        assert result == [{}, [{}], {}, [{}]]

        del up

        assert sys.getrefcount(hook) == basecnt
コード例 #51
0
ファイル: core.py プロジェクト: esc/castra
def pack_file(x, fn, encoding='utf8'):
    """ Pack numpy array into filename

    Supports binary data with bloscpack and text data with msgpack+blosc

    >>> pack_file(np.array([1, 2, 3]), 'foo.blp')  # doctest: +SKIP

    See also:
        unpack_file
    """
    if x.dtype != 'O':
        bloscpack.pack_ndarray_file(x, fn)
    else:
        bytes = blosc.compress(msgpack.packb(x.tolist(), encoding=encoding), 1)
        with open(fn, 'wb') as f:
            f.write(bytes)
コード例 #52
0
ファイル: core.py プロジェクト: thequackdaddy/castra
def pack_file(x, fn, encoding='utf8'):
    """ Pack numpy array into filename

    Supports binary data with bloscpack and text data with msgpack+blosc

    >>> pack_file(np.array([1, 2, 3]), 'foo.blp')  # doctest: +SKIP

    See also:
        unpack_file
    """
    if x.dtype != 'O':
        bloscpack.pack_ndarray_file(x, fn, bloscpack_args=bp_args,
                blosc_args=blosc_args(x.dtype))
    else:
        bytes = blosc.compress(msgpack.packb(x.tolist(), encoding=encoding), 1)
        with open(fn, 'wb') as f:
            f.write(bytes)
コード例 #53
0
ファイル: test_extension.py プロジェクト: BrenBarn/pandas
def test_extension_type():
    def default(obj):
        print('default called', obj)
        if isinstance(obj, array.array):
            typecode = 123 # application specific typecode
            data = obj.tostring()
            return ExtType(typecode, data)
        raise TypeError("Unknwon type object %r" % (obj,))

    def ext_hook(code, data):
        print('ext_hook called', code, data)
        assert code == 123
        obj = array.array('d')
        obj.fromstring(data)
        return obj

    obj = [42, b'hello', array.array('d', [1.1, 2.2, 3.3])]
    s = msgpack.packb(obj, default=default)
    obj2 = msgpack.unpackb(s, ext_hook=ext_hook)
    assert obj == obj2
コード例 #54
0
def test_extension_type():
    def default(obj):
        print('default called', obj)
        if isinstance(obj, array.array):
            typecode = 123  # application specific typecode
            data = obj.tostring()
            return ExtType(typecode, data)
        raise TypeError("Unknwon type object %r" % (obj, ))

    def ext_hook(code, data):
        print('ext_hook called', code, data)
        assert code == 123
        obj = array.array('d')
        obj.fromstring(data)
        return obj

    obj = [42, b'hello', array.array('d', [1.1, 2.2, 3.3])]
    s = msgpack.packb(obj, default=default)
    obj2 = msgpack.unpackb(s, ext_hook=ext_hook)
    assert obj == obj2
コード例 #55
0
ファイル: python.py プロジェクト: dask/partd
def dumps(x):
    try:
        return msgpack.packb(x, use_bin_type=True)
    except:
        return pickle.dumps(x, protocol=pickle.HIGHEST_PROTOCOL)
コード例 #56
0
ファイル: test_case.py プロジェクト: 5i7788/pandas
def test_unicode():
    assert unpackb(packb('foobar'), use_list=1) == b'foobar'
コード例 #57
0
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, use_list=0) == obj