Skip to content

Commit 171c538

Browse files
committed
refactoring.
1 parent da12e17 commit 171c538

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

msgpack/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@
44

55
import os
66
if os.environ.get('MSGPACK_PUREPYTHON'):
7-
from msgpack.fallback import pack, packb, Packer, unpack, unpackb, Unpacker
7+
from msgpack.fallback import Packer, unpack, unpackb, Unpacker
88
else:
99
try:
10-
from msgpack._packer import pack, packb, Packer
10+
from msgpack._packer import Packer
1111
from msgpack._unpacker import unpack, unpackb, Unpacker
1212
except ImportError:
1313
from msgpack.fallback import pack, packb, Packer, unpack, unpackb, Unpacker
1414

15+
16+
def pack(o, stream, **kwargs):
17+
"""
18+
Pack object `o` and write it to `stream`
19+
20+
See :class:`Packer` for options.
21+
"""
22+
packer = Packer(**kwargs)
23+
stream.write(packer.pack(o))
24+
25+
def packb(o, **kwargs):
26+
"""
27+
Pack object `o` and return packed bytes
28+
29+
See :class:`Packer` for options.
30+
"""
31+
return Packer(**kwargs).pack(o)
32+
1533
# alias for compatibility to simplejson/marshal/pickle.
1634
load = unpack
1735
loads = unpackb

msgpack/_packer.pyx

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cdef extern from "pack.h":
1313
char* buf
1414
size_t length
1515
size_t buf_size
16+
bint use_bin_type
1617

1718
int msgpack_pack_int(msgpack_packer* pk, int d)
1819
int msgpack_pack_nil(msgpack_packer* pk)
@@ -68,7 +69,6 @@ cdef class Packer(object):
6869
cdef char *encoding
6970
cdef char *unicode_errors
7071
cdef bool use_float
71-
cdef bool use_bin_type
7272
cdef bint autoreset
7373

7474
def __cinit__(self):
@@ -254,28 +254,3 @@ cdef class Packer(object):
254254
def bytes(self):
255255
"""Return buffer content."""
256256
return PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
257-
258-
259-
def pack(object o, object stream,
260-
default=None, str encoding='utf-8', str unicode_errors='strict',
261-
bint use_single_float=False, bint use_bin_type=False):
262-
"""
263-
pack an object `o` and write it to stream)
264-
265-
See :class:`Packer` for options.
266-
"""
267-
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
268-
use_single_float=use_single_float, use_bin_type=use_bin_type)
269-
stream.write(packer.pack(o))
270-
271-
def packb(object o,
272-
default=None, str encoding='utf-8', str unicode_errors='strict',
273-
bint use_single_float=False, bint use_bin_type=False):
274-
"""
275-
pack o and return packed bytes
276-
277-
See :class:`Packer` for options.
278-
"""
279-
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
280-
use_single_float=use_single_float, use_bin_type=use_bin_type)
281-
return packer.pack(o)

msgpack/fallback.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,6 @@ def getvalue(self):
6060

6161
DEFAULT_RECURSE_LIMIT=511
6262

63-
def pack(o, stream, **kwargs):
64-
"""
65-
Pack object `o` and write it to `stream`
66-
67-
See :class:`Packer` for options.
68-
"""
69-
packer = Packer(**kwargs)
70-
stream.write(packer.pack(o))
71-
72-
def packb(o, **kwargs):
73-
"""
74-
Pack object `o` and return packed bytes
75-
76-
See :class:`Packer` for options.
77-
"""
78-
return Packer(**kwargs).pack(o)
79-
8063
def unpack(stream, **kwargs):
8164
"""
8265
Unpack an object from `stream`.

0 commit comments

Comments
 (0)