Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ cdef class Unpacker:
self.ext_hook = ext_hook

self.file_like = file_like
if file_like:
if file_like is not None:
self.file_like_read = file_like.read
if not PyCallable_Check(self.file_like_read):
raise TypeError("`file_like.read` must be a callable.")
Expand Down
15 changes: 15 additions & 0 deletions test/test_sequnpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def test_maxbuffersize_file():
print(unpacker.unpack())


def test_falsy_file_like():
# A file-like object may define __bool__ or __len__ and be falsy
# (e.g. a file wrapper without a name). Only `.read()` is required.
class FalsyBytesIO(io.BytesIO):
def __bool__(self):
return False

stream = FalsyBytesIO(packb([1, 2]) + packb({"a": 3}))
assert list(Unpacker(stream)) == [[1, 2], {"a": 3}]

unpacker = Unpacker(FalsyBytesIO(packb(1) + b"xy"))
assert unpacker.unpack() == 1
assert unpacker.read_bytes(2) == b"xy"


def test_readbytes():
unpacker = Unpacker(read_size=3)
unpacker.feed(b"foobar")
Expand Down
Loading