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.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.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa393eb80f14dedb31f16c709fba692b708366ed46197bc6d80981ce90df67d7 |
|
MD5 | 2cb29097387949d2851db7386d13e14a |
|
BLAKE2b-256 | c505c8b5cc1ee211b42688f99ef55efb537740cf00fb00d059aeb118aac3486a |
Hashes for pycddl-0.6.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a37268a49a64a764f896d76e9f624a13ce5d3c9ae48ff1cf9c9b685fb91f9a7 |
|
MD5 | ea296f46621b0c8f63d6d236cbd58b4b |
|
BLAKE2b-256 | 81f0750623283ba15aae3da804bb36fed062596d6105988c2dbbc817947cbe2b |
Hashes for pycddl-0.6.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfb6544fdd9a97846dd04ed559a93a60cb8a9f7c6c8c9cd56bbea73a995abe96 |
|
MD5 | 26d0e4413fbcb16d084a3d21e5162ba3 |
|
BLAKE2b-256 | d821624290b7c0c9380d37234aa8e2562e16ffa8d4cdd5ad51ad84f43f49b98e |
Hashes for pycddl-0.6.0-cp312-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3c5f122dbc290ec3f944cf74169d47f39bbcc660b9077951c7a3d2debfc17db |
|
MD5 | 0917d1b2ec361e6749726de19e8b5ae7 |
|
BLAKE2b-256 | 48750015a11b60f77139d28a56fdab9b39af727ac0e6f21f25439b7a3880dd69 |
Hashes for pycddl-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00b6b016a8a1b0bd5e49c6225af6c3fc23ae4a312de271283b06f1f7e2fda84e |
|
MD5 | b0f863e7b629e1e939881efeebce6675 |
|
BLAKE2b-256 | 0b40098dc2f690a38ebfabd09634ed5b77219b2fe612dff51bc045aee478bdf4 |
Hashes for pycddl-0.6.0-cp312-cp312-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a062caa9952ccfcc8089c9121818744d4c20ca80c7d5b25732200aab6ad05743 |
|
MD5 | b5540c29cf4f1763b73248896563511b |
|
BLAKE2b-256 | 94c2deb5f576567270fd9c00892bef24b998e6792851bb7294e1d110b60b4d8e |
Hashes for pycddl-0.6.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8220e7878579c0a2c55adf19ae08609150f4ade5b0bd727854f765c42421088 |
|
MD5 | 0b287f7134b34c5515fca2707df0518e |
|
BLAKE2b-256 | c774fbe5e710bb038f150e0d3c16cc4b93a7c94b2550f19e5a7ceb1d286e5207 |
Hashes for pycddl-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2670f2fd04f9fd3298180a1e0022a472a1c2e1a2e63846089e40a37ea27f52da |
|
MD5 | 347eef5f8294b538f890ada3896fbb2f |
|
BLAKE2b-256 | 7f207e35bcec8371289c3ae96b38f7c7e9778721dcc6e4ade312b7c0b80ea1dc |
Hashes for pycddl-0.6.0-cp311-cp311-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a176938a9df8a6dedb77323b203544453fb193c4721cfb1a9dee08bda03f60d |
|
MD5 | d916e71444d5613bd0cfa8aeb10e1450 |
|
BLAKE2b-256 | 513582b65b49eaa2721b255aa6ac13f435a64a83291422e35efe25996ded2c23 |
Hashes for pycddl-0.6.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4c733278c50794b59300cf574e5b1447cd7cb26833acc6ba08647418fcb18f3 |
|
MD5 | 901ab19625273b188c12babad3de09e3 |
|
BLAKE2b-256 | 157d90048fb6c94d306d3d0267c1a041590f197fe85fc824052308012941041d |
Hashes for pycddl-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3e81fb0fcccb6042168ea3a08c57644d2668c951af71cc784d7b16f5e21fe1c |
|
MD5 | 89f891a0ad7be19501ee16d6d432649e |
|
BLAKE2b-256 | 509603e0bde156de5bf8d85c8d3144cd79d01050036311f7fbde5a209e531ae3 |
Hashes for pycddl-0.6.0-cp310-cp310-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901feab0ca25a32e10878b5cf0bf21d020482210bc85ad891533f1321ed1675a |
|
MD5 | 568ee50d7458cf638bb62164a31f4ab6 |
|
BLAKE2b-256 | 81d13658da726a0bb5c1f655c9c7cee0378c8e7077442563e2b337bf816f7b2f |
Hashes for pycddl-0.6.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 663186bf5a47c096fd2546cfa59b25fc3533867521e038481f3cd34a1ea729a2 |
|
MD5 | 0625d6af8bcdff346a7da5ad6e18b8b4 |
|
BLAKE2b-256 | 2a86f2a4c775fc55526ee2bf6c0739681239306fdeff71c425ea028ca9c437a2 |
Hashes for pycddl-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22570ac3ff0aea810d8b6332cd4a8d85b80c958398e6a17f0b437241c6b8de08 |
|
MD5 | 0679f6fabe1a359616088ee30a2b1f98 |
|
BLAKE2b-256 | cd2c522cffabd36a0656923e37abae38cd1299c407e152fd357c3b86c63222e9 |
Hashes for pycddl-0.6.0-cp39-cp39-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1908682d61b6c18cb090a97dda002eff47f907ec509a9c16918c1ff4cabfdb03 |
|
MD5 | ce26afabfe653d357c461a066c3366a4 |
|
BLAKE2b-256 | 58385499981ed050848ad653cdbf9c00c96f5c02488ad42b6e71b35b743165da |
Hashes for pycddl-0.6.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 195f7b4b230ca21b74e53b09c660d00a7a59fd26555f05fc0ba597c833cd172b |
|
MD5 | ebc5333ee9d3f1265d541a5e25955954 |
|
BLAKE2b-256 | 5edbdae5abf863351d540a70dbd0b4bf67cbaaa7041d1f31f0d0aea95bcaa826 |
Hashes for pycddl-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ad42890f6566c930a623489c4c9c30d77bde8788523acf63e3f59c4b68054a3 |
|
MD5 | 11fcfcf965458f46761c43f89517e13e |
|
BLAKE2b-256 | 7e91a0d0a41835e9ca8f8264cab6dbbd9d7bee670455802c8feec0dc365c8628 |
Hashes for pycddl-0.6.0-cp38-cp38-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11a6392214ec869f725fcfd5a23b7498a701c4769c53004e65b0b5d638b5431a |
|
MD5 | e4d551b436f3650c642d47bc20d2e877 |
|
BLAKE2b-256 | 49f9e1cdc6a90a55012043e607d0966b0d388a47025180134d576150a0d6db9b |