8000 Warn about future use_bin_type change (#264) · msgpack/msgpack-python@d0d3a40 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0d3a40

Browse files
authored
Warn about future use_bin_type change (#264)
1 parent 1979722 commit d0d3a40

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

README.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
6060
.. code-block:: pycon
6161
6262
>>> import msgpack
63-
>>> msgpack.packb([1, 2, 3])
63+
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
6464
'\x93\x01\x02\x03'
6565
>>> msgpack.unpackb(_)
6666
[1, 2, 3]
@@ -91,13 +91,13 @@ stream (or from bytes provided through its ``feed`` method).
9191
9292
buf = BytesIO()
9393
for i in range(100):
94-
buf.write(msgpack.packb(range(i)))
94+
buf.write(msgpack.packb(range(i), use_bin_type=True))
9595
9696
buf.seek(0)
9797
9898
unpacker = msgpack.Unpacker(buf)
9999
for unpacked in unpacker:
100-
print unpacked
100+
print(unpacked)
101101
102102
103103
Packing/unpacking of custom data type
@@ -109,7 +109,6 @@ It is also possible to pack/unpack custom data types. Here is an example for
109109
.. code-block:: python
110110
111111
import datetime
112-
113112
import msgpack
114113
115114
useful_dict = {
@@ -128,7 +127,7 @@ It is also possible to pack/unpack custom data types. Here is an example for
128127
return obj
129128
130129
131-
packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
130+
packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
132131
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)
133132
134133
``Unpacker``'s ``object_hook`` callback receives a dict; the
@@ -208,6 +207,10 @@ the packer. If you do so, it will use a non-standard type called **bin** to
208207
serialize byte arrays, and **raw** becomes to mean **str**. If you want to
209208
distinguish **bin** and **raw** in the unpacker, specify `encoding='utf-8'`.
210209

210+
**In future version, default value of ``use_bin_type`` will be changed to ``False``.
211+
To avoid this change will break your code, you must specify it explicitly
212+
even when you want to use old format.**
213+
211214
Note that Python 2 defaults to byte-arrays over Unicode strings:
212215

213216
.. code-block:: pycon

msgpack/_packer.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#cython: embedsignature=True
33

44
from cpython cimport *
5+
from cpython.exc cimport PyErr_WarnEx
56

67
from msgpack.exceptions import PackValueError, PackOverflowError
78
from msgpack import ExtType
@@ -76,6 +77,8 @@ cdef class Packer(object):
7677
:param bool use_bin_type:
7778
Use bin type introduced in msgpack spec 2.0 for bytes.
7879
It also enables str8 type for unicode.
80+
Current default value is false, but it will be changed to true
81+
in future version. You should specify it explicitly.
7982
:param bool strict_types:
8083
If set to true, types will be checked to be exact. Derived classes
8184
from serializeable types will not be serialized and will be
@@ -103,12 +106,17 @@ cdef class Packer(object):
103106
self.pk.length = 0
104107

105108
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
106-
use_single_float=False, bint autoreset=1, bint use_bin_type=0,
109+
use_single_float=False, bint autoreset=1, use_bin_type=None,
107110
bint strict_types=0):
111+
if use_bin_type is None:
112+
PyErr_WarnEx(
113+
FutureWarning,
114+
"use_bin_type option is not specified. Default value of the option will be changed in future version.",
115+
1)
108116
self.use_float = use_single_float
109117
self.strict_types = strict_types
110118
self.autoreset = autoreset
111-
self.pk.use_bin_type = use_bin_type
119+
self.pk.use_bin_type = <bint>use_bin_type
112120
if default is not None:
113121
if not PyCallable_Check(default):
114122
raise TypeError("default must be a callable.")

0 commit comments

Comments
 (0)
0