mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 18:59:59 +00:00
refactoring.
This commit is contained in:
parent
da12e177a3
commit
171c538113
3 changed files with 21 additions and 45 deletions
|
|
@ -4,14 +4,32 @@ from msgpack.exceptions import *
|
|||
|
||||
import os
|
||||
if os.environ.get('MSGPACK_PUREPYTHON'):
|
||||
from msgpack.fallback import pack, packb, Packer, unpack, unpackb, Unpacker
|
||||
from msgpack.fallback import Packer, unpack, unpackb, Unpacker
|
||||
else:
|
||||
try:
|
||||
from msgpack._packer import pack, packb, Packer
|
||||
from msgpack._packer import Packer
|
||||
from msgpack._unpacker import unpack, unpackb, Unpacker
|
||||
except ImportError:
|
||||
from msgpack.fallback import pack, packb, Packer, unpack, unpackb, Unpacker
|
||||
|
||||
|
||||
def pack(o, stream, **kwargs):
|
||||
"""
|
||||
Pack object `o` and write it to `stream`
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
packer = Packer(**kwargs)
|
||||
stream.write(packer.pack(o))
|
||||
|
||||
def packb(o, **kwargs):
|
||||
"""
|
||||
Pack object `o` and return packed bytes
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
return Packer(**kwargs).pack(o)
|
||||
|
||||
# alias for compatibility to simplejson/marshal/pickle.
|
||||
load = unpack
|
||||
loads = unpackb
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ cdef extern from "pack.h":
|
|||
char* buf
|
||||
size_t length
|
||||
size_t buf_size
|
||||
bint use_bin_type
|
||||
|
||||
int msgpack_pack_int(msgpack_packer* pk, int d)
|
||||
int msgpack_pack_nil(msgpack_packer* pk)
|
||||
|
|
@ -68,7 +69,6 @@ cdef class Packer(object):
|
|||
cdef char *encoding
|
||||
cdef char *unicode_errors
|
||||
cdef bool use_float
|
||||
cdef bool use_bin_type
|
||||
cdef bint autoreset
|
||||
|
||||
def __cinit__(self):
|
||||
|
|
@ -254,28 +254,3 @@ cdef class Packer(object):
|
|||
def bytes(self):
|
||||
"""Return buffer content."""
|
||||
return PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
|
||||
|
||||
|
||||
def pack(object o, object stream,
|
||||
default=None, str encoding='utf-8', str unicode_errors='strict',
|
||||
bint use_single_float=False, bint use_bin_type=False):
|
||||
"""
|
||||
pack an object `o` and write it to stream)
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
|
||||
use_single_float=use_single_float, use_bin_type=use_bin_type)
|
||||
stream.write(packer.pack(o))
|
||||
|
||||
def packb(object o,
|
||||
default=None, str encoding='utf-8', str unicode_errors='strict',
|
||||
bint use_single_float=False, bint use_bin_type=False):
|
||||
"""
|
||||
pack o and return packed bytes
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
|
||||
use_single_float=use_single_float, use_bin_type=use_bin_type)
|
||||
return packer.pack(o)
|
||||
|
|
|
|||
|
|
@ -60,23 +60,6 @@ TYPE_RAW = 3
|
|||
|
||||
DEFAULT_RECURSE_LIMIT=511
|
||||
|
||||
def pack(o, stream, **kwargs):
|
||||
"""
|
||||
Pack object `o` and write it to `stream`
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
packer = Packer(**kwargs)
|
||||
stream.write(packer.pack(o))
|
||||
|
||||
def packb(o, **kwargs):
|
||||
"""
|
||||
Pack object `o` and return packed bytes
|
||||
|
||||
See :class:`Packer` for options.
|
||||
"""
|
||||
return Packer(**kwargs).pack(o)
|
||||
|
||||
def unpack(stream, **kwargs):
|
||||
"""
|
||||
Unpack an object from `stream`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue