@@ -62,6 +62,44 @@ def getvalue(self):
6262
6363DEFAULT_RECURSE_LIMIT = 511
6464
65+ def pack (o , stream , ** kwargs ):
66+ """
67+ Pack object `o` and write it to `stream`
68+
69+ See :class:`Packer` for options.
70+ """
71+ packer = Packer (** kwargs )
72+ stream .write (packer .pack (o ))
73+
74+ def packb (o , ** kwargs ):
75+ """
76+ Pack object `o` and return packed bytes
77+
78+ See :class:`Packer` for options.
79+ """
80+ return Packer (** kwargs ).pack (o )
81+
82+ def unpack (stream , ** kwargs ):
83+ """
84+ Unpack an object from `stream`.
85+
86+ Raises `ExtraData` when `packed` contains extra bytes.
87+ See :class:`Unpacker` for options.
88+ """
89+ unpacker = Unpacker (stream , ** kwargs )
90+ return unpacker .unpack_one ()
91+
92+ def unpackb (packed , ** kwargs ):
93+ """
94+ Unpack an object from `packed`.
95+
96+ Raises `ExtraData` when `packed` contains extra bytes.
97+ See :class:`Unpacker` for options.
98+ """
99+ unpacker = Unpacker (None , ** kwargs )
100+ unpacker .feed (packed )
101+ return unpacker .unpack_one ()
102+
65103class Unpacker (object ):
66104 """
67105 Streaming unpacker.
@@ -149,6 +187,15 @@ def __init__(self, file_like=None, read_size=0, use_list=True,
149187 raise ValueError ("object_pairs_hook and object_hook are mutually "
150188 "exclusive" )
151189
190+ def unpack_one (self ):
191+ try :
192+ ret = self ._fb_unpack ()
193+ except OutOfData :
194+ raise UnpackValueError ("Data is not enough." )
195+ if self ._fb_got_extradata ():
196+ raise ExtraData (ret , self ._fb_get_extradata ())
197+ return ret
198+
152199 def feed (self , next_bytes ):
153200 if isinstance (next_bytes , array .array ):
154201 next_bytes = next_bytes .tostring ()
@@ -579,51 +626,3 @@ def bytes(self):
579626 def reset (self ):
580627 self ._buffer = StringIO ()
581628
582-
583- def pack (o , stream , Packer = Packer , ** kwargs ):
584- """
585- Pack object `o` and write it to `stream`
586-
587- See :class:`Packer` for options.
588- """
589- packer = Packer (** kwargs )
590- stream .write (packer .pack (o ))
591-
592- def packb (o , Packer = Packer , ** kwargs ):
593- """
594- Pack object `o` and return packed bytes
595-
596- See :class:`Packer` for options.
597- """
598- return Packer (** kwargs ).pack (o )
599-
600- def unpack (stream , Unpacker = Unpacker , ** kwargs ):
601- """
602- Unpack an object from `stream`.
603-
604- Raises `ExtraData` when `packed` contains extra bytes.
605- See :class:`Unpacker` for options.
606- """
607- unpacker = Unpacker (stream , ** kwargs )
608- ret = unpacker ._fb_unpack ()
609- if unpacker ._fb_got_extradata ():
610- raise ExtraData (ret , unpacker ._fb_get_extradata ())
611- return ret
612-
613- def unpackb (packed , Unpacker = Unpacker , ** kwargs ):
614- """
615- Unpack an object from `packed`.
616-
617- Raises `ExtraData` when `packed` contains extra bytes.
618- See :class:`Unpacker` for options.
619- """
620- unpacker = Unpacker (None , ** kwargs )
621- unpacker .feed (packed )
622- try :
623- ret = unpacker ._fb_unpack ()
624- except OutOfData :
625- raise UnpackValueError ("Data is not enough." )
626- if unpacker ._fb_got_extradata ():
627- raise ExtraData (ret , unpacker ._fb_get_extradata ())
628- return ret
629-
0 commit comments