-
Notifications
You must be signed in to change notification settings - Fork 12
/
msgpack_pyx.patch
308 lines (275 loc) · 10.1 KB
/
msgpack_pyx.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
--- _msgpack.pyx.orig 2011-12-21 20:51:55.549403995 +0100
+++ _msgpack.pyx 2011-12-21 21:14:06.840307133 +0100
@@ -1,22 +1,39 @@
# coding: utf-8
#cython: embedsignature=True
-from cpython cimport *
+#from cpython cimport *
cdef extern from "Python.h":
ctypedef char* const_char_ptr "const char*"
ctypedef char* const_void_ptr "const void*"
ctypedef struct PyObject
cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
+ cdef char* PyBytes_AsString(object o)
+ Py_ssize_t PyBytes_Size(object o)
+ int PyLong_Check(object o)
+ int PyInt_Check(object o)
+ int PyFloat_Check(object o)
+ int PyBytes_Check(object o)
+ int PyUnicode_Check(object o)
+ int PyDict_Check(object o)
+ int PySequence_Check(object o)
+ int PyCallable_Check(object o)
+ object PyBytes_FromStringAndSize(char* v, Py_ssize_t len)
+
+cdef extern from "stdlib.h":
+ void* malloc(size_t size)
+ void* realloc(void*p, size_t size)
+ void free(void* p)
+
-from libc.stdlib cimport *
-from libc.string cimport *
+#from libc.stdlib cimport *
+#from libc.string cimport *
import gc
_gc_disable = gc.disable
_gc_enable = gc.enable
cdef extern from "pack.h":
struct msgpack_packer:
char* buf
size_t length
size_t buf_size
@@ -26,47 +43,48 @@
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)
-cdef int DEFAULT_RECURSE_LIMIT=511
+cdef int DEFAULT_RECURSE_LIMIT
+DEFAULT_RECURSE_LIMIT=511
-cdef class Packer(object):
+cdef class Packer:
"""MessagePack Packer
usage:
packer = Packer()
astream.write(packer.pack(a))
astream.write(packer.pack(b))
"""
cdef msgpack_packer pk
cdef object _default
cdef object _bencoding
cdef object _berrors
cdef char *encoding
cdef char *unicode_errors
- def __cinit__(self):
- cdef int buf_size = 1024*1024
+ def __init__(self, default=None, encoding='utf-8', unicode_errors='strict'):
+ cdef int buf_size
+ buf_size = 1024*1024
self.pk.buf = <char*> malloc(buf_size);
if self.pk.buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
self.pk.buf_size = buf_size
self.pk.length = 0
- def __init__(self, default=None, encoding='utf-8', unicode_errors='strict'):
if default is not None:
if not PyCallable_Check(default):
raise TypeError("default must be a callable.")
self._default = default
if encoding is None:
self.encoding = NULL
self.unicode_errors = NULL
else:
if isinstance(encoding, unicode):
self._bencoding = encoding.encode('ascii')
@@ -75,21 +93,21 @@
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)
def __dealloc__(self):
free(self.pk.buf);
- cdef int _pack(self, object o, int nest_limit=DEFAULT_RECURSE_LIMIT) except -1:
+ cdef int _pack(self, object o, int nest_limit) except -1:
cdef long long llval
cdef unsigned long long ullval
cdef long longval
cdef double fval
cdef char* rawval
cdef int ret
cdef dict d
if nest_limit < 0:
raise ValueError("Too deep.")
@@ -186,26 +204,29 @@
size_t count
unsigned int ct
PyObject* key
int template_execute(template_context* ctx, const_char_ptr data,
size_t len, size_t* off) except -1
void template_init(template_context* ctx)
object template_data(template_context* ctx)
-def unpackb(object packed, object object_hook=None, object list_hook=None, bint use_list=0, encoding=None, unicode_errors="strict"):
+def unpackb(object packed, object object_hook=None, object list_hook=None, int use_list=0, encoding=None, unicode_errors="strict"):
"""
Unpack packed_bytes to object. Returns an unpacked object."""
cdef template_context ctx
- cdef size_t off = 0
+ cdef size_t off
+ off = 0
cdef int ret
+ cdef char* enc
+ cdef char* err
cdef char* buf
cdef Py_ssize_t buf_len
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
if encoding is None:
enc = NULL
err = NULL
else:
if isinstance(encoding, unicode):
@@ -236,28 +257,28 @@
try:
ret = template_execute(&ctx, buf, buf_len, &off)
finally:
_gc_enable()
if ret == 1:
return template_data(&ctx)
else:
return None
-def unpack(object stream, object object_hook=None, object list_hook=None, bint use_list=0, encoding=None, unicode_errors="strict"):
+def unpack(object stream, object object_hook=None, object list_hook=None, int use_list=0, encoding=None, unicode_errors="strict"):
"""
unpack an object from stream.
"""
return unpackb(stream.read(), use_list=use_list,
object_hook=object_hook, list_hook=list_hook, encoding=encoding, unicode_errors=unicode_errors)
-cdef class Unpacker(object):
+cdef class Unpacker:
"""
Streaming unpacker.
read_size is used like file_like.read(read_size)
`file_like` is a file-like object having `.read(n)` method.
When `Unpacker` initialized with `file_like`, unpacker reads serialized data
from it and `.feed()` method is not usable.
`read_size` is used as `file_like.read(read_size)`. (default: 1M)
@@ -280,37 +301,35 @@
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
cdef object file_like_read
cdef Py_ssize_t read_size
- cdef bint use_list
+ cdef int use_list
cdef object object_hook
cdef object _bencoding
cdef object _berrors
cdef char *encoding
cdef char *unicode_errors
- def __cinit__(self):
- self.buf = NULL
-
def __dealloc__(self):
free(self.buf)
self.buf = NULL
- def __init__(self, file_like=None, Py_ssize_t read_size=1024*1024, bint use_list=0,
+ def __init__(self, file_like=None, read_size=1024*1024, int use_list=0,
object object_hook=None, object list_hook=None,
encoding=None, unicode_errors='strict'):
+ self.buf = NULL
self.use_list = use_list
self.file_like = file_like
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.")
self.read_size = read_size
self.buf = <char*>malloc(read_size)
if self.buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
@@ -346,26 +365,29 @@
def feed(self, object next_bytes):
cdef char* buf
cdef Py_ssize_t buf_len
if self.file_like is not None:
raise AssertionError(
"unpacker.feed() is not be able to use with`file_like`.")
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
+ cdef char* buf
+ buf = self.buf
+ cdef size_t head
+ head = self.buf_head
+ cdef size_t tail
+ tail = self.buf_tail
+ cdef size_t buf_size
+ buf_size = self.buf_size
+ cdef 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
@@ -387,21 +409,24 @@
# prepare self.buf from file_like
cdef fill_buffer(self):
if self.file_like is not None:
next_bytes = self.file_like_read(self.read_size)
if next_bytes:
self.append_buffer(PyBytes_AsString(next_bytes),
PyBytes_Size(next_bytes))
else:
self.file_like = None
- cpdef unpack(self):
+ def unpack(self):
+ return self.c_unpack()
+
+ cdef c_unpack(self):
"""unpack one object"""
cdef int ret
while 1:
_gc_disable()
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
_gc_enable()
if ret == 1:
o = template_data(&self.ctx)
template_init(&self.ctx)
return o
@@ -410,18 +435,18 @@
self.fill_buffer()
continue
raise StopIteration("No more unpack data.")
else:
raise ValueError("Unpack failed: error = %d" % (ret,))
def __iter__(self):
return self
def __next__(self):
- return self.unpack()
+ return self.c_unpack()
# for debug.
#def _buf(self):
# return PyString_FromStringAndSize(self.buf, self.buf_tail)
#def _off(self):
# return self.buf_head