Skip to content
Merged
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
6 changes: 6 additions & 0 deletions capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,12 @@ cdef _to_dict(msg, bint verbose, bint ordered, bint encode_bytes_as_base64=False
# encode the message as base64 and return utf-8 string
return base64.b64encode(msg).decode('utf-8')

if msg_type is memoryview:
if encode_bytes_as_base64:
return base64.b64encode(bytes(msg)).decode('utf-8')
else:
return bytes(msg)

return msg


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def run(self): # noqa: C901
"*.capnp",
"helpers/*.pxd",
"helpers/*.h",
"includes/*.h",
"includes/*.pxd",
"lib/*.pxd",
"lib/*.py",
Expand Down
3 changes: 2 additions & 1 deletion test/test_blob_to_dict_base64.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import capnp
import base64
import pytest

this_dir = os.path.dirname(__file__)
Expand All @@ -14,7 +15,7 @@ def test_blob_to_dict(blob_schema):
blob_value = b"hello world"
blob = blob_schema.BlobTest(blob=blob_value)
blob_dict = blob.to_dict(encode_bytes_as_base64=True)
assert blob_dict["blob"].tobytes() == blob_value
assert base64.b64decode(blob_dict["blob"]) == blob_value
msg = blob_schema.BlobTest.new_message()
msg.from_dict(blob_dict)
assert blob.blob == blob_value
Loading