Skip to main content

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.4

Features:

  • Update Rust CDDL dependency to update transitive dependency lexical-core which had been revised (i.a.) because of soundness/safety issues.

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 just bytes. This is useful if you want to e.g. validate a large file, since you can mmap() 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 to Schema for easier debugging.

0.1.11

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycddl-0.6.4.tar.gz (24.6 kB view details)

Uploaded Source

Built Distributions

pycddl-0.6.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

pycddl-0.6.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

pycddl-0.6.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp313-cp313-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.13Windows x86-64

pycddl-0.6.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp313-cp313-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.13macOS 11.0+ universal2 (ARM64, x86-64)

pycddl-0.6.4-cp312-cp312-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.12Windows x86-64

pycddl-0.6.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp312-cp312-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ universal2 (ARM64, x86-64)

pycddl-0.6.4-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11Windows x86-64

pycddl-0.6.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp311-cp311-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ universal2 (ARM64, x86-64)

pycddl-0.6.4-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10Windows x86-64

pycddl-0.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp310-cp310-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ universal2 (ARM64, x86-64)

pycddl-0.6.4-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9Windows x86-64

pycddl-0.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp39-cp39-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 11.0+ universal2 (ARM64, x86-64)

pycddl-0.6.4-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8Windows x86-64

pycddl-0.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

pycddl-0.6.4-cp38-cp38-macosx_11_0_universal2.whl (2.6 MB view details)

Uploaded CPython 3.8macOS 11.0+ universal2 (ARM64, x86-64)

File details

Details for the file pycddl-0.6.4.tar.gz.

File metadata

  • Download URL: pycddl-0.6.4.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4.tar.gz
Algorithm Hash digest
SHA256 6946ba4377b5470bd637434fa9b26d596de8ff2cc9c5473483b81418a525397a
MD5 f80a2c5e7c5f97c8e10dd3116fafbc96
BLAKE2b-256 6be7f693e8243f4d19b86a770dea7c6c43a14d8722fb5827e01777dc1fccf362

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f2324b81e344d402131447d0bb2a4b1ffaae10a0d2d11c226acbc3b8156f9b37
MD5 bed444a27f4a81e0b78cf79b9bcb4655
BLAKE2b-256 2a5f4c6744a10d92a6ce7bf9e6853fa6cc65e7b0641e4d760b0185a42c8b7476

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99fd752db751e7c4a1c50ef448d7887610efa6afedcb0fa3dff767a3b4825017
MD5 3b0045ac831b0ee4c8105b972cc9edc5
BLAKE2b-256 3d8614b26caf148369aeac597e19da72fc17e8d87d556df9fc7c312988d7ce50

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f20c7ad8c2ee8703bb08e9dd071f03589ff7aaeaa27a8fc5072de40f66166e1
MD5 d21ca9850c24a6f1a5f3f8e67f77bb19
BLAKE2b-256 afd4ecded53b1396b8b37a44526611c132bc20fa3dfb2d8c2534746496eb3ac2

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8e723cb86cb3ce14a57ee369aee28a3e113b290a13905eadc56741ccafbfdbaa
MD5 67fa93b0adf14b5ce8a05bdc204c1c5b
BLAKE2b-256 68e1a591b84e414a2a100388c91bff20829ef9a8a9221409fe71072e1d05f62c

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8d6c9708685a2e0f189d8e89f1268b6ad4e1c68251ed39737cc67ed7bd866eec
MD5 ffe6005b2af2bb9f40cd5d9d4592afa6
BLAKE2b-256 3470fc167a501abd4fb2da8b79f236a5e20967de5dff061185de274db175184c

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp313-cp313-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp313-cp313-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 8e2ebc4531889d406ab7cb351cfb6479243d8e12d98694936a4c264694d67775
MD5 b9cd17a495cfaac264b2d4fefbb324d0
BLAKE2b-256 56c642aece761a57e2d681aebadf4467dfa7654e7365050b068667e837ee8e88

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4a114e6a0baa564bf8c81c9c1660a80b7d7acfa8ceaece4656cce797093624e0
MD5 f77d575434c5669453989444428aad48
BLAKE2b-256 af14be6e7f6660afb587c751fe11ac1e996811fda2f3ee17afd4c8e0b226f055

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39ca675f341a6a77654f4f6759bcb3ce0d47f1a772cb87e99b4e61d86bcb6053
MD5 50cb5b238b9743322a49491d2a0711c8
BLAKE2b-256 0c7f43d64f027a686e3af8462489f0c36ef83bfa4992e2dc5ec27897715305ac

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp312-cp312-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp312-cp312-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 314ef71197d49ec32e8763d759ab53a6962bea8d029d1b01992ecf3d9388a93b
MD5 9a2298053c4eb6150daf7b278a209d70
BLAKE2b-256 aa00a942365e10ed5dea6b962313dded051061b1bad63cd3ca7158d1998968e3

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5ba650b476a56ba2e0a9db4b70a3a651601f7176a0733aa64a43b45bdccaa85f
MD5 7c46d3102832f5822be491271684545e
BLAKE2b-256 1b57b19119099299674c9346b08924d6012245cf8c24fc8ed6d9f1f357414531

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 84870a702b519826b54b127742e784e745c53bf1373d05e0f96df0a89fc5ceca
MD5 53958744a7ca38162196ca58152a8eef
BLAKE2b-256 95b71a0f1f610e491967372c8ff301aea3cfcd731265e14e0e1014d9a4c020de

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp311-cp311-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp311-cp311-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 028d425d3ae41682bc606ed839fd93b97d787ce19bd50583c9ffaf542b7e68eb
MD5 04c728f7ec2b8c020ca7370e96e67e9a
BLAKE2b-256 7e2f34b1ba3477b1390d18bce8ba186e99ae5f7193148dbb1516a6076e049a25

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 91c59bb095a70d8714dc1859e06f29840575fd3edc6a0a871fed850fd524b306
MD5 b1f30eae85a9d287c4041b6ec1932f5c
BLAKE2b-256 07af4a51a36ab13743fb526a417d6fed87b61293d53425f18c5886f817c6aa06

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 22bc96c3bc261202b7f8652da74d2f8b84a4ba21aeaedb1d40f3d7c2f013d4c9
MD5 6df818f1159cdf8a20c887e788c4afbe
BLAKE2b-256 942675d12742a53edbea37ed9ea46776ad968b3958d46900d726f593ed729b6b

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp310-cp310-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp310-cp310-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 8809fad90afeff76bf28ad2de9ea157cc0033a2321d80c2e92858c4d0af2384f
MD5 1dc939f7b9a6acd49b2c4ef1255994ce
BLAKE2b-256 80d8fd98b8ecdde1575a53f8c104c39639a82c1bc310b63045a4f5b1a640225c

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 cd2a86b339a013402d90469e28a8b1b03d06c607e8a1e8db52e8d91f76ed859d
MD5 bef2e8304c893d6a09e3f4aa1300d7e4
BLAKE2b-256 faf1a4a037590b79b27335bbd0d5c8c39ff71642e59c4788664a314b1d205669

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f46649bfc665f7f1fa06b5c451dd6cd8f18899b12e5a56e5534a24d800bca541
MD5 16b16c31f3d314a0364704e6ab94232f
BLAKE2b-256 b002ecf1ffbb30f0baff03202db5bdec5658288deabe4f56822f363d938be35a

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp39-cp39-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp39-cp39-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 8a92bbce1de1d48ea8deeee0008eb3a81f0811c80174e3c7169dcf9bd5b2b696
MD5 f8c8abb71bd57479ccee4b744e17f781
BLAKE2b-256 43b3a63ea0a3478fb313b1de87e2f5699a1d6cb3cc499c2ed850e2841ec831a1

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pycddl-0.6.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for pycddl-0.6.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a1ed33d3427507ee274663c9ea0dea2f2cd3c8473ff1c4247970d4b5697ff330
MD5 309a696a0066642c85a686a6b5f24926
BLAKE2b-256 b7e59dec716cf59a7cd166486c15d76ba8fa9e36e388665fbdabb3ea272046df

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 61d079fff3f29198ed18c5435ed50abc6e98f17f0e0c3805f94ee791577d09e1
MD5 4b19e876564be4517f5e343937096d24
BLAKE2b-256 b79a79ad4ae9a2f7fbee3f0f133403845a4431edc2caff799820bb31cd3ee9bb

See more details on using hashes here.

File details

Details for the file pycddl-0.6.4-cp38-cp38-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.4-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 3c2696412b50381e679085f9c76ae1ccbc1fd6538a5c4c6b6349f9d906893d4b
MD5 b374b065bdc05263217d3ee9751af2c6
BLAKE2b-256 5fc984cf87fbeeb102baa1780921ab27ca1eedcd969a073b403375400e878d0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page