@@ -38,8 +38,12 @@ cdef extern from "pack.h":
3838cdef int DEFAULT_RECURSE_LIMIT= 511
3939
4040
41- class BufferFull (Exception ):
42- pass
41+ from msgpack.exceptions import (
42+ BufferFull,
43+ OutOfData,
44+ UnpackValueError,
45+ ExtraData,
46+ )
4347
4448
4549cdef class Packer(object ):
@@ -102,7 +106,7 @@ cdef class Packer(object):
102106 cdef dict d
103107
104108 if nest_limit < 0 :
105- raise ValueError ( " Too deep ." )
109+ raise UnpackValueError( " recursion limit exceeded ." )
106110
107111 if o is None :
108112 ret = msgpack_pack_nil(& self .pk)
@@ -174,7 +178,9 @@ cdef class Packer(object):
174178 cpdef pack(self , object obj):
175179 cdef int ret
176180 ret = self ._pack(obj, DEFAULT_RECURSE_LIMIT)
177- if ret:
181+ if ret == - 1 :
182+ raise MemoryError
183+ elif ret: # should not happen.
178184 raise TypeError
179185 buf = PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
180186 self .pk.length = 0
@@ -296,7 +302,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
296302 if ret == 1 :
297303 obj = template_data(& ctx)
298304 if off < buf_len:
299- raise ValueError ( " Extra data. " )
305+ raise ExtraData(obj, PyBytes_FromStringAndSize(buf + off, buf_len - off) )
300306 return obj
301307 else :
302308 return None
@@ -421,11 +427,12 @@ cdef class Unpacker(object):
421427 init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
422428
423429 def feed (self , object next_bytes ):
430+ """ Append `next_bytes` to internal buffer."""
424431 cdef char * buf
425432 cdef Py_ssize_t buf_len
426433 if self .file_like is not None :
427434 raise AssertionError (
428- " unpacker.feed() is not be able to use with`file_like`." )
435+ " unpacker.feed() is not be able to use with `file_like`." )
429436 PyObject_AsReadBuffer(next_bytes, < const_void_ptr* > & buf, & buf_len)
430437 self .append_buffer(buf, buf_len)
431438
@@ -479,7 +486,7 @@ cdef class Unpacker(object):
479486 else :
480487 self .file_like = None
481488
482- cdef object _unpack(self , execute_fn execute, object write_bytes):
489+ cdef object _unpack(self , execute_fn execute, object write_bytes, bint iter = 0 ):
483490 cdef int ret
484491 cdef object obj
485492 cdef size_t prev_head
@@ -497,7 +504,10 @@ cdef class Unpacker(object):
497504 if self .file_like is not None :
498505 self .read_from_file()
499506 continue
500- raise StopIteration (" No more data to unpack." )
507+ if iter :
508+ raise StopIteration (" No more data to unpack." )
509+ else :
510+ raise OutOfData(" No more data to unpack." )
501511 else :
502512 raise ValueError (" Unpack failed: error = %d " % (ret,))
503513
@@ -515,31 +525,45 @@ cdef class Unpacker(object):
515525 """
516526 unpack one object
517527
518- If write_bytes is not None, it will be called with parts of the raw message as it is unpacked.
528+ If write_bytes is not None, it will be called with parts of the raw
529+ message as it is unpacked.
530+
531+ Raises `OutOfData` when there are no more bytes to unpack.
519532 """
520533 return self ._unpack(template_construct, write_bytes)
521534
522535 def skip (self , object write_bytes = None ):
523536 """
524537 read and ignore one object, returning None
525538
526- If write_bytes is not None, it will be called with parts of the raw message as it is unpacked.
539+ If write_bytes is not None, it will be called with parts of the raw
540+ message as it is unpacked.
541+
542+ Raises `OutOfData` when there are no more bytes to unpack.
527543 """
528544 return self ._unpack(template_skip, write_bytes)
529545
530546 def read_array_header (self , object write_bytes = None ):
531- """ assuming the next object is an array, return its size n, such that the next n unpack() calls will iterate over its contents."""
547+ """ assuming the next object is an array, return its size n, such that
548+ the next n unpack() calls will iterate over its contents.
549+
550+ Raises `OutOfData` when there are no more bytes to unpack.
551+ """
532552 return self ._unpack(read_array_header, write_bytes)
533553
534554 def read_map_header (self , object write_bytes = None ):
535- """ assuming the next object is a map, return its size n, such that the next n * 2 unpack() calls will iterate over its key-value pairs."""
555+ """ assuming the next object is a map, return its size n, such that the
556+ next n * 2 unpack() calls will iterate over its key-value pairs.
557+
558+ Raises `OutOfData` when there are no more bytes to unpack.
559+ """
536560 return self ._unpack(read_map_header, write_bytes)
537561
538562 def __iter__ (self ):
539563 return self
540564
541565 def __next__ (self ):
542- return self ._unpack(template_construct, None )
566+ return self ._unpack(template_construct, None , 1 )
543567
544568 # for debug.
545569 # def _buf(self):
0 commit comments