8000 Warn when use_list is not specified. · peter80/msgpack-python@d503788 · GitHub
[go: up one dir, main page]

Skip to content

Commit d503788

Browse files
committed
Warn when use_list is not specified.
Conflicts: test/test_sequnpack.py
1 parent c3da845 commit d503788

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

msgpack/_msgpack.pyx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# coding: utf-8
22
#cython: embedsignature=True
33

4+
import warnings
5+
46
from cpython cimport *
57
cdef extern from "Python.h":
68
ctypedef char* const_char_ptr "const char*"
79
ctypedef char* const_void_ptr "const void*"
810
ctypedef struct PyObject
911
cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
12+
char* __FILE__
13+
int __LINE__
1014

1115
from libc.stdlib cimport *
1216
from libc.string cimport *
@@ -195,7 +199,7 @@ def packb(object o, default=None, encoding='utf-8', unicode_errors='strict', use
195199

196200
cdef extern from "unpack.h":
197201
ctypedef struct msgpack_user:
198-
int use_list
202+
bint use_list
199203
PyObject* object_hook
200204
PyObject* list_hook
201205
char *encoding
@@ -215,7 +219,7 @@ cdef extern from "unpack.h":
215219

216220

217221
def unpackb(object packed, object object_hook=None, object list_hook=None,
218-
bint use_list=0, encoding=None, unicode_errors="strict",
222+
use_list=None, encoding=None, unicode_errors="strict",
219223
):
220224
"""Unpack packed_bytes to object. Returns an unpacked object.
221225
@@ -227,6 +231,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
227231

228232
cdef char* buf
229233
cdef Py_ssize_t buf_len
234+
230235
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
231236

232237
if encoding is None:
@@ -245,7 +250,11 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
245250
err = PyBytes_AsString(berrors)
246251

247252
template_init(&ctx)
248-
ctx.user.use_list = use_list
253+
if use_list is None:
254+
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
255+
ctx.user.use_list = 0
256+
else:
257+
ctx.user.use_list = use_list
249258
ctx.user.object_hook = ctx.user.list_hook = NULL
250259
ctx.user.encoding = <const_char_ptr>enc
251260
ctx.user.unicode_errors = <const_char_ptr>err
@@ -268,12 +277,15 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
268277

269278

270279
def unpack(object stream, object object_hook=None, object list_hook=None,
271-
bint use_list=0, encoding=None, unicode_errors="strict",
280+
use_list=None, encoding=None, unicode_errors="strict",
272281
):
273282
"""Unpack an object from `stream`.
274283
275284
Raises `ValueError` when `stream` has extra bytes.
276285
"""
286+
if use_list is None:
287+
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
288+
use_list = 0
277289
return unpackb(stream.read(), use_list=use_list,
278290
object_hook=object_hook, list_hook=list_hook,
279291
encoding=encoding, unicode_errors=unicode_errors,
@@ -292,7 +304,7 @@ cdef class Unpacker(object):
292304
(default: min(1024**2, max_buffer_size))
293305
294306
If `use_list` is true, msgpack list is deserialized to Python list.
295-
Otherwise, it is deserialized to Python tuple. (default: False)
307+
Otherwise, it is deserialized to Python tuple.
296308
297309
`object_hook` is same to simplejson. If it is not None, it should be callable
298310
and Unpacker calls it when deserializing key-value.
@@ -330,7 +342,6 @@ cdef class Unpacker(object):
330342
cdef object file_like
331343
cdef object file_like_read
332344
cdef Py_ssize_t read_size
333-
cdef bint use_list
334345
cdef object object_hook
335346
cdef object _bencoding
336347
cdef object _berrors
@@ -345,12 +356,15 @@ cdef class Unpacker(object):
345356
free(self.buf)
346357
self.buf = NULL
347358

348-
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
359+
def __init__(self, file_like=None, Py_ssize_t read_size=0, use_list=None,
349360
object object_hook=None, object list_hook=None,
350361
encoding=None, unicode_errors='strict', int max_buffer_size=0,
351362
object object_pairs_hook=None,
352363
):
353-
self.use_list = use_list
364+
if use_list is None:
365+
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
366+
use_list = 0
367+
354368
self.file_like = file_like
355369
if file_like:
356370
self.file_like_read = file_like.read

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
def cythonize(src):
2020
sys.stderr.write("cythonize: %r\n" % (src,))
21-
cython_compiler.compile([src])
21+
cython_compiler.compile([src], emit_linenums=True)
2222

2323
def ensure_source(src):
2424
pyx = os.path.splitext(src)[0] + '.pyx'

test/test_buffer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def test_unpack_buffer():
99
from array import array
1010
buf = array('b')
1111
buf.fromstring(packb(('foo', 'bar')))
12-
obj = unpackb(buf)
13-
assert_equal((b'foo', b'bar'), obj)
12+
obj = unpackb(buf, use_list=1)
13+
assert_equal([b'foo', b'bar'], obj)
1414

1515
if __name__ == '__main__':
1616
main()

test/test_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from nose.tools import *
66
from msgpack import unpackb
77

8-
def check(src, should):
9-
assert_equal(unpackb(src), should)
8+
def check(src, should, use_list=0):
9+
assert_equal(unpackb(src, use_list=use_list), should)
1010

1111
def testSimpleValue():
1212
check(b"\x93\xc0\xc2\xc3",

test/test_pack.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
from io import BytesIO
1313

14-
def check(data):
15-
re = unpackb(packb(data))
14+
def check(data, use_list=False):
15+
re = unpackb(packb(data), use_list=use_list)
1616
assert_equal(re, data)
1717

1818
def testPack():
@@ -34,7 +34,7 @@ def testPackUnicode():
3434
six.u(""), six.u("abcd"), (six.u("defgh"),), six.u("Русский текст"),
3535
]
3636
for td in test_data:
37-
re = unpackb(packb(td, encoding='utf-8'), encoding='utf-8')
37+
re = unpackb(packb(td, encoding='utf-8'), use_list=0, encoding='utf-8')
3838
assert_equal(re, td)
3939
packer = Packer(encoding='utf-8')
4040
data = packer.pack(td)
@@ -46,11 +46,11 @@ def testPackUTF32():
4646
test_data = [
4747
six.u(""),
4848
six.u("abcd"),
49-
(six.u("defgh"),),
49+
[six.u("defgh")],
5050
six.u("Русский текст"),
5151
]
5252
for td in test_data:
53-
re = unpackb(packb(td, encoding='utf-32'), encoding='utf-32')
53+
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
5454
assert_equal(re, td)
5555
except LookupError:
5656
raise SkipTest
@@ -110,7 +110,7 @@ def keys(self):
110110
def test_odict():
111111
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
112112
od = odict(seq)
113-
assert_equal(unpackb(packb(od)), dict(seq))
113+
assert_equal(unpackb(packb(od), use_list=1), dict(seq))
114114
# After object_pairs_hook is implemented.
115115
#def pair_hook(seq):
116116
# return seq

test/test_seq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_exceeding_unpacker_read_size():
3434
f = io.BytesIO(dumpf.getvalue())
3535
dumpf.close()
3636

37-
unpacker = msgpack.Unpacker(f, read_size=read_size)
37+
unpacker = msgpack.Unpacker(f, read_size=read_size, use_list=1)
3838

3939
read_count = 0
4040
for idx, o in enumerate(unpacker):

test/test_sequnpack.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import nose
66

77
def test_foobar():
8-
unpacker = Unpacker(read_size=3)
8+
unpacker = Unpacker(read_size=3, use_list=1)
99
unpacker.feed(b'foobar')
1010
assert unpacker.unpack() == ord(b'f')
1111
assert unpacker.unpack() == ord(b'o')
@@ -28,10 +28,9 @@ def test_foobar():
2828
k += 1
2929
assert k == len(b'foobar')
3030

31-
3231
def test_maxbuffersize():
3332
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
34-
unpacker = Unpacker(read_size=3, max_buffer_size=3)
33+
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
3534
unpacker.feed(b'fo')
3635
nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob')
3736
unpacker.feed(b'o')

0 commit comments

Comments
 (0)
0