Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MessagePack serializer implementation for Python msgpack.org[Python]

License

NotificationsYou must be signed in to change notification settings

msgpack/msgpack-python

Repository files navigation

Build StatusDocumentation Status

What is this?

MessagePack is an efficient binary serialization format.It lets you exchange data among multiple languages like JSON.But it's faster and smaller.This package provides CPython bindings for reading and writing MessagePack data.

Install

$ pip install msgpack

Pure Python implementation

The extension module in msgpack (msgpack._cmsgpack) does not support PyPy.

But msgpack provides a pure Python implementation (msgpack.fallback) for PyPy.

Windows

If you can't use a binary distribution, you need to install Visual Studioor the Windows SDK on Windows.Without the extension, the pure Python implementation on CPython runs slowly.

How to use

One-shot pack & unpack

Usepackb for packing andunpackb for unpacking.msgpack providesdumps andloads as aliases for compatibility withjson andpickle.

pack anddump pack to a file-like object.unpack andload unpack from a file-like object.

>>>import msgpack>>> msgpack.packb([1,2,3])'\x93\x01\x02\x03'>>> msgpack.unpackb(_)[1, 2, 3]

Read the docstring for options.

Streaming unpacking

Unpacker is a "streaming unpacker". It unpacks multiple objects from onestream (or from bytes provided through itsfeed method).

importmsgpackfromioimportBytesIObuf=BytesIO()foriinrange(100):buf.write(msgpack.packb(i))buf.seek(0)unpacker=msgpack.Unpacker(buf)forunpackedinunpacker:print(unpacked)

Packing/unpacking of custom data types

It is also possible to pack/unpack custom data types. Here is an example fordatetime.datetime.

importdatetimeimportmsgpackuseful_dict= {"id":1,"created":datetime.datetime.now(),}defdecode_datetime(obj):if'__datetime__'inobj:obj=datetime.datetime.strptime(obj["as_str"],"%Y%m%dT%H:%M:%S.%f")returnobjdefencode_datetime(obj):ifisinstance(obj,datetime.datetime):return {'__datetime__':True,'as_str':obj.strftime("%Y%m%dT%H:%M:%S.%f")}returnobjpacked_dict=msgpack.packb(useful_dict,default=encode_datetime)this_dict_again=msgpack.unpackb(packed_dict,object_hook=decode_datetime)

Unpacker'sobject_hook callback receives a dict; theobject_pairs_hook callback may instead be used to receive a list ofkey-value pairs.

NOTE: msgpack can encode datetime with tzinfo into standard ext type for now.Seedatetime option inPacker docstring.

Extended types

It is also possible to pack/unpack custom data types using theext type.

>>>import msgpack>>>import array>>>defdefault(obj):...ifisinstance(obj, array.array)and obj.typecode=='d':...return msgpack.ExtType(42, obj.tostring())...raiseTypeError("Unknown type:%r"% (obj,))...>>>defext_hook(code,data):...if code==42:...         a= array.array('d')...         a.fromstring(data)...return a...return ExtType(code, data)...>>> data= array.array('d', [1.2,3.4])>>> packed= msgpack.packb(data,default=default)>>> unpacked= msgpack.unpackb(packed,ext_hook=ext_hook)>>> data== unpackedTrue

Advanced unpacking control

As an alternative to iteration,Unpacker objects provideunpack,skip,read_array_header, andread_map_header methods. The former tworead an entire message from the stream, respectively deserializing and returningthe result, or ignoring it. The latter two methods return the number of elementsin the upcoming container, so that each element in an array, or key-value pairin a map, can be unpacked or skipped individually.

Notes

String and binary types in the old MessagePack spec

Early versions of msgpack didn't distinguish string and binary types.The type for representing both string and binary types was namedraw.

You can pack into and unpack from this old spec usinguse_bin_type=Falseandraw=True options.

>>>import msgpack>>> msgpack.unpackb(msgpack.packb([b'spam','eggs'],use_bin_type=False),raw=True)[b'spam', b'eggs']>>> msgpack.unpackb(msgpack.packb([b'spam','eggs'],use_bin_type=True),raw=False)[b'spam', 'eggs']

ext type

To use theext type, pass amsgpack.ExtType object to the packer.

>>>import msgpack>>> packed= msgpack.packb(msgpack.ExtType(42,b'xyzzy'))>>> msgpack.unpackb(packed)ExtType(code=42, data='xyzzy')

You can use it withdefault andext_hook. See below.

Security

When unpacking data received from an unreliable source, msgpack providestwo security options.

max_buffer_size (default:100*1024*1024) limits the internal buffer size.It is also used to limit preallocated list sizes.

strict_map_key (default:True) limits the type of map keys to bytes and str.While the MessagePack spec doesn't limit map key types,there is a risk of a hash DoS.If you need to support other types for map keys, usestrict_map_key=False.

Performance tips

CPython's GC starts when the number of allocated objects grows.This means unpacking may trigger unnecessary GC.You can usegc.disable() when unpacking a large message.

A list is the default sequence type in Python.However, a tuple is lighter than a list.You can useuse_list=False while unpacking when performance is important.

Major breaking changes in the history

msgpack 0.5

The package name on PyPI was changed frommsgpack-python tomsgpack in 0.5.

When upgrading from msgpack-0.4 or earlier, dopip uninstall msgpack-python beforepip install -U msgpack.

msgpack 1.0

  • Python 2 support

    • The extension module no longer supports Python 2.The pure Python implementation (msgpack.fallback) is used for Python 2.

    • msgpack 1.0.6 drops official support of Python 2.7, as pip andGitHub Action "setup-python" no longer supports Python 2.7.

  • Packer

    • Packer usesuse_bin_type=True by default.Bytes are encoded in the bin type in MessagePack.
    • Theencoding option is removed. UTF-8 is always used.
  • Unpacker

    • Unpacker usesraw=False by default. It assumes str values are valid UTF-8 stringsand decodes them to Python str (Unicode) objects.
    • encoding option is removed. You can useraw=True to support old format (e.g. unpack into bytes, not str).
    • The default value ofmax_buffer_size is changed from 0 to 100 MiB to avoid DoS attacks.You need to passmax_buffer_size=0 if you have large but safe data.
    • The default value ofstrict_map_key is changed to True to avoid hash DoS.You need to passstrict_map_key=False if you have data that contain map keyswhose type is neither bytes nor str.

About

MessagePack serializer implementation for Python msgpack.org[Python]

Topics

Resources

License

Security policy

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp