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.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.2-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6be835a9adb99c88119be4d32e88de013b84ee4eed84c5fe5b3915442f625c79 |
|
MD5 | e3659c4f7a25ab6f2ffc7ccc4d1b5f50 |
|
BLAKE2b-256 | dfd75013b12952f7d0c2d7d27bf5c8750268aea084de04960dc828208ea89c28 |
Hashes for pycddl-0.6.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dea652a13ddd6393d18bf0988bcdeae35f3f0caeee2acfd4e614a15965f26fb |
|
MD5 | 288974cb1ce9e26939c2711b42eee63f |
|
BLAKE2b-256 | 76302c5492bd5ea3c020c2898c3b9ca1e181965ab1c4c97c31aec3f25d29249d |
Hashes for pycddl-0.6.2-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e438843f73c56032f78f5bda4a01e7c1572963abf8b9ad5fd527987dde33983 |
|
MD5 | 82aefdc166c62219f835f69e857aaed2 |
|
BLAKE2b-256 | bdac040894e2c21cd152f2ad0b222a0b59ede5331bbe6e5a210390a690033102 |
Hashes for pycddl-0.6.2-cp313-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 417472bf4054e85fd49d40fd048165c46c7b28c635349bb07384bf8e16b778d3 |
|
MD5 | 6ccaa5a0bb4bf8d567402dffe917f669 |
|
BLAKE2b-256 | 72cc91ce2e3def2c56eda58c0e25ffc570a24d763eadf18268ec63cef8a68c31 |
Hashes for pycddl-0.6.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bebf6baf91ec1ab7d6a2c5cfb8b125b3fb7944363e988b5acbb607c0089025c2 |
|
MD5 | aa4a361c456ac391b96691cfb1ab6d45 |
|
BLAKE2b-256 | 32c796ed0293614fb20d1ada547675f89dff6ae8af2e7e9bbee4c6f0d107c075 |
Hashes for pycddl-0.6.2-cp313-cp313-macosx_11_0_universal2.macosx_11_0_x86_64.macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07c565d6ab7b9bf066784ced2cfe5380f6808865df5a78a1c227d27ce51dc057 |
|
MD5 | 6a2078e0547299360ffd90e987731b10 |
|
BLAKE2b-256 | 7d5d01e37dcc36e19ef9cf8c83c844f82675d404200cfee4e06ae0281e192771 |
Hashes for pycddl-0.6.2-cp312-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1cc9343f99f50efa340e4f5a29f228955d90fab243000677d505ffb816fe33e |
|
MD5 | c5dd94765ccc243733a265ce1b4efea8 |
|
BLAKE2b-256 | cb3c6eefd658d3885fe4a0ad3b4f5310e27a1d5a6e03a2dc5a993dd5ff2f7763 |
Hashes for pycddl-0.6.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9327592a60572b134ea64eb64420c672366cce3c26a16fde17ed03ea4c73bd77 |
|
MD5 | c8009e3613d221db2656de5ea9ca751c |
|
BLAKE2b-256 | acc708c725a8413e33ff34ca49f3975fc6d53f45f1ae774903366910ad730713 |
Hashes for pycddl-0.6.2-cp312-cp312-macosx_11_0_arm64.macosx_11_0_universal2.macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6349a920beb379281a20488fc80e6fb37fb386299b21fcd384236c5c5e88b9c4 |
|
MD5 | a918561aacfca560e775d928813a2540 |
|
BLAKE2b-256 | b556033f325a0887c3c49941d975c9fdb933630b8de38aa2afa77cf841fc4dac |
Hashes for pycddl-0.6.2-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88a76a06f36f0ba54b65ec976199e4ef344b5779308ece0822bf6c52a3fbce27 |
|
MD5 | 31610cfd235ce506b9d888e16833752d |
|
BLAKE2b-256 | f1d3905b3316840089cc8d735355e271206188417f5e66b09863bfb82df716ef |
Hashes for pycddl-0.6.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ee9368de0a05ff707182e681862f6b6ee0a0c050f0a566144125ddd15012f17 |
|
MD5 | 495e282e78753050a88f8c5b97342002 |
|
BLAKE2b-256 | 7d016d1b0f7b013c2fc7a173c20169d9db6fef0f1e3f78740aeea9033cb57e68 |
Hashes for pycddl-0.6.2-cp311-cp311-macosx_11_0_universal2.macosx_11_0_x86_64.macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e3d27c2930d02501b8bbc27730df6e6165c5c50681fbe6b49f5c325a07117ce |
|
MD5 | 66e7bf58486eed81ec619d4cea0e76ac |
|
BLAKE2b-256 | 7dd846dc5414c42117ae35c4e7b7de5c43fa5dd3ec2ee1b687c7dcc26ef1ce4b |
Hashes for pycddl-0.6.2-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fe3ec0c818fad05ea5bc494e29cf9813e4f51d0b5a195dd65f7e0dbb604768f |
|
MD5 | 9f36c9eb4927d74b88cc8ebead1aabd0 |
|
BLAKE2b-256 | f9d09f31fb85ba395b4b90e6f8cb206c598a5dd798f482afe3db4637e2d8df74 |
Hashes for pycddl-0.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f7bca40274d2d62bc03b9eb2e504e4efac751150f60a4b94f4674285176b69b |
|
MD5 | 07243a5555fc694c2e7934561a913956 |
|
BLAKE2b-256 | dae68af3e365a7481b34569211bd39665e123e9b0a7426ededd565c9da11b8e6 |
Hashes for pycddl-0.6.2-cp310-cp310-macosx_11_0_x86_64.macosx_11_0_universal2.macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd174b163ebb4e60b671bbcb154d6a29327b399f836b95a30fd026b88bd7ef18 |
|
MD5 | 0fbf2fd6690003bff8f8045b584849cd |
|
BLAKE2b-256 | 81b1791813973092973a841dbcd5caf266d306431db3f6abd4279cdde27e4db6 |
Hashes for pycddl-0.6.2-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc9b368dc047ef997fd666b9540f254ac66d898fa0bfdd84088f03a04d9cc2c6 |
|
MD5 | 7d97ce777c6315448c6ac49d7d8b8e90 |
|
BLAKE2b-256 | 170218c26b94b9178aa47d0c6b5ddb810b50cb2a36366fdc398710943f72a2fd |
Hashes for pycddl-0.6.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba1dc7a60a9af54da18023744c87da07a9ec05dc711c0273a186bd62298556b5 |
|
MD5 | 12c74281f2318a2956e258b1d077d938 |
|
BLAKE2b-256 | 2c80250518da69d842c342bdb5010e86f5a288c50862c63e578c42665b998a8d |
Hashes for pycddl-0.6.2-cp39-cp39-macosx_11_0_universal2.macosx_11_0_x86_64.macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e909ec0e1c3b59200e2edf07b8039b80d5ee88b863e5997869ef965d6e43a8 |
|
MD5 | ce16e577351a6059e6c29c510d3b94a8 |
|
BLAKE2b-256 | c5c09b233a988df6aedd8be13445bf3544aab93034a6ac70491de187285c8eba |
Hashes for pycddl-0.6.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65be6cf4b54dd2f94074371452755f5b8a72cfd58eb6eb2fac15410e5f7f4863 |
|
MD5 | 840401bfd3277ada2126adb016328fb8 |
|
BLAKE2b-256 | 5fb5bfbc52eff7b0312bc85b52926277ca2eefab4f3a9799c5ba77d7ffc60537 |
Hashes for pycddl-0.6.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4db8f7d16301803c8d3e356eae7a8b49844e9eb8b6e26fc07876175c7c4d1af9 |
|
MD5 | 5228f7f6df3372f1476f0cb86839f98d |
|
BLAKE2b-256 | 7ea151a8596149c751819638ed2afb589a94203467ca50fb9fcd6c512e9be663 |
Hashes for pycddl-0.6.2-cp38-cp38-macosx_11_0_arm64.macosx_11_0_x86_64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 455778592fc8c308c52e1d6dd6fa27e19913753e58e228bb9e22225138fd6265 |
|
MD5 | 4f114e14ab3245aae5cd448fb9797498 |
|
BLAKE2b-256 | 3054822d502567e332026e7556d3d9a8d0208bdcd0662f9b8570a4e621d08304 |