Fix reading more than read_size.

This commit is contained in:
INADA Naoki 2012-09-21 13:58:56 +09:00
parent 56ec7ee1b1
commit 0297b36bda

View file

@ -278,8 +278,8 @@ cdef class Unpacker(object):
`unicode_errors` is used for decoding bytes.
`max_buffer_size` limits size of data waiting unpacked. 0 means unlimited
(default).
`max_buffer_size` limits size of data waiting unpacked.
0 means system's INT_MAX (default).
Raises `BufferFull` exception when it is insufficient.
You shoud set this parameter when unpacking data from untrasted source.
@ -332,11 +332,11 @@ cdef class Unpacker(object):
raise ValueError("`file_like.read` must be a callable.")
if not max_buffer_size:
max_buffer_size = INT_MAX
if read_size > max_buffer_size:
raise ValueError("read_size should be less or equal to max_buffer_size")
if not read_size:
read_size = min(max_buffer_size, 1024**2)
self.max_buffer_size = max_buffer_size
if read_size > max_buffer_size:
raise ValueError("read_size should be less or equal to max_buffer_size")
self.read_size = read_size
self.buf = <char*>malloc(read_size)
if self.buf == NULL:
@ -419,16 +419,13 @@ cdef class Unpacker(object):
self.buf_size = buf_size
self.buf_tail = tail + _buf_len
# prepare self.buf from file_like
cdef fill_buffer(self):
if self.file_like is not None:
cdef read_from_file(self):
next_bytes = self.file_like_read(
max(self.read_size,
min(self.read_size,
self.max_buffer_size - (self.buf_tail - self.buf_head)
))
if next_bytes:
self.append_buffer(PyBytes_AsString(next_bytes),
PyBytes_Size(next_bytes))
self.append_buffer(PyBytes_AsString(next_bytes), PyBytes_Size(next_bytes))
else:
self.file_like = None
@ -443,7 +440,7 @@ cdef class Unpacker(object):
return o
elif ret == 0:
if self.file_like is not None:
self.fill_buffer()
self.read_from_file()
continue
raise StopIteration("No more unpack data.")
else: