msgpack/msgpack-python — pull request #23 by jnothman (2012-09-23 06:23:17+00:00)
Title: Allow packed data to be captured while executing skip(), etc.

One may want to acquire the bytes corresponding to a single msgpack message. For example, consider a server whose job is to forward messages to a set of workers.

In the first instance, it may not care what the messages contain, it just reads messages off a stream and passes them on to workers. With the current `msgpack-python`, if the message length is not known, this requires unpacking and re-packing. Ideally one might want `Unpacker.read_packed()`. This patch instead offers the idiom:

bio = BytesIO()
unpacker.skip(bio.write)
msg_bytes = bio.getvalue()

Now consider the case that our server still passes on raw messages, but only those who have a particular value, say `route_to == 'me'`:

bio = BytesIO()
obj = unpacker.unpack(bio.write)
if obj['route_to'] is 'me':
route_to_me(bio.getvalue())

This avoids packing. With `read_map_header` supporting a similar interface, the following does the same, but avoids full unpacking:

bio = BytesIO()
route_to = None
map_size = unpacker.read_map_header(bio.write)
for i in range(map_size):
key = unpacker.unpack(bio.write)
if key is 'route_to':
route_to = unpacker.unpack(bio.write)
else:
unpacker.skip(bio.write)
if route_to is 'me':
route_to_me(bio.getvalue())