Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #43 from maralla/skip
Browse files Browse the repository at this point in the history
use skip instead of raising exception
  • Loading branch information
lxyu committed Sep 18, 2014
2 parents 0722124 + 038777a commit 76617ef
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
65 changes: 65 additions & 0 deletions tests/test_protocol_cybinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,68 @@ class Hello(TPayload):

item2 = Hello()
p.read_struct(item2)


def test_skip_bool():
b = TCyBufferedTransport(TMemoryBuffer())
proto.write_val(b, TType.BOOL, 1)
proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.BOOL)
assert 123 == proto.read_val(b, TType.I32)


def test_skip_double():
b = TCyBufferedTransport(TMemoryBuffer())
proto.write_val(b, TType.DOUBLE, 0.123425897)
proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.DOUBLE)
assert 123 == proto.read_val(b, TType.I32)


def test_skip_string():
b = TCyBufferedTransport(TMemoryBuffer())
proto.write_val(b, TType.STRING, "hello world")
proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.STRING)
assert 123 == proto.read_val(b, TType.I32)


def test_skip_list():
b = TCyBufferedTransport(TMemoryBuffer())
proto.write_val(b, TType.LIST, [5, 6, 7, 8, 9], spec=TType.I32)
proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.LIST)
assert 123 == proto.read_val(b, TType.I32)


def test_skip_map():
b = TCyBufferedTransport(TMemoryBuffer())
proto.write_val(b, TType.MAP, {"hello": 0.3456},
spec=(TType.STRING, TType.DOUBLE))
proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.MAP)
assert 123 == proto.read_val(b, TType.I32)


def test_skip_struct():
b = TCyBufferedTransport(TMemoryBuffer())
p = proto.TCyBinaryProtocol(b)
item = TItem(id=123, phones=["123456", "abcdef"])
p.write_struct(item)
p.write_message_end()

proto.write_val(b, TType.I32, 123)
b.flush()

proto.skip(b, TType.STRUCT)
assert 123 == proto.read_val(b, TType.I32)
39 changes: 38 additions & 1 deletion thriftpy/protocol/cybin/cybin.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ cdef inline read_struct(TCyBufferedTransport buf, obj):

fid = read_i16(buf)
if fid not in field_specs:
raise ProtocolError("skip")
skip(buf, field_type)
continue

field_spec = field_specs[fid]
ttype = field_spec[0]
Expand Down Expand Up @@ -326,6 +327,42 @@ cdef c_write_val(TCyBufferedTransport buf, TType ttype, val, spec=None):
write_struct(buf, val)


cpdef skip(TCyBufferedTransport buf, TType ttype):
cdef TType v_type, k_type, f_type
cdef int i, size

if ttype == T_BOOL or ttype == T_I08:
read_i08(buf)
elif ttype == T_I16:
read_i16(buf)
elif ttype == T_I32:
read_i32(buf)
elif ttype == T_I64 or ttype == T_DOUBLE:
read_i64(buf)
elif ttype == T_STRING:
size = read_i32(buf)
c_read_string(buf, size)
elif ttype == T_SET or ttype == T_LIST:
v_type = <TType>read_i08(buf)
size = read_i32(buf)
for i in range(size):
skip(buf, v_type)
elif ttype == T_MAP:
k_type = <TType>read_i08(buf)
v_type = <TType>read_i08(buf)
size = read_i32(buf)
for i in range(size):
skip(buf, k_type)
skip(buf, v_type)
elif ttype == T_STRUCT:
while 1:
f_type = <TType>read_i08(buf)
if f_type == T_STOP:
break
read_i16(buf)
skip(buf, f_type)


def read_val(TCyBufferedTransport buf, TType ttype):
return c_read_val(buf, ttype)

Expand Down

0 comments on commit 76617ef

Please # to comment.