refactoring.

This commit is contained in:
INADA Naoki 2013-10-17 08:44:25 +09:00
parent da12e177a3
commit 171c538113
3 changed files with 21 additions and 45 deletions

View file

@ -4,14 +4,32 @@ from msgpack.exceptions import *
import os import os
if os.environ.get('MSGPACK_PUREPYTHON'): 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: else:
try: try:
from msgpack._packer import pack, packb, Packer from msgpack._packer import Packer
from msgpack._unpacker import unpack, unpackb, Unpacker from msgpack._unpacker import unpack, unpackb, Unpacker
except ImportError: except ImportError:
from msgpack.fallback import pack, packb, Packer, unpack, unpackb, Unpacker 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. # alias for compatibility to simplejson/marshal/pickle.
load = unpack load = unpack
loads = unpackb loads = unpackb

View file

@ -13,6 +13,7 @@ cdef extern from "pack.h":
char* buf char* buf
size_t length size_t length
size_t buf_size size_t buf_size
bint use_bin_type
int msgpack_pack_int(msgpack_packer* pk, int d) int msgpack_pack_int(msgpack_packer* pk, int d)
int msgpack_pack_nil(msgpack_packer* pk) int msgpack_pack_nil(msgpack_packer* pk)
@ -68,7 +69,6 @@ cdef class Packer(object):
cdef char *encoding cdef char *encoding
cdef char *unicode_errors cdef char *unicode_errors
cdef bool use_float cdef bool use_float
cdef bool use_bin_type
cdef bint autoreset cdef bint autoreset
def __cinit__(self): def __cinit__(self):
@ -254,28 +254,3 @@ cdef class Packer(object):
def bytes(self): def bytes(self):
"""Return buffer content.""" """Return buffer content."""
return PyBytes_FromStringAndSize(self.pk.buf, self.pk.length) 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)

View file

@ -60,23 +60,6 @@ TYPE_RAW = 3
DEFAULT_RECURSE_LIMIT=511 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): def unpack(stream, **kwargs):
""" """
Unpack an object from `stream`. Unpack an object from `stream`.