File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -359,6 +359,24 @@ cdef class Unpacker(object):
359359 """
360360 return self ._unpack(unpack_construct, write_bytes)
361361
362+ def unpack_one (self , object write_bytes = None ):
363+ """
364+ unpack one object
365+
366+ If write_bytes is not None, it will be called with parts of the raw
367+ message as it is unpacked.
368+
369+ Raises `UnpackValueError` if there are no more bytes to unpack.
370+ Raises ``ExtraData`` if there are still bytes left after the unpacking.
371+ """
372+ try :
373+ result = self .unpack()
374+ except OutOfData:
375+ raise UnpackValueError(" Data is not enough" )
376+ if self .buf_head < self .buf_tail:
377+ raise ExtraData(result, self .buf[self .buf_head:])
378+ return result
379+
362380 def skip (self , object write_bytes = None ):
363381 """
364382 read and ignore one object, returning None
Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22# coding: utf-8
33
4+ import py
45import six
56from msgpack import Unpacker , BufferFull
6- from msgpack .exceptions import OutOfData
7+ from msgpack .exceptions import OutOfData , ExtraData , UnpackValueError
78from pytest import raises
89
910
@@ -85,3 +86,15 @@ def test_readbytes():
8586 assert unpacker .unpack () == ord (b'a' )
8687 assert unpacker .unpack () == ord (b'r' )
8788
89+ def test_unpack_one ():
90+ unpacker = Unpacker ()
91+ unpacker .feed ('\xda \x00 \x03 abc' )
92+ assert unpacker .unpack_one () == 'abc'
93+ #
94+ unpacker = Unpacker ()
95+ unpacker .feed ('\xda \x00 \x03 abcd' )
96+ py .test .raises (ExtraData , "unpacker.unpack_one()" )
97+ #
98+ unpacker = Unpacker ()
99+ unpacker .feed ('\xda \x00 \x03 ab' )
100+ py .test .raises (UnpackValueError , "unpacker.unpack_one()" )
You can’t perform that action at this time.
0 commit comments