2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# coding: utf-8
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from cpython cimport *
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								cdef extern from "Python.h":
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ctypedef char* const_char_ptr "const char*"
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctypedef char* const_void_ptr "const void*"
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctypedef struct PyObject
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from libc.stdlib cimport *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								from libc.string cimport *
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:23:56 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import gc
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								_gc_disable = gc.disable
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								_gc_enable = gc.enable
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cdef extern from "pack.h":
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    struct msgpack_packer:
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        char* buf
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        size_t length
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        size_t buf_size
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-29 10:09:04 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_int(msgpack_packer* pk, int d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_nil(msgpack_packer* pk)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_true(msgpack_packer* pk)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_false(msgpack_packer* pk)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_long(msgpack_packer* pk, long d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_long_long(msgpack_packer* pk, long long d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_double(msgpack_packer* pk, double d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_array(msgpack_packer* pk, size_t l)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_map(msgpack_packer* pk, size_t l)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_raw(msgpack_packer* pk, size_t l)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    int msgpack_pack_raw_body(msgpack_packer* pk, char* body, size_t l)
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cdef int DEFAULT_RECURSE_LIMIT=511
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-28 21:24:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cdef class Packer(object):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """MessagePack Packer
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    usage:
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        packer = Packer()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        astream.write(packer.pack(a))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        astream.write(packer.pack(b))
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef msgpack_packer pk
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef object _default
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef object _bencoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef object _berrors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char *encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char *unicode_errors
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __cinit__(self):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef int buf_size = 1024*1024
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.pk.buf = <char*> malloc(buf_size);
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 20:47:23 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self.pk.buf == NULL:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise MemoryError("Unable to allocate internal buffer.")
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.pk.buf_size = buf_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.pk.length = 0
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, default=None, encoding='utf-8', unicode_errors='strict'):
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if default is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not PyCallable_Check(default):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                raise TypeError("default must be a callable.")
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._default = default
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if encoding is None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.encoding = NULL
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.unicode_errors = NULL
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if isinstance(encoding, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._bencoding = encoding.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._bencoding = encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.encoding = PyBytes_AsString(self._bencoding)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if isinstance(unicode_errors, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._berrors = unicode_errors.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._berrors = unicode_errors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.unicode_errors = PyBytes_AsString(self._berrors)
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __dealloc__(self):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        free(self.pk.buf);
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef int _pack(self, object o, int nest_limit=DEFAULT_RECURSE_LIMIT) except -1:
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-28 21:24:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef long long llval
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-29 10:09:04 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef unsigned long long ullval
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-28 21:24:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef long longval
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef double fval
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef char* rawval
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef int ret
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 02:16:28 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cdef dict d
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:49:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if nest_limit < 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise ValueError("Too deep.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if o is None:
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_nil(&self.pk)
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 01:29:57 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif isinstance(o, bool):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if o:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_true(&self.pk)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_false(&self.pk)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-30 23:03:33 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyLong_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-29 10:09:04 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if o > 0:
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                ullval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_unsigned_long_long(&self.pk, ullval)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-29 10:09:04 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                llval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_long_long(&self.pk, llval)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-30 23:03:33 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyInt_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-28 21:24:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            longval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_long(&self.pk, longval)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-30 23:03:33 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyFloat_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-16 01:58:07 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            fval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_double(&self.pk, fval)
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 01:29:57 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyBytes_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            rawval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_raw(&self.pk, len(o))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if ret == 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_raw_body(&self.pk, rawval, len(o))
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-30 23:03:33 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyUnicode_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if not self.encoding:
							 | 
						
					
						
							
								
									
										
										
										
											2011-05-31 15:40:11 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                raise TypeError("Can't encode utf-8 no encoding is specified")
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            rawval = o
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_raw(&self.pk, len(o))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if ret == 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                ret = msgpack_pack_raw_body(&self.pk, rawval, len(o))
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 02:16:28 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PyDict_Check(o):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            d = o
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_map(&self.pk, len(d))
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if ret == 0:
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 02:16:28 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                for k,v in d.items():
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    ret = self._pack(k, nest_limit-1)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    if ret != 0: break
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    ret = self._pack(v, nest_limit-1)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    if ret != 0: break
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-28 21:24:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif PySequence_Check(o):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = msgpack_pack_array(&self.pk, len(o))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if ret == 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                for v in o:
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    ret = self._pack(v, nest_limit-1)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    if ret != 0: break
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:22:41 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif self._default:
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            o = self._default(o)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:22:41 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = self._pack(o, nest_limit-1)
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-22 14:31:20 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            raise TypeError("can't serialize %r" % (o,))
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return ret
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def pack(self, object obj):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef int ret
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 07:27:10 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise TypeError
							 | 
						
					
						
							
								
									
										
										
										
											2010-09-02 01:29:57 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.pk.length = 0
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return buf
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 00:57:46 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def pack(object o, object stream, default=None, encoding='utf-8', unicode_errors='strict'):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-12 20:02:21 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """pack an object `o` and write it to stream)."""
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    stream.write(packer.pack(o))
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def packb(object o, default=None, encoding='utf-8', unicode_errors='strict'):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """pack o and return packed bytes."""
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return packer.pack(o)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:17:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								dumps = packs = packb
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cdef extern from "unpack.h":
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 11:13:47 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctypedef struct msgpack_user:
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 15:19:18 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        int use_list
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        PyObject* object_hook
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        PyObject* list_hook
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        char *encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        char *unicode_errors
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 11:13:47 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctypedef struct template_context:
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 11:13:47 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        msgpack_user user
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        PyObject* obj
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        size_t count
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        unsigned int ct
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        PyObject* key
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    int template_execute(template_context* ctx, const_char_ptr data,
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                         size_t len, size_t* off) except -1
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    void template_init(template_context* ctx)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 12:46:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    object template_data(template_context* ctx)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def unpackb(object packed, object object_hook=None, object list_hook=None, bint use_list=0, encoding=None, unicode_errors="strict"):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-12 20:02:21 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """Unpack packed_bytes to object. Returns an unpacked object."""
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef template_context ctx
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef size_t off = 0
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef int ret
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char* buf
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef Py_ssize_t buf_len
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if encoding is None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        enc = NULL
							 | 
						
					
						
							
								
									
										
										
										
											2011-08-22 01:52:45 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        err = NULL
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if isinstance(encoding, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            bencoding = encoding.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            bencoding = encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if isinstance(unicode_errors, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            berrors = unicode_errors.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            berrors = unicode_errors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        enc = PyBytes_AsString(bencoding)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        err = PyBytes_AsString(berrors)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    template_init(&ctx)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-28 18:59:05 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctx.user.use_list = use_list
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctx.user.object_hook = ctx.user.list_hook = NULL
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ctx.user.encoding = enc
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ctx.user.unicode_errors = err
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if object_hook is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if not PyCallable_Check(object_hook):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise TypeError("object_hook must be a callable.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ctx.user.object_hook = <PyObject*>object_hook
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if list_hook is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if not PyCallable_Check(list_hook):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise TypeError("list_hook must be a callable.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ctx.user.list_hook = <PyObject*>list_hook
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:23:56 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    _gc_disable()
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    try:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ret = template_execute(&ctx, buf, buf_len, &off)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    finally:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        _gc_enable()
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if ret == 1:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return template_data(&ctx)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return None
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 00:23:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:17:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								loads = unpacks = unpackb
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-12 09:29:11 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def unpack(object stream, object object_hook=None, object list_hook=None, bint use_list=0, encoding=None, unicode_errors="strict"):
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-12 20:02:21 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """unpack an object from stream."""
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-28 18:59:05 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return unpackb(stream.read(), use_list=use_list,
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                   object_hook=object_hook, list_hook=list_hook, encoding=encoding, unicode_errors=unicode_errors)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-08 01:30:43 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cdef class Unpacker(object):
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    """Unpacker(read_size=1024*1024)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    Streaming unpacker.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    read_size is used like file_like.read(read_size)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    example:
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        unpacker = Unpacker()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        while 1:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            buf = astream.read()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            unpacker.feed(buf)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            for o in unpacker:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                do_something(o)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    """
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef template_context ctx
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char* buf
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef size_t buf_size, buf_head, buf_tail
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef object file_like
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef object file_like_read
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef Py_ssize_t read_size
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-07 03:02:02 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef bint use_list
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef object object_hook
							 | 
						
					
						
							
								
									
										
										
										
											2011-08-22 01:52:45 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef object _bencoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef object _berrors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char *encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef char *unicode_errors
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __cinit__(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf = NULL
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __dealloc__(self):
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        free(self.buf);
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 05:07:07 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.buf = NULL;
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-01 20:55:24 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
							 | 
						
					
						
							
								
									
										
										
										
											2011-08-22 01:52:45 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                 object object_hook=None, object list_hook=None,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                 encoding=None, unicode_errors='strict'):
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 22:05:31 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if read_size == 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            read_size = 1024*1024
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 15:19:18 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.use_list = use_list
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.file_like = file_like
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if file_like:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.file_like_read = file_like.read
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not PyCallable_Check(self.file_like_read):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                raise ValueError("`file_like.read` must be a callable.")
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.read_size = read_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf = <char*>malloc(read_size)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 05:07:07 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self.buf == NULL:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise MemoryError("Unable to allocate internal buffer.")
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.buf_size = read_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf_head = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf_tail = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        template_init(&self.ctx)
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-17 15:19:18 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.ctx.user.use_list = use_list
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.ctx.user.object_hook = self.ctx.user.list_hook = <PyObject*>NULL
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 01:26:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if object_hook is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not PyCallable_Check(object_hook):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                raise TypeError("object_hook must be a callable.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.object_hook = <PyObject*>object_hook
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if list_hook is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not PyCallable_Check(list_hook):
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 05:05:14 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                raise TypeError("list_hook must be a callable.")
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-26 02:09:52 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.list_hook = <PyObject*>list_hook
							 | 
						
					
						
							
								
									
										
										
										
											2011-08-22 01:52:45 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if encoding is None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.encoding = NULL
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.unicode_errors = NULL
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if isinstance(encoding, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._bencoding = encoding.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._bencoding = encoding
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.encoding = PyBytes_AsString(self._bencoding)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if isinstance(unicode_errors, unicode):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._berrors = unicode_errors.encode('ascii')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._berrors = unicode_errors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.ctx.user.unicode_errors = PyBytes_AsString(self._berrors)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def feed(self, object next_bytes):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef char* buf
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef Py_ssize_t buf_len
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self.file_like is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise AssertionError(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    "unpacker.feed() is not be able to use with`file_like`.")
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        PyObject_AsReadBuffer(next_bytes, <const_void_ptr*>&buf, &buf_len)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.append_buffer(buf, buf_len)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cdef append_buffer(self, void* _buf, Py_ssize_t _buf_len):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            char* buf = self.buf
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            size_t head = self.buf_head
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            size_t tail = self.buf_tail
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            size_t buf_size = self.buf_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            size_t new_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if tail + _buf_len > buf_size:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if ((tail - head) + _buf_len)*2 < buf_size:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                # move to front.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                memmove(buf, buf + head, tail - head)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                tail -= head
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                head = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                # expand buffer.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                new_size = tail + _buf_len
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                if new_size < buf_size*2:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    new_size = buf_size*2
							 | 
						
					
						
							
								
									
										
										
										
											2011-04-15 17:36:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                buf = <char*>realloc(buf, new_size)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 05:07:07 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                if buf == NULL:
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-10 20:47:23 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    # self.buf still holds old buffer and will be freed during
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    # obj destruction
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    raise MemoryError("Unable to enlarge internal buffer.")
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                buf_size = new_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        memcpy(buf + tail, <char*>(_buf), _buf_len)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:54:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.buf = buf
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.buf_head = head
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf_size = buf_size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.buf_tail = tail + _buf_len
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    # prepare self.buf from file_like
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cdef fill_buffer(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self.file_like is not None:
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            next_bytes = self.file_like_read(self.read_size)
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-13 15:49:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if next_bytes:
							 | 
						
					
						
							
								
									
										
										
										
											2010-11-03 03:11:00 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                self.append_buffer(PyBytes_AsString(next_bytes),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                   PyBytes_Size(next_bytes))
							 | 
						
					
						
							
								
									
										
										
										
											2009-07-13 15:49:38 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self.file_like = None
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cpdef unpack(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        """unpack one object"""
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cdef int ret
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        while 1:
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:23:56 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            _gc_disable()
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-29 23:23:56 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            _gc_enable()
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-09 23:40:09 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if ret == 1:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                o = template_data(&self.ctx)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                template_init(&self.ctx)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return o
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            elif ret == 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                if self.file_like is not None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    self.fill_buffer()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    continue
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                raise StopIteration("No more unpack data.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                raise ValueError("Unpack failed: error = %d" % (ret,))
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-26 14:10:20 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __iter__(self):
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-30 10:45:39 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return self
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __next__(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self.unpack()
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 22:05:31 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # for debug.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    #def _buf(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    #    return PyString_FromStringAndSize(self.buf, self.buf_tail)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    #def _off(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    #    return self.buf_head
							 |