Deserialize CBOR and/or do CDDL schema validation
Project description
PyCDDL: Deserialize CBOR and/or do CDDL schema validation
CDDL is a schema language for the CBOR serialization format.
pycddl
allows you to:
- Validate CBOR documents match a particular CDDL schema, based on the Rust
cddl
library. - Optionally, decode CBOR documents.
Usage
Validation
Here we use the cbor2
library to serialize a dictionary to CBOR, and then validate it:
from pycddl import Schema
import cbor2
uint_schema = Schema("""
object = {
xint: uint
}
"""
)
uint_schema.validate_cbor(cbor2.dumps({"xint", -2}))
If validation fails, a pycddl.ValidationError
is raised.
Validation + deserialization
You can deserialize CBOR to Python objects using cbor.loads()
.
However:
cbor2
uses C code by default, and the C programming language is prone to memory safety issues. If you are reading untrusted CBOR, better to use a Rust library to decode the data.- You will need to parse the CBOR twice, once for validation and once for decoding, adding performance overhead.
By deserializing with pycddl
, you solve the first problem, and a future version of pycddl
will solve the second problem (see https://gitlab.com/tahoe-lafs/pycddl/-/issues/37).
from pycddl import Schema
import cbor2
uint_schema = Schema("""
object = {
xint: uint
}
"""
)
deserialized = uint_schema.validate_cbor(cbor2.dumps({"xint", -2}), True)
assert deserialized == {"xint": -2}
Deserializing without schema validation
If you don't care about schemas, you can just deserialize the CBOR like so:
from pycddl import Schema
ACCEPT_ANYTHING = Schema("main = any")
def loads(encoded_cbor_bytes):
return ACCEPT_ANYTHING.validate_cbor(encoded_cbor_bytes, True)
In a future release this will become a standalone, more efficient API, see https://gitlab.com/tahoe-lafs/pycddl/-/issues/36
Reducing memory usage and safety constraints
In order to reduce memory usage, you can pass in any Python object that implements the buffer API and stores bytes, e.g. a memoryview()
or a mmap
object.
The passed-in object must be read-only, and the data must not change during validation! If you mutate the data while validation is happening the result can be memory corruption or other undefined behavior.
Supported CBOR types for deserialization
If you are deserializing a CBOR document into Python objects, you can deserialize:
- Null/None.
- Booleans.
- Floats.
- Integers up to 64-bit size. Larger integers aren't supported yet.
- Bytes.
- Strings.
- Lists.
- Maps/dictionaries.
- Sets.
Other types will be added in the future if there is user demand.
Schema validation is not restricted to this list, but rather is limited by the functionality of the cddl
Rust crate.
Release notes
0.6.3
Features:
- Support final 3.13.
0.6.2
Features:
- Added support for Python 3.13.
0.6.1
Bug fixes:
- Allow PyPy to deserialize CBOR too.
0.6.0
Features:
validate_cbor(serialized_cbor, True)
will deserialize the CBOR document into Python objects, so you don't need to use e.g.cbor2.loads(serialized_cbor)
separately.
Bug fixes:
- Release the GIL in a much more useful way.
Misc:
- Upgrade to newer
cddl
crate (0.9.4).
0.5.3
- Upgrade to newer
cddl
crate (0.9.3). - Add Python 3.12 support.
0.5.2
- Upgrade to newer
cddl
crate (0.9.2), improving validation functionality.
0.5.1
- Upgrade to newer
cddl
crate, fixing some validation bugs.
0.5.0
- Support for ARM macOS.
- Dropped Python 3.7 support.
0.4.1
- Test fixes, with no user-relevant changes.
0.4.0
validate_cbor()
now accepts read-only buffers, not justbytes
. This is useful if you want to e.g. validate a large file, since you canmmap()
it.- The GIL is released when parsing documents larger than 10KiB.
0.3.0
- Fixed major bug where if the document was valid UTF-8, the library would attempt to parse it as JSON!
- Added support for ARM macOS.
0.2.2
- Updated to
cddl
0.9.1.
0.2.1
- Added PyPy wheels.
0.2.0
- Schemas are now only parsed once (when the
Schema()
object is created), instead of every time validation happens, which should improve validation performance. - Updated to a newer version of underlying CDDL library, which should make CDDL parsing more compliant.
- Added a
repr()
implementation toSchema
for easier debugging.
0.1.11
- Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pycddl-0.6.3-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33362857d55a5c0f8a58e97d77badf6c6a078081e0c84f82b8c753ddb34267d0 |
|
MD5 | dccecfb2ad19bd02f1ad05babdb7c59d |
|
BLAKE2b-256 | 157fa74075c7c5879f4d2e3415b09f4a7d95ccb7d7f3e0fb6a90d578cf321364 |
Hashes for pycddl-0.6.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80d7b7ef2e01a58d28cea9b7be95a988f1f4b409134e63665fd670c1dae1c646 |
|
MD5 | b820b378ffa855d9d02df25d33734083 |
|
BLAKE2b-256 | d6c26dda256f04c8a34a154f6852fdd2ebd9ab781c0eb3f0488c04b8203d8b65 |
Hashes for pycddl-0.6.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b0c1dd1c32bc14ff48a40d93c4c695cb296fa8d0fe9307c46963076379a1e33 |
|
MD5 | 0747a58dc23588c0d21a7e32c3229197 |
|
BLAKE2b-256 | 42fd4ea626aed9eef67331602c95c7c6dba7d0e59e3b17d833fdecdc06cebb2f |
Hashes for pycddl-0.6.3-cp313-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4df48f5d0fb9023793936c373d8637636479d9328658f6ffb1b03927bb294b0d |
|
MD5 | 4462bde8aaf00ac86e5ba860f4b2fee0 |
|
BLAKE2b-256 | 919695cc4196eb94a72d1adf4c5fef86a21c16d5f448ab3c6e9988d2b6693cff |
Hashes for pycddl-0.6.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ea54635a3064d4f2504d3deae81c31ce6c3518081381da1a043be35e7d5865 |
|
MD5 | af79323bb9a2df441df9f8b76c6e62fa |
|
BLAKE2b-256 | a4065030dfeba17e48826417a2125c3bd44bec5736bd7285cc5922cc10bddc84 |
Hashes for pycddl-0.6.3-cp313-cp313-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee065d28dac7224728a8c2fd415b1a83bfe90048fa2af9270d525bf94cf08ac7 |
|
MD5 | ece7184cd5058f44a910fb5e9ec732b1 |
|
BLAKE2b-256 | 936df25e296a77b81c64003c8c15ba45070b147b041dd0e03246b16e0e150bcc |
Hashes for pycddl-0.6.3-cp312-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63790a5075bfefcb5f33c62ed7adaedab48fbbe1d1a06f66f1bcadc57eaf8d19 |
|
MD5 | 4aad3a657af9dacdccbfa11c992d42cf |
|
BLAKE2b-256 | efa2d75838bd4a64cddccd5e8e2eb08a25637d73bd4b7f0bcc5084f9f363a86b |
Hashes for pycddl-0.6.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26fe2145492df9b6ea935e7f62d041357d350ece1c4632c7af3726b17dc2be32 |
|
MD5 | 0a97498301b0f79460928c79bb31d6c9 |
|
BLAKE2b-256 | 708f17aa32359d5d246bac8d28c7273ca7f79595d07d5d418a6b4427c91606cf |
Hashes for pycddl-0.6.3-cp312-cp312-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c04c25a87401775be5368104aa481783acf3d07a8b12dcdb1ca3014d40726d02 |
|
MD5 | 7900dd0843d7b90de83c28adcb383fa9 |
|
BLAKE2b-256 | 13725f5a7c7414e6cac2e08c45708ca618b9983fdee41c65c4c2c7f3de208abf |
Hashes for pycddl-0.6.3-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f1cca5f96870511406a1278cf88804604d67752fb9acabadd61de7b8e3a9354 |
|
MD5 | c4c587589c7bf4b069e9f193ee5ed7d1 |
|
BLAKE2b-256 | b5b290b1f8af2bc4b542808c82955803a1ff20d81f2f7712677e0aebd98026a6 |
Hashes for pycddl-0.6.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ef23c89a9a45d2061a515a6e848cbbeeac7a2ee4ffb7fae7f0b3da3df1ee663 |
|
MD5 | 5083d21b676f076fa802ffc1c52758e7 |
|
BLAKE2b-256 | cda3eccf5451f35b8445b541e8a4796b044160b09f76368d37e01f4515ce31a1 |
Hashes for pycddl-0.6.3-cp311-cp311-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be28d2435ef580f9db1b0b255458c58dc4fc10d8e672d3ea1b8c18172f3b7b7c |
|
MD5 | 8abd33f2e444fc3aca8eb440e3c7d526 |
|
BLAKE2b-256 | 14ec55dea652ab919acb8a8ff1240f44fc73680a989a731a05d8e5b65c27bd12 |
Hashes for pycddl-0.6.3-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72adeeb012928fdfb7347758d1bb274310c6613cb0af6cd09713f33e37310f5a |
|
MD5 | 825d0dd9008dd88573bf47394d8e0eb3 |
|
BLAKE2b-256 | 3f06e14a92020491b15977a0ab3b2911f61aba7559a0dca88a8cb1734cf58c4b |
Hashes for pycddl-0.6.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00179a5a7b7d62673a502313d6a2596fc92d162ef45e0c1eba046cde18ac3479 |
|
MD5 | 804ddcee3d25cd331cff6a768eb70211 |
|
BLAKE2b-256 | fdcff415e331ef31f72cfd4fd727eaf5ba068cab2abc153300c880a166e359b5 |
Hashes for pycddl-0.6.3-cp310-cp310-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2585867ae003433f597daecc8c27672d558d2975f71b3218d7297feead14083 |
|
MD5 | 8e7300343fab43c7fad5a109413a15f8 |
|
BLAKE2b-256 | 2945798ed9e93a729f91b0c0690618b63e4fca06f1e77b0a6cf09cae39debe20 |
Hashes for pycddl-0.6.3-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b08730dd7666acf867326ea67a8350b32820cbad58e55db9679ef41783701ba1 |
|
MD5 | cbfed10eedd428d2c786521c262cb8e4 |
|
BLAKE2b-256 | ece765e1823617d29ab0fbcf7f556c05ea8941934ab0e25b391b313210252899 |
Hashes for pycddl-0.6.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8bba18c6cede2bd2f05355605f33a870a4c85eebf284fee8b19a2e15c175bbe |
|
MD5 | ab646f26bf12bbda6ff6ab99b503a9d2 |
|
BLAKE2b-256 | 42331aaa8fd89a4f4ff8e1940f76ce44387c0c0149776ac50e358d4f9dc674d8 |
Hashes for pycddl-0.6.3-cp39-cp39-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eab9f26d4036f042273e43e5bb6230a6c8637d2d2d56831dbe4a6ac7acfe07e5 |
|
MD5 | 33868ef2c1d648a1616fbb54f52a430a |
|
BLAKE2b-256 | 9afb43e9e791027eca37ed837a476b4fda93de886680ddc68a052c5a219cca04 |
Hashes for pycddl-0.6.3-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bd57741be0ae21861cf35e966d66ec077abc604087b4f20139e3e06d2b4a8ab |
|
MD5 | e442ac69b19fbd7caac1c7dd96e28af9 |
|
BLAKE2b-256 | 0ba99f198707b972dc552689bb41fcf96b20e723f7ff4017856fd84f2f91fb91 |
Hashes for pycddl-0.6.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d17ffa1e36b81306e3bd21f522c3f89282c581193a3bd98b6d2a1eb1b106177c |
|
MD5 | fcc8f9551a3c3db1fa3b3432ad2c8b1a |
|
BLAKE2b-256 | 5df4176e175e8f882b9649a036f98b31e70b4c4721a7895de41fef1e94c6cdd9 |
Hashes for pycddl-0.6.3-cp38-cp38-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c882ea6ae0d2daf145acbac166c020e87617f78110c1cdb66854a25c0f63cf1b |
|
MD5 | a6cec51642e8d4c9b13a904a56f1d51c |
|
BLAKE2b-256 | c9d32efd59f1fc0a4d8dc1f160d66f1d65a72b557494ab7a1bfc0a4d4357b040 |