@@ -145,11 +145,11 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
145
145
bint use_list = True , bint raw = True , bint strict_map_key = False ,
146
146
encoding = None , unicode_errors = None ,
147
147
object_pairs_hook = None , ext_hook = ExtType,
148
- Py_ssize_t max_str_len = 1024 * 1024 ,
149
- Py_ssize_t max_bin_len = 1024 * 1024 ,
150
- Py_ssize_t max_array_len = 128 * 1024 ,
151
- Py_ssize_t max_map_len = 32 * 1024 ,
152
- Py_ssize_t max_ext_len = 1024 * 1024 ):
148
+ Py_ssize_t max_str_len = - 1 ,
149
+ Py_ssize_t max_bin_len = - 1 ,
150
+ Py_ssize_t max_array_len = - 1 ,
151
+ Py_ssize_t max_map_len = - 1 ,
152
+ Py_ssize_t max_ext_len = - 1 ):
153
153
"""
154
154
Unpack packed_bytes to object. Returns an unpacked object.
155
155
@@ -160,6 +160,8 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
160
160
Other exceptions can be raised during unpacking.
161
161
162
162
See :class:`Unpacker` for options.
163
+
164
+ *max_xxx_len* options are configured automatically from ``len(packed)``.
163
165
"""
164
166
cdef unpack_context ctx
165
167
cdef Py_ssize_t off = 0
@@ -180,6 +182,18 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
180
182
cerr = unicode_errors
181
183
182
184
get_data_from_buffer(packed, & view, & buf, & buf_len, & new_protocol)
185
+
186
+ if max_str_len == - 1 :
187
+ max_str_len = buf_len
188
+ if max_bin_len == - 1 :
189
+ max_bin_len = buf_len
190
+ if max_array_len == - 1 :
191
+ max_array_len = buf_len
192
+ if max_map_len == - 1 :
193
+ max_map_len = buf_len// 2
194
+ if max_ext_len == - 1
8000
span>:
195
+ max_ext_len = buf_len
196
+
183
197
try :
184
198
init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
185
199
use_list, raw, strict_map_key, cenc, cerr,
@@ -259,19 +273,19 @@ cdef class Unpacker(object):
259
273
You should set this parameter when unpacking data from untrusted source.
260
274
261
275
:param int max_str_len:
262
- Limits max length of str. (default: 1024*1024)
276
+ Limits max length of str. (default: max_buffer_size or 1024*1024)
263
277
264
278
:param int max_bin_len:
265
- Limits max length of bin. (default: 1024*1024)
279
+ Limits max length of bin. (default: max_buffer_size or 1024*1024)
266
280
267
281
:param int max_array_len:
268
- Limits max length of array. (default: 128*1024)
282
+ Limits max length of array. (default: max_buffer_size or 128*1024)
269
283
270
284
:param int max_map_len:
271
- Limits max length of map. (default: 32*1024)
285
+ Limits max length of map. (default: max_buffer_size//2 or 32*1024)
272
286
273
287
:param int max_ext_len:
274
- Limits max size of ext type. (default: 1024*1024)
288
+ Limits max size of ext type. (default: max_buffer_size or 1024*1024)
275
289
276
290
:param str encoding:
277
291
Deprecated, use raw instead.
@@ -329,11 +343,11 @@ cdef class Unpacker(object):
329
343
object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
330
344
encoding = None , unicode_errors = None , Py_ssize_t max_buffer_size = 0 ,
331
345
object ext_hook = ExtType,
332
- Py_ssize_t max_str_len = 1024 * 1024 ,
333
- Py_ssize_t max_bin_len = 1024 * 1024 ,
334
- Py_ssize_t max_array_len = 128 * 1024 ,
335
- Py_ssize_t max_map_len = 32 * 1024 ,
336
- Py_ssize_t max_ext_len = 1024 * 1024 ):
346
+ Py_ssize_t max_str_len = - 1 ,
347
+ Py_ssize_t max_bin_len = - 1 ,
348
+ Py_ssize_t max_array_len = - 1 ,
349
+ Py_ssize_t max_map_len = - 1 ,
350
+ Py_ssize_t max_ext_len = - 1 ):
337
351
cdef const char * cenc= NULL ,
338
352
cdef const char * cerr= NULL
339
353
@@ -347,6 +361,18 @@ cdef class Unpacker(object):
347
361
self .file_like_read = file_like.read
348
362
if not PyCallable_Check(self .file_like_read):
349
363
raise TypeError (" `file_like.read` must be a callable." )
364
+
365
+ if max_str_len == - 1 :
366
+ max_str_len = max_buffer_size or 1024 * 1024
367
+ if max_bin_len == - 1 :
368
+ max_bin_len = max_buffer_size or 1024 * 1024
369
+ if max_array_len == - 1 :
370
+ max_array_len = max_buffer_size or 128 * 1024
371
+ if max_map_len == - 1 :
372
+ max_map_len = max_buffer_size// 2 or 32 * 1024
373
+ if max_ext_len == - 1 :
374
+ max_ext_len = max_buffer_size or 1024 * 1024
375
+
350
376
if not max_buffer_size:
351
377
max_buffer_size = INT_MAX
352
378
if read_size > max_buffer_size:
0 commit comments