8000 s/raw_as_bytes/raw/g (#276) · sa7mon/msgpack-python@5569a4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 5569a4e

Browse files
authored
s/raw_as_bytes/raw/g (msgpack#276)
fixes msgpack#273
1 parent d9ec8fc commit 5569a4e

File tree

9 files changed

+48
-47
lines changed

9 files changed

+48
-47
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ serve-doc: all
1818
.PHONY: clean
1919
clean:
2020
rm -rf build
21-
rm msgpack/*.so
21+
rm -f msgpack/_packer.cpp
22+
rm -f msgpack/_unpacker.cpp
2223
rm -rf msgpack/__pycache__
2324
rm -rf test/__pycache__
2425

README.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ In case of packer, use UTF-8 always. Storing other than UTF-8 is not recommende
4747
For backward compatibility, you can use ``use_bin_type=False`` and pack ``bytes``
4848
object into msgpack raw type.
4949

50-
In case of unpacker, there is new ``raw_as_bytes`` option. It is ``True`` by default
50+
In case of unpacker, there is new ``raw`` option. It is ``True`` by default
5151
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'``.
52+
You can use ``raw=False`` instead of ``encoding='utf-8'``.
5353

5454
Planned backward incompatible changes
5555
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -58,14 +58,14 @@ When msgpack 1.0, I planning these breaking changes:
5858

5959
* packer and unpacker: Remove ``encoding`` and ``unicode_errors`` option.
6060
* 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.
61+
* unpacker: Change default of ``raw`` option from True to False.
6262
* unpacker: Reduce all ``max_xxx_len`` options for typical usage.
6363
* unpacker: Remove ``write_bytes`` option from all methods.
6464

6565
To avoid these breaking changes breaks your application, please:
6666

6767
* Don't use deprecated options.
68-
* Pass ``use_bin_type`` and ``raw_as_bytes`` options explicitly.
68+
* Pass ``use_bin_type`` and ``raw`` options explicitly.
6969
* If your application handle large (>1MB) data, specify ``max_xxx_len`` options too.
7070

7171

@@ -113,14 +113,14 @@ msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
113113
>>> import msgpack
114114
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
115115
'\x93\x01\x02\x03'
116-
>>> msgpack.unpackb(_, raw_as_bytes=False)
116+
>>> msgpack.unpackb(_, raw=False)
117117
[1, 2, 3]
118118
119119
``unpack`` unpacks msgpack's array to Python's list, but can also unpack to tuple:
120120

121121
.. code-block:: pycon
122122
123-
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw_as_bytes=False)
123+
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
124124
(1, 2, 3)
125125
126126
You should always specify the ``use_list`` keyword argument for backward compatibility.
@@ -146,7 +146,7 @@ stream (or from bytes provided through its ``feed`` method).
146146
147147
buf.seek(0)
148148
149-
unpacker = msgpack.Unpacker(buf, raw_as_bytes=False)
149+
unpacker = msgpack.Unpacker(buf, raw=False)
150150
for unpacked in unpacker:
151151
print(unpacked)
152152
@@ -179,7 +179,7 @@ It is also possible to pack/unpack custom data types. Here is an example for
179179
180180
181181
packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
182-
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw_as_bytes=False)
182+
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)
183183
184184
``Unpacker``'s ``object_hook`` callback receives a dict; the
185185
``object_pairs_hook`` callback may instead be used to receive a list of
@@ -209,7 +209,7 @@ It is also possible to pack/unpack custom data types using the **ext** type.
209209
...
210210
>>> data = array.array('d', [1.2, 3.4])
211211
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
212-
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw_as_bytes=False)
212+
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
213213
>>> data == unpacked
214214
True
215215
@@ -257,7 +257,7 @@ For backward compatibility reasons, msgpack-python will still default all
257257
strings to byte strings, unless you specify the ``use_bin_type=True`` option in
258258
the packer. If you do so, it will use a non-standard type called **bin** to
259259
serialize byte arrays, and **raw** becomes to mean **str**. If you want to
260-
distinguish **bin** and **raw** in the unpacker, specify ``raw_as_bytes=False``.
260+
distinguish **bin** and **raw** in the unpacker, specify ``raw=False``.
261261

262262
Note that Python 2 defaults to byte-arrays over Unicode strings:
263263

@@ -267,7 +267,7 @@ Note that Python 2 defaults to byte-arrays over Unicode strings:
267267
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
268268
['spam', 'eggs']
269269
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
270-
raw_as_bytes=False)
270+
raw=False)
271271
['spam', u'eggs']
272272
273273
This is the same code in Python 3 (same behaviour, but Python 3 has a
@@ -279,7 +279,7 @@ different default):
279279
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
280280
[b'spam', b'eggs']
281281
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
282-
raw_as_bytes=False)
282+
raw=False)
283283
[b'spam', 'eggs']
284284
285285

msgpack/_unpacker.pyx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ from msgpack import ExtType
4343
cdef extern from "unpack.h":
4444
ctypedef struct msgpack_user:
4545
bint use_list
46-
bint raw_as_bytes
46+
bint raw
4747
bint has_pairs_hook # call object_hook with k-v pairs
4848
PyObject* object_hook
4949
PyObject* list_hook
@@ -74,14 +74,14 @@ cdef extern from "unpack.h":
7474
cdef inline init_ctx(unpack_context *ctx,
7575
object object_hook, object object_pairs_hook,
7676
object list_hook, object ext_hook,
77-
bint use_list, bint raw_as_bytes,
77+
bint use_list, bint raw,
7878
char* encoding, char* unicode_errors,
7979
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
8080
Py_ssize_t max_array_len, Py_ssize_t max_map_len,
8181
Py_ssize_t max_ext_len):
8282
unpack_init(ctx)
8383
ctx.user.use_list = use_list
84-
ctx.user.raw_as_bytes = raw_as_bytes
84+
ctx.user.raw = raw
8585
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
8686
ctx.user.max_str_len = max_str_len
8787
ctx.user.max_bin_len = max_bin_len
@@ -158,7 +158,7 @@ cdef inline int get_data_from_buffer(object obj,
158158
return 1
159159

160160
def unpackb(object packed, object object_hook=None, object list_hook=None,
161-
bint use_list=True, bint raw_as_bytes=True,
161+
bint use_list=True, bint raw=True,
162162
encoding=None, unicode_errors="strict",
163163
object_pairs_hook=None, ext_hook=ExtType,
164164
Py_ssize_t max_str_len=2147483647, # 2**32-1
@@ -185,7 +185,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
185185
cdef int new_protocol = 0
186186

187187
if encoding is not None:
188-
PyErr_WarnEx(PendingDeprecationWarning, "encoding is deprecated, Use raw_as_bytes=False instead.", 1)
188+
PyErr_WarnEx(PendingDeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
189189
if isinstance(encoding, unicode):
190190
encoding = encoding.encode('ascii')
191191
elif not isinstance(encoding, bytes):
@@ -203,7 +203,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
203203
get_data_from_buffer(packed, &view, &buf, &buf_len, &new_protocol)
204204
try:
205205
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
206-
use_list, raw_as_bytes, cenc, cerr,
206+
use_list, raw, cenc, cerr,
207207
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
208208
ret = unpack_construct(&ctx, buf, buf_len, &off)
209209
finally:
@@ -261,7 +261,7 @@ cdef class Unpacker(object):
261261
If true, unpack msgpack array to Python list.
262262
Otherwise, unpack to Python tuple. (default: True)
263263
264-
:param bool raw_as_bytes:
264+
:param bool raw:
265265
If true, unpack msgpack raw to Python bytes (default).
266266
Otherwise, unpack to Python str (or unicode on Python 2) by decoding
267267
with UTF-8 encoding (recommended).
@@ -299,7 +299,7 @@ cdef class Unpacker(object):
299299
Limits max length of map. (default: 2**31-1)
300300
301301
:param str encoding:
302-
Deprecated, use raw_as_bytes instead.
302+
Deprecated, use raw instead.
303303
Encoding used for decoding msgpack raw.
304304
If it is None (default), msgpack raw is deserialized to Python bytes.
305305
@@ -310,13 +310,13 @@ cdef class Unpacker(object):
310310
311311
Example of streaming deserialize from file-like object::
312312
313-
unpacker = Unpacker(file_like, raw_as_bytes=False)
313+
unpacker = Unpacker(file_like, raw=False)
314314
for o in unpacker:
315315
process(o)
316316
317317
Example of streaming deserialize from socket::
318318
319-
unpacker = Unpacker(raw_as_bytes=False)
319+
unpacker = Unpacker(raw=False)
320320
while True:
321321
buf = sock.recv(1024**2)
322322
if not buf:
@@ -345,7 +345,7 @@ cdef class Unpacker(object):
345345
self.buf = NULL
346346

347347
def __init__(self, file_like=None, Py_ssize_t read_size=0,
348-
bint use_list=True, bint raw_as_bytes=True,
348+
bint use_list=True, bint raw=True,
349349
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
350350
encoding=None, unicode_errors='strict', int max_buffer_size=0,
351351
object ext_hook=ExtType,
@@ -384,7 +384,7 @@ cdef class Unpacker(object):
384384
self.stream_offset = 0
385385

386386
if encoding is not None:
387-
PyErr_WarnEx(PendingDeprecationWarning, "encoding is deprecated, Use raw_as_bytes=False instead.", 1)
387+
PyErr_WarnEx(PendingDeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
388388
if isinstance(encoding, unicode):
389389
self.encoding = encoding.encode('ascii')
390390
elif isinstance(encoding, bytes):
@@ -404,7 +404,7 @@ cdef class Unpacker(object):
404404
cerr = PyBytes_AsString(self.unicode_errors)
405405

406406
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook,
407-
ext_hook, use_list, raw_as_bytes, cenc, cerr,
407+
ext_hook, use_list, raw, cenc, cerr,
408408
max_str_len, max_bin_len, max_array_len,
409409
max_map_len, max_ext_len)
410410

msgpack/fallback.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Unpacker(object):
145145
If true, unpack msgpack array to Python list.
146146
Otherwise, unpack to Python tuple. (default: True)
147147
148-
:param bool raw_as_bytes:
148+
:param bool raw:
149149
If true, unpack msgpack raw to Python bytes (default).
150150
Otherwise, unpack to Python str (or unicode on Python 2) by decoding
151151
with UTF-8 encoding (recommended).
@@ -193,13 +193,13 @@ class Unpacker(object):
193193
194194
example of streaming deserialize from file-like object::
195195
196-
unpacker = Unpacker(file_like, raw_as_bytes=False)
196+
unpacker = Unpacker(file_like, raw=False)
197197
for o in unpacker:
198198
process(o)
199199
200200
example of streaming deserialize from socket::
201201
202-
unpacker = Unpacker(raw_as_bytes=False)
202+
unpacker = Unpacker(raw=False)
203203
while True:
204204
buf = sock.recv(1024**2)
205205
if not buf:
@@ -209,7 +209,7 @@ class Unpacker(object):
209209
process(o)
210210
"""
211211

212-
def __init__(self, file_like=None, read_size=0, use_list=True, raw_as_bytes=True,
212+
def __init__(self, file_like=None, read_size=0, use_list=True, raw=True,
213213
object_hook=None, object_pairs_hook=None, list_hook=None,
214214
encoding=None, unicode_errors=None, max_buffer_size=0,
215215
ext_hook=ExtType,
@@ -221,7 +221,7 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw_as_bytes=True
221221

222222
if encoding is not None:
223223
warnings.warn(
224-
"encoding is deprecated, Use raw_as_bytes=False instead.",
224+
"encoding is deprecated, Use raw=False instead.",
225225
PendingDeprecationWarning)
226226

227227
if unicode_errors is not None:
@@ -257,7 +257,7 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw_as_bytes=True
257257
if read_size > self._max_buffer_size:
258258
raise ValueError("read_size must be smaller than max_buffer_size")
259259
self._read_size = read_size or min(self._max_buffer_size, 16*1024)
260-
self._raw_as_bytes = bool(raw_as_bytes)
260+
self._raw = bool(raw)
261261
self._encoding = encoding
262262
self._unicode_errors = unicode_errors
263263
self._use_list = use_list
@@ -606,7 +606,7 @@ def _unpack(self, execute=EX_CONSTRUCT):
606606
if typ == TYPE_RAW:
607607
if self._encoding is not None:
608608
obj = obj.decode(self._encoding, self._unicode_errors)
609-
elif self._raw_as_bytes:
609+
elif self._raw:
610610
obj = bytes(obj)
611611
else:
612612
obj = obj.decode('utf_8')
@@ -715,7 +715,7 @@ def __init__(self, default=None, encoding=None, unicode_errors=None,
715715
encoding = 'utf_8'
716716
else:
717717
warnings.warn(
718-
"encoding is deprecated, Use raw_as_bytes=False instead.",
718+
"encoding is deprecated, Use raw=False instead.",
719719
PendingDeprecationWarning)
720720

721721
if unicode_errors is None:

msgpack/unpack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
typedef struct unpack_user {
2323
bool use_list;
24-
bool raw_as_bytes;
24+
bool raw;
2525
bool has_pairs_hook;
2626
PyObject *object_hook;
2727
PyObject *list_hook;
@@ -229,7 +229,7 @@ static inline int unpack_callback_raw(unpack_user* u, const char* b, const char*
229229

230230
if (u->encoding) {
231231
py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
232-
} else if (u->raw_as_bytes) {
232+
} else if (u->raw) {
233233
py = PyBytes_FromStringAndSize(p, l);
234234
} else {
235235
py = PyUnicode_DecodeUTF8(p, l, NULL);

test/test_limits.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def test_max_str_len():
3939
d = 'x' * 3
4040
packed = packb(d)
4141

42-
unpacker = Unpacker(max_str_len=3, raw_as_bytes=False)
42+
unpacker = Unpacker(max_str_len=3, raw=False)
4343
unpacker.feed(packed)
4444
assert unpacker.unpack() == d
4545

46-
unpacker = Unpacker(max_str_len=2, raw_as_bytes=False)
46+
unpacker = Unpacker(max_str_len=2, raw=False)
4747
with pytest.raises(UnpackValueError):
4848
unpacker.feed(packed)
4949
unpacker.unpack()

test/test_pack.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def testPack():
3131
def testPackUnicode():
3232
test_data = ["", "abcd", ["defgh"], "Русский текст"]
3333
for td in test_data:
34-
re = unpackb(packb(td), use_list=1, raw_as_bytes=False)
34+
re = unpackb(packb(td), use_list=1, raw=False)
3535
assert re == td
3636
packer = Packer()
3737
data = packer.pack(td)
38-
re = Unpacker(BytesIO(data), raw_as_bytes=False, use_list=1).unpack()
38+
re = Unpacker(BytesIO(data), raw=False, use_list=1).unpack()
3939
assert re == td
4040

4141
def testPackUTF32(): # deprecated
@@ -72,14 +72,14 @@ def testIgnoreUnicodeErrors(): # deprecated
7272

7373
def testStrictUnicodeUnpack():
7474
with raises(UnicodeDecodeError):
75-
unpackb(packb(b'abc\xeddef'), raw_as_bytes=False, use_list=1)
75+
unpackb(packb(b'abc\xeddef'), raw=False, use_list=1)
7676

7777
def testStrictUnicodePack(): # deprecated
7878
with raises(UnicodeEncodeError):
7979
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')
8080

8181
def testIgnoreErrorsPack(): # deprecated
82-
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), raw_as_bytes=False, use_list=1)
82+
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), raw=False, use_list=1)
8383
assert re == "abcdef"
8484

8585
def testDecodeBinary():

test/test_stricttype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def default(o):
1111
return dict(o._asdict())
1212
raise TypeError('Unsupported type %s' % (type(o),))
1313
packed = packb(T(1, 42), strict_types=True, use_bin_type=True, default=default)
14-
unpacked = unpackb(packed, raw_as_bytes=False)
14+
unpacked = unpackb(packed, raw=False)
1515
assert unpacked == {'foo': 1, 'bar': 42}
1616

1717

@@ -32,7 +32,7 @@ def convert(o):
3232
return o
3333

3434
data = packb(t, strict_types=True, use_bin_type=True, default=default)
35-
expected = unpackb(data, raw_as_bytes=False, object_hook=convert)
35+
expected = unpackb(data, raw=False, object_hook=convert)
3636

3737
assert expected == t
3838

@@ -53,10 +53,10 @@ def default(o):
5353
def convert(code, payload):
5454
if code == MSGPACK_EXT_TYPE_TUPLE:
5555
# Unpack and convert to tuple
56-
return tuple(unpackb(payload, raw_as_bytes=False, ext_hook=convert))
56+
return tuple(unpackb(payload, raw=False, ext_hook=convert))
5757
raise ValueError('Unknown Ext code {}'.format(code))
5858

5959
data = packb(t, strict_types=True, use_bin_type=True, default=default)
60-
expected = unpackb(data, raw_as_bytes=False, ext_hook=convert)
60+
expected = unpackb(data, raw=False, ext_hook=convert)
6161

6262
assert expected == t

test/test_unpack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MyUnpacker(Unpacker):
4848

4949
def __init__(self):
5050
super(MyUnpacker, self).__init__(
51-
ext_hook=self._hook, raw_as_bytes=False)
51+
ext_hook=self._hook, raw=False)
5252

5353
def _hook(self, code, data):
5454
if code == 1:

0 commit comments

Comments
 (0)
0