@@ -145,11 +145,11 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
145145 bint use_list = True , bint raw = True , bint strict_map_key = False ,
146146 encoding = None , unicode_errors = None ,
147147 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 ):
153153 """
154154 Unpack packed_bytes to object. Returns an unpacked object.
155155
@@ -160,6 +160,8 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
160160 Other exceptions can be raised during unpacking.
161161
162162 See :class:`Unpacker` for options.
163+
164+ *max_xxx_len* options are configured automatically from ``len(packed)``.
163165 """
164166 cdef unpack_context ctx
165167 cdef Py_ssize_t off = 0
@@ -180,6 +182,18 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
180182 cerr = unicode_errors
181183
182184 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 :
195+ max_ext_len = buf_len
196+
183197 try :
184198 init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
185199 use_list, raw, strict_map_key, cenc, cerr,
@@ -259,19 +273,19 @@ cdef class Unpacker(object):
259273 You should set this parameter when unpacking data from untrusted source.
260274
261275 :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)
263277
264278 :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)
266280
267281 :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)
269283
270284 :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)
272286
273287 :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)
275289
276290 :param str encoding:
277291 Deprecated, use raw instead.
@@ -329,11 +343,11 @@ cdef class Unpacker(object):
329343 object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
330344 encoding = None , unicode_errors = None , Py_ssize_t max_buffer_size = 0 ,
331345 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 ):
337351 cdef const char * cenc= NULL ,
338352 cdef const char * cerr= NULL
339353
@@ -347,6 +361,18 @@ cdef class Unpacker(object):
347361 self .file_like_read = file_like.read
348362 if not PyCallable_Check(self .file_like_read):
349363 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+
350376 if not max_buffer_size:
351377 max_buffer_size = INT_MAX
352378 if read_size > max_buffer_size:
0 commit comments