8000 [WIP] Newspec stage 2. by methane · Pull Request #79 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Newspec stage 2. #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 20, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d610975
add support for extended types: you can now pack/unpack custom python…
antocuni Oct 15, 2013
5529dfe
kill some duplicate code from unpack/unpackb and move the logic to Un…
antocuni Oct 18, 2013
522c4bf
slightly change to API
antocuni Oct 18, 2013
c727440
automatically find the best format to encode extended types
antocuni Oct 18, 2013
afa28fb
add support to unpack all ext formats
antocuni Oct 18, 2013
5467515
implement Packer.pack_extended_type also in the cython version of the…
antocuni Oct 18, 2013
a7485ec
add the hook for unknown types also to the cython Packer
antocuni Oct 18, 2013
ff85838
implement unpack_one also for the cython version, and add a test for it
antocuni Oct 18, 2013
985d4c1
add a test for unpacking extended types
antocuni Oct 19, 2013
56dd165
implement unpacking for all the fixtext formats
antocuni Oct 19, 2013
c9b97f0
implement unpacking of ext 8,16,32
antocuni Oct 19, 2013
6386481
add a note in the README
antocuni Oct 19, 2013
27f0cba
Merge branch 'master' of https://github.com/antocuni/msgpack-python i…
methane Oct 20, 2013
aa68c9b
fallback: Support pack_ext_type.
methane Oct 20, 2013
96bcd76
Packing ExtType and some cleanup
methane Oct 20, 2013
822cce8
Support unpacking new types.
methane Oct 20, 2013
0d5c58b
cleanup
methane Oct 20, 2013
84dc99c
Add ext_type example to README.
methane Oct 20, 2013
cb78959
Update README.
methane Oct 20, 2013
37c2ad6
Add tests and bugfix.
methane Oct 20, 2013
e3fee4d
fallback: support packing ExtType
methane Oct 20, 2013
d84a403
fix bugs.
methane Oct 20, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
kill some duplicate code from unpack/unpackb and move the logic to Un…
…packer.unpack_one. By doing this we no longer need to make the module-level pack/unpack parametric on the class, because they contain no logic at all
  • Loading branch information
antocuni committed Oct 18, 2013
commit 5529dfe59660f3c2fc5058e6fa42b24fe764a255
95 changes: 47 additions & 48 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ def getvalue(self):

DEFAULT_RECURSE_LIMIT=511

def pack(o, stream, **kwargs):
"""
Pack object `o` and write it to `stream`

See :class:`Packer` for options.
"""
packer = Packer(**kwargs)
stream.write(packer.pack(o))

def packb(o, **kwargs):
"""
Pack object `o` and return packed bytes

See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)

def unpack(stream, **kwargs):
"""
Unpack an object from `stream`.

Raises `ExtraData` when `packed` contains extra bytes.
See :class:`Unpacker` for options.
"""
unpacker = Unpacker(stream, **kwargs)
return unpacker.unpack_one()

def unpackb(packed, **kwargs):
"""
Unpack an object from `packed`.

Raises `ExtraData` when `packed` contains extra bytes.
See :class:`Unpacker` for options.
"""
unpacker = Unpacker(None, **kwargs)
unpacker.feed(packed)
return unpacker.unpack_one()

class Unpacker(object):
"""
Streaming unpacker.
Expand Down Expand Up @@ -149,6 +187,15 @@ def __init__(self, file_like=None, read_size=0, use_list=True,
raise ValueError("object_pairs_hook and object_hook are mutually "
"exclusive")

def unpack_one(self):
try:
ret = self._fb_unpack()
except OutOfData:
raise UnpackValueError("Data is not enough.")
if self._fb_got_extradata():
raise ExtraData(ret, self._fb_get_extradata())
return ret

def feed(self, next_bytes):
if isinstance(next_bytes, array.array):
next_bytes = next_bytes.tostring()
Expand Down Expand Up @@ -579,51 +626,3 @@ def bytes(self):
def reset(self):
self._buffer = StringIO()


def pack(o, stream, Packer=Packer, **kwargs):
"""
Pack object `o` and write it to `stream`

See :class:`Packer` for options.
"""
packer = Packer(**kwargs)
stream.write(packer.pack(o))

def packb(o, Packer=Packer, **kwargs):
"""
Pack object `o` and return packed bytes

See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)

def unpack(stream, Unpacker=Unpacker, **kwargs):
"""
Unpack an object from `stream`.

Raises `ExtraData` when `packed` contains extra bytes.
See :class:`Unpacker` for options.
"""
unpacker = Unpacker(stream, **kwargs)
ret = unpacker._fb_unpack()
if unpacker._fb_got_extradata():
raise ExtraData(ret, unpacker._fb_get_extradata())
return ret

def unpackb(packed, Unpacker=Unpacker, **kwargs):
"""
Unpack an object from `packed`.

Raises `ExtraData` when `packed` contains extra bytes.
See :class:`Unpacker` for options.
"""
unpacker = Unpacker(None, **kwargs)
A131 unpacker.feed(packed)
try:
ret = unpacker._fb_unpack()
except OutOfData:
raise UnpackValueError("Data is not enough.")
if unpacker._fb_got_extradata():
raise ExtraData(ret, unpacker._fb_get_extradata())
return ret

8 changes: 5 additions & 3 deletions test/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def handle_extended_type(self, typecode, data):
return obj

obj = [42, 'hello', array.array('d', [1.1, 2.2, 3.3])]
s = msgpack.packb(obj, MyPacker)
obj2 = msgpack.unpackb(s, MyUnpacker)
packer = MyPacker()
unpacker = MyUnpacker(None)
s = packer.pack(obj)
unpacker.feed(s)
obj2 = unpacker.unpack_one()
assert obj == obj2

0