mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-31 17:40:54 +00:00
Warn when use_list is not specified.
This commit is contained in:
parent
96ed236c1d
commit
60df5eadaf
7 changed files with 37 additions and 23 deletions
|
|
@ -1,12 +1,16 @@
|
|||
# coding: utf-8
|
||||
#cython: embedsignature=True
|
||||
|
||||
import warnings
|
||||
|
||||
from cpython cimport *
|
||||
cdef extern from "Python.h":
|
||||
ctypedef char* const_char_ptr "const char*"
|
||||
ctypedef char* const_void_ptr "const void*"
|
||||
ctypedef struct PyObject
|
||||
cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
|
||||
char* __FILE__
|
||||
int __LINE__
|
||||
|
||||
from libc.stdlib cimport *
|
||||
from libc.string cimport *
|
||||
|
|
@ -195,7 +199,7 @@ def packb(object o, default=None, encoding='utf-8', unicode_errors='strict', use
|
|||
|
||||
cdef extern from "unpack.h":
|
||||
ctypedef struct msgpack_user:
|
||||
int use_list
|
||||
bint use_list
|
||||
PyObject* object_hook
|
||||
PyObject* list_hook
|
||||
char *encoding
|
||||
|
|
@ -215,7 +219,7 @@ cdef extern from "unpack.h":
|
|||
|
||||
|
||||
def unpackb(object packed, object object_hook=None, object list_hook=None,
|
||||
bint use_list=0, encoding=None, unicode_errors="strict",
|
||||
use_list=None, encoding=None, unicode_errors="strict",
|
||||
):
|
||||
"""Unpack packed_bytes to object. Returns an unpacked object.
|
||||
|
||||
|
|
@ -227,6 +231,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
|||
|
||||
cdef char* buf
|
||||
cdef Py_ssize_t buf_len
|
||||
|
||||
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
|
||||
|
||||
if encoding is None:
|
||||
|
|
@ -245,7 +250,11 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
|||
err = PyBytes_AsString(berrors)
|
||||
|
||||
template_init(&ctx)
|
||||
ctx.user.use_list = use_list
|
||||
if use_list is None:
|
||||
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
|
||||
ctx.user.use_list = 0
|
||||
else:
|
||||
ctx.user.use_list = use_list
|
||||
ctx.user.object_hook = ctx.user.list_hook = NULL
|
||||
ctx.user.encoding = <const_char_ptr>enc
|
||||
ctx.user.unicode_errors = <const_char_ptr>err
|
||||
|
|
@ -268,12 +277,15 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
|||
|
||||
|
||||
def unpack(object stream, object object_hook=None, object list_hook=None,
|
||||
bint use_list=0, encoding=None, unicode_errors="strict",
|
||||
use_list=None, encoding=None, unicode_errors="strict",
|
||||
):
|
||||
"""Unpack an object from `stream`.
|
||||
|
||||
Raises `ValueError` when `stream` has extra bytes.
|
||||
"""
|
||||
if use_list is None:
|
||||
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
|
||||
use_list = 0
|
||||
return unpackb(stream.read(), use_list=use_list,
|
||||
object_hook=object_hook, list_hook=list_hook,
|
||||
encoding=encoding, unicode_errors=unicode_errors,
|
||||
|
|
@ -292,7 +304,7 @@ cdef class Unpacker(object):
|
|||
(default: min(1024**2, max_buffer_size))
|
||||
|
||||
If `use_list` is true, msgpack list is deserialized to Python list.
|
||||
Otherwise, it is deserialized to Python tuple. (default: False)
|
||||
Otherwise, it is deserialized to Python tuple.
|
||||
|
||||
`object_hook` is same to simplejson. If it is not None, it should be callable
|
||||
and Unpacker calls it when deserializing key-value.
|
||||
|
|
@ -330,7 +342,6 @@ cdef class Unpacker(object):
|
|||
cdef object file_like
|
||||
cdef object file_like_read
|
||||
cdef Py_ssize_t read_size
|
||||
cdef bint use_list
|
||||
cdef object object_hook
|
||||
cdef object _bencoding
|
||||
cdef object _berrors
|
||||
|
|
@ -345,12 +356,15 @@ cdef class Unpacker(object):
|
|||
free(self.buf)
|
||||
self.buf = NULL
|
||||
|
||||
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
|
||||
def __init__(self, file_like=None, Py_ssize_t read_size=0, use_list=None,
|
||||
object object_hook=None, object list_hook=None,
|
||||
encoding=None, unicode_errors='strict', int max_buffer_size=0,
|
||||
object object_pairs_hook=None,
|
||||
):
|
||||
self.use_list = use_list
|
||||
if use_list is None:
|
||||
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
|
||||
use_list = 0
|
||||
|
||||
self.file_like = file_like
|
||||
if file_like:
|
||||
self.file_like_read = file_like.read
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -18,7 +18,7 @@ except ImportError:
|
|||
|
||||
def cythonize(src):
|
||||
sys.stderr.write("cythonize: %r\n" % (src,))
|
||||
cython_compiler.compile([src])
|
||||
cython_compiler.compile([src], emit_linenums=True)
|
||||
|
||||
def ensure_source(src):
|
||||
pyx = os.path.splitext(src)[0] + '.pyx'
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ def test_unpack_buffer():
|
|||
from array import array
|
||||
buf = array('b')
|
||||
buf.fromstring(packb(('foo', 'bar')))
|
||||
obj = unpackb(buf)
|
||||
assert_equal((b'foo', b'bar'), obj)
|
||||
obj = unpackb(buf, use_list=1)
|
||||
assert_equal([b'foo', b'bar'], obj)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ from nose import main
|
|||
from nose.tools import *
|
||||
from msgpack import unpackb
|
||||
|
||||
def check(src, should):
|
||||
assert_equal(unpackb(src), should)
|
||||
def check(src, should, use_list=0):
|
||||
assert_equal(unpackb(src, use_list=use_list), should)
|
||||
|
||||
def testSimpleValue():
|
||||
check(b"\x93\xc0\xc2\xc3",
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ from msgpack import packb, unpackb, Unpacker, Packer
|
|||
|
||||
from io import BytesIO
|
||||
|
||||
def check(data):
|
||||
re = unpackb(packb(data))
|
||||
def check(data, use_list=False):
|
||||
re = unpackb(packb(data), use_list=use_list)
|
||||
assert_equal(re, data)
|
||||
|
||||
def testPack():
|
||||
|
|
@ -34,7 +34,7 @@ def testPackUnicode():
|
|||
six.u(""), six.u("abcd"), (six.u("defgh"),), six.u("Русский текст"),
|
||||
]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-8'), encoding='utf-8')
|
||||
re = unpackb(packb(td, encoding='utf-8'), use_list=0, encoding='utf-8')
|
||||
assert_equal(re, td)
|
||||
packer = Packer(encoding='utf-8')
|
||||
data = packer.pack(td)
|
||||
|
|
@ -46,11 +46,11 @@ def testPackUTF32():
|
|||
test_data = [
|
||||
six.u(""),
|
||||
six.u("abcd"),
|
||||
(six.u("defgh"),),
|
||||
[six.u("defgh")],
|
||||
six.u("Русский текст"),
|
||||
]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-32'), encoding='utf-32')
|
||||
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
|
||||
assert_equal(re, td)
|
||||
except LookupError:
|
||||
raise SkipTest
|
||||
|
|
@ -110,7 +110,7 @@ class odict(dict):
|
|||
def test_odict():
|
||||
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
|
||||
od = odict(seq)
|
||||
assert_equal(unpackb(packb(od)), dict(seq))
|
||||
assert_equal(unpackb(packb(od), use_list=1), dict(seq))
|
||||
# After object_pairs_hook is implemented.
|
||||
#def pair_hook(seq):
|
||||
# return seq
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ def test_exceeding_unpacker_read_size():
|
|||
f = io.BytesIO(dumpf.getvalue())
|
||||
dumpf.close()
|
||||
|
||||
unpacker = msgpack.Unpacker(f, read_size=read_size)
|
||||
unpacker = msgpack.Unpacker(f, read_size=read_size, use_list=1)
|
||||
|
||||
read_count = 0
|
||||
for idx, o in enumerate(unpacker):
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from msgpack import Unpacker, BufferFull
|
|||
import nose
|
||||
|
||||
def test_foobar():
|
||||
unpacker = Unpacker(read_size=3)
|
||||
unpacker = Unpacker(read_size=3, use_list=1)
|
||||
unpacker.feed(b'foobar')
|
||||
assert unpacker.unpack() == ord(b'f')
|
||||
assert unpacker.unpack() == ord(b'o')
|
||||
|
|
@ -29,7 +29,7 @@ def test_foobar():
|
|||
assert k == len(b'foobar')
|
||||
|
||||
def test_foobar_skip():
|
||||
unpacker = Unpacker(read_size=3)
|
||||
unpacker = Unpacker(read_size=3, use_list=1)
|
||||
unpacker.feed(b'foobar')
|
||||
assert unpacker.unpack() == ord(b'f')
|
||||
unpacker.skip()
|
||||
|
|
@ -45,7 +45,7 @@ def test_foobar_skip():
|
|||
|
||||
def test_maxbuffersize():
|
||||
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
|
||||
unpacker = Unpacker(read_size=3, max_buffer_size=3)
|
||||
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
|
||||
unpacker.feed(b'fo')
|
||||
nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob')
|
||||
unpacker.feed(b'o')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue