A cython wrapping of the C++ Cap'n Proto library
C++14 supported compiler
- gcc 6.1+ (5+ may work)
- clang 6 (3.4+ may work)
- Visual Studio 2017+
cmake (needed for bundled capnproto)
- ninja (macOS + Linux)
- Visual Studio 2017+
capnproto-0.8.0 (0.7.0 will also work if linking to system libraries)
- Not necessary if using bundled capnproto
32-bit Linux requires that capnproto be compiled with
-fPIC. This is usually set correctly unless you are compiling canproto yourself. This is also called
-DCMAKE_POSITION_INDEPENDENT_CODE=1 for cmake.
pycapnp has additional development dependencies, including cython and pytest. See requirements.txt for them all.
Building and installation
pip install pycapnp. You can set the CC environment variable to control which compiler is used, ie
CC=gcc-8.2 pip install pycapnp.
Or you can clone the repo like so:
git clone https://github.com/capnproto/pycapnp.git cd pycapnp pip install .
If you wish to install using the latest upstream C++ Cap'n Proto:
pip install \ --install-option "--libcapnp-url" \ --install-option "https://github.com/capnproto/capnproto/archive/master.tar.gz" \ --install-option "--force-bundled-libcapnp" .
To force bundled python:
pip install --install-option "--force-bundled-libcapnp" .
Slightly more prompt error messages using distutils rather than pip.
python setup.py install --force-bundled-libcapnp
The bundling system isn't that smart so it might be necessary to clean up the bundled build when changing versions:
python setup.py clean
Python 3.7+ is supported. Earlier versions of Python have asyncio bugs that might be possible to work around, but may require significant work (3.5 and 3.6).
Git flow has been abandoned, use master.
To test, use a pipenv (or install requirements.txt and run pytest manually).
pip install pipenv pipenv install pipenv run pytest
Building a dumb binary distribution:
python setup.py bdist_dumb
Building a Python wheel distributiion:
python setup.py bdist_wheel
There is some basic documentation here.
Make sure to look at the examples. The examples are generally kept up to date with the recommended usage of the library.
The examples directory has one example that shows off pycapnp quite nicely. Here it is, reproduced:
import os import capnp import addressbook_capnp def writeAddressBook(file): addresses = addressbook_capnp.AddressBook.new_message() people = addresses.init('people', 2) alice = people alice.id = 123 alice.name = 'Alice' alice.email = 'firstname.lastname@example.org' alicePhones = alice.init('phones', 1) alicePhones.number = "555-1212" alicePhones.type = 'mobile' alice.employment.school = "MIT" bob = people bob.id = 456 bob.name = 'Bob' bob.email = 'email@example.com' bobPhones = bob.init('phones', 2) bobPhones.number = "555-4567" bobPhones.type = 'home' bobPhones.number = "555-7654" bobPhones.type = 'work' bob.employment.unemployed = None addresses.write(file) def printAddressBook(file): addresses = addressbook_capnp.AddressBook.read(file) for person in addresses.people: print(person.name, ':', person.email) for phone in person.phones: print(phone.type, ':', phone.number) which = person.employment.which() print(which) if which == 'unemployed': print('unemployed') elif which == 'employer': print('employer:', person.employment.employer) elif which == 'school': print('student at:', person.employment.school) elif which == 'selfEmployed': print('self employed') print() if __name__ == '__main__': f = open('example', 'w') writeAddressBook(f) f = open('example', 'r') printAddressBook(f)
Also, pycapnp has gained RPC features that include pipelining and a promise style API. Refer to the calculator example in the examples directory for a much better demonstration:
import capnp import socket import test_capability_capnp class Server(test_capability_capnp.TestInterface.Server): def __init__(self, val=1): self.val = val def foo(self, i, j, **kwargs): return str(i * 5 + self.val) def server(write_end): server = capnp.TwoPartyServer(write_end, bootstrap=Server(100)) def client(read_end): client = capnp.TwoPartyClient(read_end) cap = client.bootstrap() cap = cap.cast_as(test_capability_capnp.TestInterface) remote = cap.foo(i=5) response = remote.wait() assert response.x == '125' if __name__ == '__main__': read_end, write_end = socket.socketpair(socket.AF_UNIX) # This is a toy example using socketpair. # In real situations, you can use any socket. server(write_end) client(read_end)
- Validated Python 3.9 (3.7 and 3.8 are also supported)
- Updated package to include LICENSE file
- Updated examples to avoid run_forever() as ctrl+c will not work
- Adding xfail to pytest cases which fail sometimes due to network port oddities (please use asyncio, as Python handles things more gracefully)
- Minimum capnproto version is now 0.8.0
- Added asyncio ssl calculator test
- Added poll_once to TwoPartyServer API
- More cleanup
- Fix absolute and circular imports
- Fix Promise aliasing issue (Promise to _Promise)
- Documentation update
- Updated installation instructions
- Added RPC documentation for asyncio
- Python 3.7+ required (asyncio support)
- TLS/SSL support using asyncio
- Windows support
- General cleanup
- May be incompatible with code written for pycapnp 0.6.4 and lower
- Removing pypandoc/pandoc packaging requirement
- Minimum capnproto version is now 0.7.0
- Fix bugs in
read_multiple_bytes(thanks to @tsh56)
- Remove end-of-life Python versions 2.6, 3.2, and 3.3. Add CI tests for 3.6
- Expose SchemaParser in Cython header
- Bump bundled capnp version to v0.6.1 (thanks to @E8Yuval)
- Fix a memleak in RemotePromise (thanks to @E8Yuval)
- Add support for buffers/memoryviews in
from_bytes(thanks to @aldanor)
- Fixed upload to PyPi (forgot to cythonize)
- Update bundled capnp version to v0.6.0 and fix related problems (thanks to @benmoran)
- Fix memleak with KjException (thanks to @tsh56)
- Bump bundled capnp version to v0.5.3.1
- Make enums hashable (thanks to @madeleine-empirical)
- Rework logic on when to build bundled libcapnp. Fixes cross-compilation (thanks to @benizl)
- Add traversal_limit_in_words and nesting_limit to RPC classes (thanks to @asilversempirical)
- Include class attributes in dir. This allows for code completion of class methods (thanks to @chaoflow )
- Allow setting lists with python tuples (thanks to @chaoflow)
- Fix traversal_limit_in_words and nesting_limit being ignored by
from_bytes(thanks to @plesner)
- Fix bug that prevented event loop from actually being lazy initialized
- Fix possible recursive loop in KjException
clear_write_flagmethod to builder classes
- Make the event loop be lazy initialized
- Add support for segment (de)serialization (thanks to @gcv). See to_segments/from_segments methods.
- Fix response objects not referencing parents correctly
- Add test for large reads
- Fix build problem with Cython v0.24
- Include the changelog in the manifest (should fix install problems if pandoc is present)
- Include the traceback in exceptions
- Make sure to encode to utf-8, not the default encoding (thanks to @novas0x2a)
- Add --libcapnp-url option in installer to allow installing arbitrary libcapnp versions
- Support mmap objects for reading with from_bytes (thanks to @bpiwowar)
- Change read_multiple and read_multiple_packed to copy by default
- Fix mistakenly discarding the file parameter on reads
- Add reraise_kj_exception to the prettyPrint functions. (thanks to @kdienes)
- Fix KjException init (missing wrapper). (thanks to @E8-Storage)
- Update bundled libcapnp to v0.5.2
- Add warnings for using old restorer methods. You should use
- Fix warning from PyEventPort
- Handle AnyPointers better as arguments to RPC functions
- Add support for using keyword arguments with a named struct in an RPC
- Add bootstrap method to TwoPartyServer
initmethod to lists
- Add support for unix sockets in RPC
- Fix a serious bug in TwoPartyServer that was preventing it from working when passed a string address.
- Fix bugs that were exposed by defining KJDEBUG (thanks @davidcarne for finding this)
- Update bundled C++ libcapnp to v0.5.1.2 security release
- Update bundled C++ libcapnp to v0.5.1.1 security release
- Add bootstrap RPC methods
- Fix possible segfault when importing multiple schemas
- Fix possible crash due to bad destructor ordering in MessageReader (by @JohnEmhoff)
- Default to no longer using cython
- Add read_multiple_bytes/read_multiple_bytes_packed methods
- Added Python 3.4 to the travis build matrix
- Bump version for bundled C++ libcapnp to v0.5.1
- Remove installation dependency on cython. We now have no dependencies since libcapnp will automatically build as well.
- Timer class
- pycapnp is now thread-safe and allows an event loop to be run in each thread
- You must destroy and re-create the event loop to get this functionality (see
- You must destroy and re-create the event loop to get this functionality (see
- Inheritance now works correctly for interfaces (previously inherited methods were inaccessible from pycapnp)
- Add ability to import modules with dashes or spaces. Use underscores in place of them
from_byteswith builder=True is no longer zero copy. It never worked correctly, and is much safer now
num_first_segment_wordsargument wherever message creation can occur
- Allow restoring a null objectId by passing None to restore
- Support ordered dictionary in
- Add ListSchema class and schemas for native types under
capnp.typeswhich completes all the Schemas needed to be wrapped. See
test_schema.pyfor examples using it
- Add automatic build of C++ libcapnp if it's not detected on the system. Also add flags --force-bundled-libcapnp and --force-system-libcapnp respectively
- Fix build for new 0.21 release of Cython. 0.21 is now the minimum supported version of Cython.
to_dictnot converting enums to strings
- Fix compilation problem with gcc 4.8
- Fix problem with uninitialized unions in _from_dict
- Add accesible version numbers for C++ libcapnp
- Remove onDrained since it was removed upstream
- Replace usage of strings as enum type with custom
- Also change
Struct.which()method to be a property
Struct.whichand return an enum type (
_DynamicEnumField, which behaves much like
- TwoPartyServer.run_forever() now will handle more than 1 simulataneous connection.
- Change exception wrapper to detect and raise AttributeError for field lookup exceptions (Fixes problem in Python3.x
- Allow setting of fields with python dicts.
- Remove python 3.2 from travis tests. Python 3.2 still should work fine, but it's more trouble than it's worth to write unicode tests that work in both it and Python2.
- Fix problems with null characters in Text/Data fields. Fixes #19
- Initial working version of RPC
- Add get_root_as_any to _MessageReader
- Add capnp.pxd for public declarations of cython classes
- Fix problems compiling with gcc4.7
- Change naming of ReaderOption parameters to be pep8 compliant
- Add ReaderOptions to read/read_packed/from_bytes
- Add defaults flag to capnp-json. Also remove 'which' field
- Add capnp-json serializer script. Also fix bugs in from_dict
- Fix build for clang/python3. Also remove -fpermissive
as_buildermethod to Struct Reader
- Add warning when writing the same message more than once
- First working version of capability interfaces
- Wrap InterfaceSchema
- Fix setting string fields to support all types of strings
- Fix changed API for DynamicObject/ObjectPointer
- Add not having installed the C++ libcapnp library to 'Common Problems'
- Add _short_str function for use in capnp_test_pycapnp.py
- Add test script for testing with https://github.com/kaos/capnp_test
- Add handling of DynamicObject
- Fix lists of lists or dicts for from_dict
- Fix problem with to_dict
- Add _DynamicStructBuilder.to_bytes() and <struct module>.from_bytes()
- Change == on StructSchema to return cbool
- Add Builder and Reader ABCs for each struct type
- Fix handling of empty path '' in load_module
- Add from_dict
- Fix bug in exception handling for which(). Also standardize exceptions.
- Change import hook to require modules to end in '_capnp'
- Add import monkey patch function.
- Change naming for functions to conform to PEP 8. Also deprecate old read/write API
- Update preferred method for reading/writing messages from files
- Forgot to change project name in setup.py
- Change all references to old project name (change from capnpc-python-cpp to pycapnp)
- Change DynamicValue.Reader lists to be returned as _DynamicListReader
- Unify setters for DynamicList and DynamicStruct
- Add shortcuts for reading from / writing to files. In Python, it doesn't make much sense to force people to muck around with MessageReaders and MessageBuilders since everything is landing on the heap anyway. Instead, let's make it easy: MyType.read[Packed]From(file) reads a file and returns a MyType reader. MyType.newMessage() returns a MyType builder representing the root of a new message. You can call this builder's write[Packed]To(file) method to write it to a file.
- Store Builders by value rather than allocate them separately on the heap (matches treatment of Readers). v0.3 fixes the bug that made this not work.
- Wrap MessageBuilder::setRoot().
- Add tests based on TestAllTypes from the C++ test.capnp. Fix problems uncovered in capnp.pyx.
- Implement str and repr for struct and list builders. str uses prettyPrint while repr shows the type name and the low-whitespace stringification. Also implement repr for StructSchema, just because why not?
- Change load to use a global SchemaParser. Make structs settable as field
- Add docstrings for new functions and _DynamicResizableListBuilder
- Add initial tests
- Add _capnp for original Cython module. Meant for testing.
- Lowercase schema so it conforms to member naming conventions
- Expose _StructSchema's raw node
- Add some useful _StructSchema, reader, and builder methods
- Add full orphan functionality. Also, allow special orphan lists
- Finish up adding docstrings to all public classes/methods
- Add a ton of docstrings and add to official docs
- Add DynamicOrphan
- Add intersphinx for linking to python docs
- Add C++ library version check
- Add handling of constants in schemas
- Fix new error with DynamicValue.Builder no longer being copyable
- Fix Void namespace change
- Updated capnp schema to conform with new union rules
- Fix for the removal of DynamicUnion from the C++ API
- Add MANIFEST.in to include README
- Update docs with lines about upgrading setuptools
- Initial commit of docs
- Add querying unnamed enums to structs
- Fix enum interface change for benchmark
- Random formatting cleanup
- Allow import paths in the schema loader
- Add travis CI
- Initial working version
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pycapnp-1.0.0-cp37-cp37m-macosx_10_14_x86_64.whl (1.6 MB)||File type Wheel||Python version cp37||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp37-cp37m-manylinux2010_i686.whl (4.2 MB)||File type Wheel||Python version cp37||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp37-cp37m-manylinux2010_x86_64.whl (4.2 MB)||File type Wheel||Python version cp37||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp37-cp37m-win_amd64.whl (1.1 MB)||File type Wheel||Python version cp37||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp38-cp38-macosx_10_14_x86_64.whl (1.6 MB)||File type Wheel||Python version cp38||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp38-cp38-manylinux2010_i686.whl (4.6 MB)||File type Wheel||Python version cp38||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp38-cp38-manylinux2010_x86_64.whl (4.7 MB)||File type Wheel||Python version cp38||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp38-cp38-win_amd64.whl (1.2 MB)||File type Wheel||Python version cp38||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp39-cp39-macosx_10_14_x86_64.whl (1.6 MB)||File type Wheel||Python version cp39||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp39-cp39-manylinux2010_i686.whl (4.4 MB)||File type Wheel||Python version cp39||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp39-cp39-manylinux2010_x86_64.whl (4.5 MB)||File type Wheel||Python version cp39||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0-cp39-cp39-win_amd64.whl (1.2 MB)||File type Wheel||Python version cp39||Upload date||Hashes View|
|Filename, size pycapnp-1.0.0.tar.gz (528.3 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for pycapnp-1.0.0-cp37-cp37m-macosx_10_14_x86_64.whl
Hashes for pycapnp-1.0.0-cp37-cp37m-manylinux2010_i686.whl
Hashes for pycapnp-1.0.0-cp37-cp37m-manylinux2010_x86_64.whl
Hashes for pycapnp-1.0.0-cp37-cp37m-win_amd64.whl
Hashes for pycapnp-1.0.0-cp38-cp38-macosx_10_14_x86_64.whl
Hashes for pycapnp-1.0.0-cp38-cp38-manylinux2010_i686.whl
Hashes for pycapnp-1.0.0-cp38-cp38-manylinux2010_x86_64.whl
Hashes for pycapnp-1.0.0-cp38-cp38-win_amd64.whl
Hashes for pycapnp-1.0.0-cp39-cp39-macosx_10_14_x86_64.whl
Hashes for pycapnp-1.0.0-cp39-cp39-manylinux2010_i686.whl
Hashes for pycapnp-1.0.0-cp39-cp39-manylinux2010_x86_64.whl
Hashes for pycapnp-1.0.0-cp39-cp39-win_amd64.whl