@@ -10,8 +10,21 @@ MessagePack for Python
10
10
:target: https://msgpack-python.readthedocs.io/en/latest/?badge=latest
11
11
:alt: Documentation Status
12
12
13
- IMPORTANT: Upgrading from msgpack-0.4
14
- --------------------------------------
13
+
14
+ What's this
15
+ -----------
16
+
17
+ `MessagePack <https://msgpack.org/ >`_ is an efficient binary serialization format.
18
+ It lets you exchange data among multiple languages like JSON.
19
+ But it's faster and smaller.
20
+ This package provides CPython bindings for reading and writing MessagePack data.
21
+
22
+
23
+ Very important notes for existing users
24
+ ---------------------------------------
25
+
26
+ PyPI package name
27
+ ^^^^^^^^^^^^^^^^^
15
28
16
29
TL;DR: When upgrading from msgpack-0.4 or earlier, don't do `pip install -U msgpack-python `.
17
30
Do `pip uninstall msgpack-python; pip install msgpack ` instead.
@@ -24,13 +37,37 @@ Sadly, this doesn't work for upgrade install. After `pip install -U msgpack-pyt
24
37
msgpack is removed and `import msgpack ` fail.
25
38
26
39
27
- What's this
28
- -----------
40
+ Deprecating encoding option
41
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42
+
43
+ encoding and unicode_errors options are deprecated.
44
+
45
+ In case of packer, use UTF-8 always. Storing other than UTF-8 is not recommended.
46
+
47
+ For backward compatibility, you can use ``use_bin_type=False `` and pack ``bytes ``
48
+ object into msgpack raw type.
49
+
50
+ In case of unpacker, there is new ``raw_as_bytes `` option. It is ``True `` by default
51
+ for backward compatibility, but it is changed to ``False `` in near future.
52
+ You can use ``raw_as_bytes=False `` instead of ``encoding='utf-8' ``.
53
+
54
+ Planned backward incompatible changes
55
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56
+
57
+ When msgpack 1.0, I planning these breaking changes:
58
+
59
+ * packer and unpacker: Remove ``encoding `` and ``unicode_errors `` option.
60
+ * packer: Change default of ``use_bin_type `` option from False to True.
61
+ * unpacker: Change default of ``raw_as_bytes `` option from True to False.
62
+ * unpacker: Reduce all ``max_xxx_len `` options for typical usage.
63
+ * unpacker: Remove ``write_bytes `` option from all methods.
64
+
65
+ To avoid these breaking changes breaks your application, please:
66
+
67
+ * Don't use deprecated options.
68
+ * Pass ``use_bin_type `` and ``raw_as_bytes `` options explicitly.
69
+ * If your application handle large (>1MB) data, specify ``max_xxx_len `` options too.
29
70
30
- `MessagePack <https://msgpack.org/ >`_ is an efficient binary serialization format.
31
- It lets you exchange data among multiple languages like JSON.
32
- But it's faster and smaller.
33
- This package provides CPython bindings for reading and writing MessagePack data.
34
71
35
72
Install
36
73
-------
@@ -76,14 +113,14 @@ msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
76
113
>>> import msgpack
77
114
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
78
115
'\x93\x01\x02\x03'
79
- >>> msgpack.unpackb(_)
116
+ >>> msgpack.unpackb(_, raw_as_bytes=False )
80
117
[1, 2, 3]
81
118
82
119
``unpack `` unpacks msgpack's array to Python's list, but can also unpack to tuple:
83
120
84
121
.. code-block :: pycon
85
122
86
- >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False)
123
+ >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw_as_bytes=False )
87
124
(1, 2, 3)
88
125
89
126
You should always specify the ``use_list `` keyword argument for backward compatibility.
@@ -109,7 +146,7 @@ stream (or from bytes provided through its ``feed`` method).
109
146
110
147
buf.seek(0 )
111
148
112
- unpacker = msgpack.Unpacker(buf)
149
+ unpacker = msgpack.Unpacker(buf, raw_as_bytes = False )
113
150
for unpacked in unpacker:
114
151
print (unpacked)
115
152
@@ -142,7 +179,7 @@ It is also possible to pack/unpack custom data types. Here is an example for
142
179
143
180
144
181
packed_dict = msgpack.packb(useful_dict, default = encode_datetime, use_bin_type = True )
145
- this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime)
182
+ this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime, raw_as_bytes = False )
146
183
147
184
``Unpacker ``'s ``object_hook `` callback receives a dict; the
148
185
``object_pairs_hook `` callback may instead be used to receive a list of
@@ -172,7 +209,7 @@ It is also possible to pack/unpack custom data types using the **ext** type.
172
209
...
173
210
>>> data = array.array('d', [1.2, 3.4])
174
211
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
175
- >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook)
212
+ >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw_as_bytes=False )
176
213
>>> data == unpacked
177
214
True
178
215
@@ -217,14 +254,10 @@ Early versions of msgpack didn't distinguish string and binary types (like Pytho
217
254
The type for representing both string and binary types was named **raw **.
218
255
219
256
For backward compatibility reasons, msgpack-python will still default all
220
- strings to byte strings, unless you specify the `use_bin_type=True ` option in
257
+ strings to byte strings, unless you specify the `` use_bin_type=True ` ` option in
221
258
the packer. If you do so, it will use a non-standard type called **bin ** to
222
259
serialize byte arrays, and **raw ** becomes to mean **str **. If you want to
223
- distinguish **bin ** and **raw ** in the unpacker, specify `encoding='utf-8' `.
224
-
225
- **In future version, default value of ``use_bin_type`` will be changed to ``True``.
226
- To avoid this change will break your code, you must specify it explicitly
227
- even when you want to use old format. **
260
+ distinguish **bin ** and **raw ** in the unpacker, specify ``raw_as_bytes=False ``.
228
261
229
262
Note that Python 2 defaults to byte-arrays over Unicode strings:
230
263
@@ -234,7 +267,7 @@ Note that Python 2 defaults to byte-arrays over Unicode strings:
234
267
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
235
268
['spam', 'eggs']
236
269
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
237
- encoding='utf-8' )
270
+ raw_as_bytes=False )
238
271
['spam', u'eggs']
239
272
240
273
This is the same code in Python 3 (same behaviour, but Python 3 has a
@@ -246,7 +279,7 @@ different default):
246
279
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
247
280
[b'spam', b'eggs']
248
281
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
249
- encoding='utf-8' )
282
+ raw_as_bytes=False )
250
283
[b'spam', 'eggs']
251
284
252
285
@@ -277,6 +310,7 @@ You can use ``gc.disable()`` when unpacking large message.
277
310
278
311
use_list option
279
312
^^^^^^^^^^^^^^^
313
+
280
314
List is the default sequence type of Python.
281
315
But tuple is lighter than list.
282
316
You can use ``use_list=False `` while unpacking when performance is important.
295
329
MessagePack uses `pytest ` for testing.
296
330
Run test with following command:
297
331
298
- $ pytest -v test
332
+ $ make test
299
333
300
334
301
335
..
0 commit comments