This PR is for resolving the following issue: [issue](https://github.com/capnproto/pycapnp/issues/379) 1. Created `_PyCustomMessageBuilder` extends `MessageBuilder`, enabling the ability to customise the `SegmentAllocate` method in Python. This allows allocation and data population within shared memory, and supports zero-copy inter-process data transfer by passing segment offsets. 2. Fields of type `Data` now support being set with a `memoryview`. When retrieving a `Data` field from a `DynamicStructBuilder`, it will return a writable `memoryview`, allowing users to modify the data directly. This enables memory to be pre-allocated and content to be modified in later, eliminating an extra copy. When retrieving a `Data` field from a `DynamicStructReader`, it will return a read-only `memoryview`, allowing user to read data without memory copy. * add memoryview and custom builder * support set dynamic field * add curSize * add initialSize and lastSize * change StringPtr name * add test case * refine test case * convert func to py callable object * add initial value * refine example * add copy as_reader and new_message, make structReader's data field return RO memoryView * rebase master and bugfix * reformat flake8 * refine test case * refine test cases for blob * remove unused import for flake8 * run black . --------- Co-authored-by: Brian Xu <brian.xu1@bytedance.com>
63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
"""A python library wrapping the Cap'n Proto C++ library
|
|
|
|
Example Usage::
|
|
|
|
import capnp
|
|
|
|
addressbook = capnp.load('addressbook.capnp')
|
|
|
|
# Building
|
|
addresses = addressbook.AddressBook.newMessage()
|
|
people = addresses.init('people', 1)
|
|
|
|
alice = people[0]
|
|
alice.id = 123
|
|
alice.name = 'Alice'
|
|
alice.email = 'alice@example.com'
|
|
alicePhone = alice.init('phones', 1)[0]
|
|
alicePhone.type = 'mobile'
|
|
|
|
f = open('example.bin', 'w')
|
|
addresses.write(f)
|
|
f.close()
|
|
|
|
# Reading
|
|
f = open('example.bin')
|
|
|
|
addresses = addressbook.AddressBook.read(f)
|
|
|
|
for person in addresses.people:
|
|
print(person.name, ':', person.email)
|
|
for phone in person.phones:
|
|
print(phone.type, ':', phone.number)
|
|
"""
|
|
|
|
# flake8: noqa F401 F403 F405
|
|
from .version import version as __version__
|
|
from .lib.capnp import *
|
|
from .lib.capnp import (
|
|
_CapabilityClient,
|
|
_DynamicCapabilityClient,
|
|
_DynamicListBuilder,
|
|
_DynamicListReader,
|
|
_DynamicOrphan,
|
|
_DynamicResizableListBuilder,
|
|
_DynamicStructBuilder,
|
|
_DynamicStructReader,
|
|
_EventLoop,
|
|
_InterfaceModule,
|
|
_ListSchema,
|
|
_MallocMessageBuilder,
|
|
_PyCustomMessageBuilder,
|
|
_PackedFdMessageReader,
|
|
_StreamFdMessageReader,
|
|
_StructModule,
|
|
_write_message_to_fd,
|
|
_write_packed_message_to_fd,
|
|
_AsyncIoStream as AsyncIoStream,
|
|
_init_capnp_api,
|
|
)
|
|
|
|
_init_capnp_api()
|
|
add_import_hook() # enable import hook by default
|