예제 #1
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'
예제 #2
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'
예제 #3
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'
예제 #4
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
예제 #5
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)
예제 #6
0
 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)
예제 #7
0
    def testArraySize(self, sizes=[0, 5, 50, 1000]):
        bio = compat.BytesIO()
        packer = Packer()
        for size in sizes:
            bio.write(packer.pack_array_header(size))
            for i in range(size):
                bio.write(packer.pack(i))

        bio.seek(0)
        unpacker = Unpacker(bio, use_list=1)
        for size in sizes:
            assert unpacker.unpack() == list(range(size))
예제 #8
0
    def testArraySize(self, sizes=[0, 5, 50, 1000]):
        bio = compat.BytesIO()
        packer = Packer()
        for size in sizes:
            bio.write(packer.pack_array_header(size))
            for i in range(size):
                bio.write(packer.pack(i))

        bio.seek(0)
        unpacker = Unpacker(bio, use_list=1)
        for size in sizes:
            assert unpacker.unpack() == list(range(size))
예제 #9
0
    def testMapSize(self, sizes=[0, 5, 50, 1000]):
        bio = compat.BytesIO()
        packer = Packer()
        for size in sizes:
            bio.write(packer.pack_map_header(size))
            for i in range(size):
                bio.write(packer.pack(i))  # key
                bio.write(packer.pack(i * 2))  # value

        bio.seek(0)
        unpacker = Unpacker(bio)
        for size in sizes:
            assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
예제 #10
0
    def testMapSize(self, sizes=[0, 5, 50, 1000]):
        bio = compat.BytesIO()
        packer = Packer()
        for size in sizes:
            bio.write(packer.pack_map_header(size))
            for i in range(size):
                bio.write(packer.pack(i))  # key
                bio.write(packer.pack(i * 2))  # value

        bio.seek(0)
        unpacker = Unpacker(bio)
        for size in sizes:
            assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
예제 #11
0
    def test_readbytes(self):
        unpacker = Unpacker(read_size=3)
        unpacker.feed(b'foobar')
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.read_bytes(3) == b'oob'
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')

        # Test buffer refill
        unpacker = Unpacker(compat.BytesIO(b'foobar'), read_size=3)
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.read_bytes(3) == b'oob'
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')
예제 #12
0
    def test_manualreset(self, sizes=[0, 5, 50, 1000]):
        packer = Packer(autoreset=False)
        for size in sizes:
            packer.pack_array_header(size)
            for i in range(size):
                packer.pack(i)

        bio = compat.BytesIO(packer.bytes())
        unpacker = Unpacker(bio, use_list=1)
        for size in sizes:
            assert unpacker.unpack() == list(range(size))

        packer.reset()
        assert packer.bytes() == b''
예제 #13
0
    def test_readbytes(self):
        unpacker = Unpacker(read_size=3)
        unpacker.feed(b"foobar")
        assert unpacker.unpack() == ord(b"f")
        assert unpacker.read_bytes(3) == b"oob"
        assert unpacker.unpack() == ord(b"a")
        assert unpacker.unpack() == ord(b"r")

        # Test buffer refill
        unpacker = Unpacker(compat.BytesIO(b"foobar"), read_size=3)
        assert unpacker.unpack() == ord(b"f")
        assert unpacker.read_bytes(3) == b"oob"
        assert unpacker.unpack() == ord(b"a")
        assert unpacker.unpack() == ord(b"r")
예제 #14
0
    def test_manualreset(self, sizes=[0, 5, 50, 1000]):
        packer = Packer(autoreset=False)
        for size in sizes:
            packer.pack_array_header(size)
            for i in range(size):
                packer.pack(i)

        bio = compat.BytesIO(packer.bytes())
        unpacker = Unpacker(bio, use_list=1)
        for size in sizes:
            assert unpacker.unpack() == list(range(size))

        packer.reset()
        assert packer.bytes() == b''
예제 #15
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)
예제 #16
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)
예제 #17
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)
예제 #18
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)
예제 #19
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)
예제 #20
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'
예제 #21
0
 def test_issue124(self):
     unpacker = Unpacker()
     unpacker.feed(b'\xa1?\xa1!')
     assert tuple(unpacker) == (b'?', b'!')
     assert tuple(unpacker) == ()
     unpacker.feed(b"\xa1?\xa1")
     assert tuple(unpacker) == (b'?', )
     assert tuple(unpacker) == ()
     unpacker.feed(b"!")
     assert tuple(unpacker) == (b'!', )
     assert tuple(unpacker) == ()
예제 #22
0
 def test_maxbuffersize(self):
     self.assertRaises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
     unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
     unpacker.feed(b'fo')
     self.assertRaises(BufferFull, unpacker.feed, b'ob')
     unpacker.feed(b'o')
     assert ord('f') == next(unpacker)
     unpacker.feed(b'b')
     assert ord('o') == next(unpacker)
     assert ord('o') == next(unpacker)
     assert ord('b') == next(unpacker)
예제 #23
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'
예제 #24
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'
예제 #25
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
예제 #26
0
 def test_maxbuffersize(self):
     self.assertRaises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
     unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
     unpacker.feed(b'fo')
     self.assertRaises(BufferFull, unpacker.feed, b'ob')
     unpacker.feed(b'o')
     assert ord('f') == next(unpacker)
     unpacker.feed(b'b')
     assert ord('o') == next(unpacker)
     assert ord('o') == next(unpacker)
     assert ord('b') == next(unpacker)
예제 #27
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'
예제 #28
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)
예제 #29
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)
예제 #30
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)
예제 #31
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)
예제 #32
0
 def test_maxbuffersize(self):
     self.assertRaises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
     unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
     unpacker.feed(b"fo")
     self.assertRaises(BufferFull, unpacker.feed, b"ob")
     unpacker.feed(b"o")
     assert ord("f") == next(unpacker)
     unpacker.feed(b"b")
     assert ord("o") == next(unpacker)
     assert ord("o") == next(unpacker)
     assert ord("b") == next(unpacker)
예제 #33
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)
예제 #34
0
 def test_issue124(self):
     unpacker = Unpacker()
     unpacker.feed(b'\xa1?\xa1!')
     assert tuple(unpacker) == (b'?', b'!')
     assert tuple(unpacker) == ()
     unpacker.feed(b"\xa1?\xa1")
     assert tuple(unpacker) == (b'?', )
     assert tuple(unpacker) == ()
     unpacker.feed(b"!")
     assert tuple(unpacker) == (b'!', )
     assert tuple(unpacker) == ()
예제 #35
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'
예제 #36
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'
예제 #37
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'
예제 #38
0
    def test_foobar(self):
        unpacker = Unpacker(read_size=3, use_list=1)
        unpacker.feed(b'foobar')
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.unpack() == ord(b'o')
        assert unpacker.unpack() == ord(b'o')
        assert unpacker.unpack() == ord(b'b')
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')
        self.assertRaises(OutOfData, unpacker.unpack)

        unpacker.feed(b'foo')
        unpacker.feed(b'bar')

        k = 0
        for o, e in zip(unpacker, 'foobarbaz'):
            assert o == ord(e)
            k += 1
        assert k == len(b'foobar')
예제 #39
0
def test_write_bytes():
    unpacker = Unpacker()
    unpacker.feed(b'abc')
    f = io.BytesIO()
    assert unpacker.unpack(f.write) == ord('a')
    assert f.getvalue() == b'a'
    f = io.BytesIO()
    assert unpacker.skip(f.write) is None
    assert f.getvalue() == b'b'
    f = io.BytesIO()
    assert unpacker.skip() is None
    assert f.getvalue() == b''
예제 #40
0
    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
예제 #41
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
예제 #42
0
    def test_foobar(self):
        unpacker = Unpacker(read_size=3, use_list=1)
        unpacker.feed(b'foobar')
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.unpack() == ord(b'o')
        assert unpacker.unpack() == ord(b'o')
        assert unpacker.unpack() == ord(b'b')
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')
        self.assertRaises(OutOfData, unpacker.unpack)

        unpacker.feed(b'foo')
        unpacker.feed(b'bar')

        k = 0
        for o, e in zip(unpacker, 'foobarbaz'):
            assert o == ord(e)
            k += 1
        assert k == len(b'foobar')
예제 #43
0
    def test_foobar(self):
        unpacker = Unpacker(read_size=3, use_list=1)
        unpacker.feed(b"foobar")
        assert unpacker.unpack() == ord(b"f")
        assert unpacker.unpack() == ord(b"o")
        assert unpacker.unpack() == ord(b"o")
        assert unpacker.unpack() == ord(b"b")
        assert unpacker.unpack() == ord(b"a")
        assert unpacker.unpack() == ord(b"r")
        self.assertRaises(OutOfData, unpacker.unpack)

        unpacker.feed(b"foo")
        unpacker.feed(b"bar")

        k = 0
        for o, e in zip(unpacker, "foobarbaz"):
            assert o == ord(e)
            k += 1
        assert k == len(b"foobar")
예제 #44
0
 def test_foobar_skip(self):
     unpacker = Unpacker(read_size=3, use_list=1)
     unpacker.feed(b"foobar")
     assert unpacker.unpack() == ord(b"f")
     unpacker.skip()
     assert unpacker.unpack() == ord(b"o")
     unpacker.skip()
     assert unpacker.unpack() == ord(b"a")
     unpacker.skip()
     self.assertRaises(OutOfData, unpacker.unpack)
예제 #45
0
 def test_foobar_skip(self):
     unpacker = Unpacker(read_size=3, use_list=1)
     unpacker.feed(b'foobar')
     assert unpacker.unpack() == ord(b'f')
     unpacker.skip()
     assert unpacker.unpack() == ord(b'o')
     unpacker.skip()
     assert unpacker.unpack() == ord(b'a')
     unpacker.skip()
     self.assertRaises(OutOfData, unpacker.unpack)
예제 #46
0
 def test_partialdata(self):
     unpacker = Unpacker()
     unpacker.feed(b'\xa5')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'h')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'a')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'l')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'l')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'o')
     assert next(iter(unpacker)) == b'hallo'
예제 #47
0
 def test_partialdata(self):
     unpacker = Unpacker()
     unpacker.feed(b'\xa5')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'h')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'a')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'l')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'l')
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b'o')
     assert next(iter(unpacker)) == b'hallo'
예제 #48
0
 def test_foobar_skip(self):
     unpacker = Unpacker(read_size=3, use_list=1)
     unpacker.feed(b'foobar')
     assert unpacker.unpack() == ord(b'f')
     unpacker.skip()
     assert unpacker.unpack() == ord(b'o')
     unpacker.skip()
     assert unpacker.unpack() == ord(b'a')
     unpacker.skip()
     self.assertRaises(OutOfData, unpacker.unpack)
예제 #49
0
    def test_readbytes(self):
        unpacker = Unpacker(read_size=3)
        unpacker.feed(b'foobar')
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.read_bytes(3) == b'oob'
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')

        # Test buffer refill
        unpacker = Unpacker(compat.BytesIO(b'foobar'), read_size=3)
        assert unpacker.unpack() == ord(b'f')
        assert unpacker.read_bytes(3) == b'oob'
        assert unpacker.unpack() == ord(b'a')
        assert unpacker.unpack() == ord(b'r')
예제 #50
0
 def test_partialdata(self):
     unpacker = Unpacker()
     unpacker.feed(b"\xa5")
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b"h")
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b"a")
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b"l")
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b"l")
     self.assertRaises(StopIteration, next, iter(unpacker))
     unpacker.feed(b"o")
     assert next(iter(unpacker)) == b"hallo"