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.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.1.tar.gz (23.8 kB view details)

Uploaded Source

Built Distributions

pycddl-0.6.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp312-none-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.12 Windows x86-64

pycddl-0.6.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp312-cp312-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (2.4 MB view details)

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

pycddl-0.6.1-cp311-none-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.11 Windows x86-64

pycddl-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp311-cp311-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (2.4 MB view details)

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

pycddl-0.6.1-cp310-none-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.10 Windows x86-64

pycddl-0.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp310-cp310-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (2.4 MB view details)

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

pycddl-0.6.1-cp39-none-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.9 Windows x86-64

pycddl-0.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp39-cp39-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (2.4 MB view details)

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

pycddl-0.6.1-cp38-none-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.8 Windows x86-64

pycddl-0.6.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pycddl-0.6.1-cp38-cp38-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl (2.4 MB view details)

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

File details

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

File metadata

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

File hashes

Hashes for pycddl-0.6.1.tar.gz
Algorithm Hash digest
SHA256 eb77def14257107eade25eee8d5f090ef9466fbab790be9f1c701315d925cc57
MD5 b671b7fe869a8734b1819f0b6b6732d2
BLAKE2b-256 bc4311eabf4498e040002e0ebf667f5b030ca039b8c1b429b8fbc22cc09effc9

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 71c6aea188812df532eb748c2b9c2a29609f2e8ff139da501bb0bab90468f686
MD5 bc5aa630d25f8d475d80af1575584119
BLAKE2b-256 623c2f3e989039cd70cc0d93179e65bb0b6992b613b622f51d4e7458c3bf987c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3434f4b3521e11676e2dc3d277c3b507f58e180ffb823940f3a38bed5cc80800
MD5 94aacb879c2d7d0d15a70d27208d3d08
BLAKE2b-256 b95bd686ef81461f7c29b5e0725e6c96e1c99655f8150e8cbccf6f8b3d47b7d8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ead2f631bd58434cbecf0b6a24a0c8664f8c962c02a2c08ab585bb65c106b96f
MD5 10b42c57be854d53f5190f9d0facc1cd
BLAKE2b-256 d0f21505a2ebcd6b811687e294f531087b1f79c460a6583828a687a0dd58dee9

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 e885fd603eb7aea6ccee756531c0af0a217c593a70e7b512d32271d366559ac7
MD5 afd06b6a3798c848ce1560a6d9eab4de
BLAKE2b-256 4ca5307d6f3ce5d72cf3207684a8235283950131f37766cccbe574e48aabe493

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 96eacb46453385c303754297a9ba7b218de779eb439702a51a4289c87d7d5086
MD5 b0a5f940397a7294ab6a3ecff9ec8d87
BLAKE2b-256 d0da0b1fa6e51bf365d4b442c45933c125f9e6cdf2fcff1cde58b8ff2dea362c

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp312-cp312-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp312-cp312-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 a549cdf6efea4e4602e0e7f67250679c79e0e34029748f45ea7cfe348f4509df
MD5 ef3777e19a8b120911bf642b84302806
BLAKE2b-256 f5b0f0294f3ba2c273f0d076ecdd0f011e4f108268fde3acc7cbff4e26d8c00f

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 7f497e264613a2eda2663e93bdc0566b09b82172c8ae293a4192d05994e89e72
MD5 b34b36b622d1ede341cdbc9caa83fe7a
BLAKE2b-256 d0868c9015e3c7d790d669d4e41b16452634ed8cee96f88379993fc29287ed22

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12a5aa2c86bf73e638822969bf6f51bbd3e1781c023b8bf17492d2a0cb67b54b
MD5 9b0128d50a018ad48d68e369fcf81377
BLAKE2b-256 71feb5502a9be95ef43dbf080f4beb042b6540aee99cca75aad0155d49b24c04

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp311-cp311-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp311-cp311-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 7cb3c1cc0fee98ec701e64bda306e9e07a70cfd7d55ce85bc135ca7e8135b152
MD5 8d38ea4b2ff9585c0dff0744f201978d
BLAKE2b-256 77c0409a07db146b3b43e359cc76a5adedeea9896188c710bfead41ec45b2419

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 33a5579c0d627c3de8575fea25672c82a56b8ffa2c0af28a99882bf4935be251
MD5 2a737173ec752009ad28985d635a15b9
BLAKE2b-256 be3c081ed73bb913a920e3abe2dfe41f1d3016461ffd3a94943078d735f15756

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a92d6e43853dd6a0c63ce0e6ba84123e07ea6fac83016667e6d6d622dd9f55c6
MD5 00d9548560f520d821e2da810d1c4504
BLAKE2b-256 6742ef21b44e12a47c23efb74d0328f15a20ec65d86afd8dae4edfe9809a0de4

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp310-cp310-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp310-cp310-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 ada36f12ff4a5c6e96e0c40d5809af70af46ae06f59809c1a8e8552fb3c6e235
MD5 a5f11b3491beb7cde1e0f592a990fcb8
BLAKE2b-256 7b1a0e66a67344f9353696aef16725c4d9e77b503fb897aa4e750a44d37d4e46

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp39-none-win_amd64.whl.

File metadata

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

File hashes

Hashes for pycddl-0.6.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 58be95c2cdb67e13539ad5cf6d4f250fa36e633bd4118ec488ba01693cb8616d
MD5 4a80373a8c3e16c5ea885ba8275ca478
BLAKE2b-256 ab71977839c33a935e0cef49b18e4eadbb581692b34c037d90ca3e085e4f3f18

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2f4d0b1e6bb84044b27ca0477c2eec3b583ad946b8df61f089ac9998ea677f19
MD5 3ec01040dca27869d019f2bad802c17e
BLAKE2b-256 7df893cf4bb44d000f04010fc1a62da77892a69dc51ce7bc2012a1b1b0dbbba6

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp39-cp39-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp39-cp39-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 2925f8286fca29b8f41566e3944bdf808c0e2664cbb2e37e8c16657f6680efcf
MD5 f4975a261fc9609fbe7cb01e8ff0e8e9
BLAKE2b-256 00c4a85d51971673366b0f6c737a10635d678c88b754e22098e2ac9aac4ca962

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp38-none-win_amd64.whl.

File metadata

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

File hashes

Hashes for pycddl-0.6.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 ba0ae83a699d4106ff2335d6f899c2520502961b1cdf17c83a56c8f4cafc635a
MD5 f079f26fe8a55e77010790a3b87b40fc
BLAKE2b-256 8b35144c7b68e95a1dc42043e2f879187a380e5d50fe81a0d35caa3388d46340

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pycddl-0.6.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4557309fb37b5b73c44cd2cb834c26476cdf7ef104d3183d8ab509595441cde8
MD5 f2c49450fd321e988dcfc2baa6ec4a91
BLAKE2b-256 5d68ce94f037d80b137e3268e4a4da1d4d637d089fd560a91a3d19af11e1a630

See more details on using hashes here.

Provenance

File details

Details for the file pycddl-0.6.1-cp38-cp38-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pycddl-0.6.1-cp38-cp38-macosx_11_0_x86_64.macosx_11_0_arm64.macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 a15a1a01910106bb30813e6b36a9713d68a4484bc4ac8e60e59e4cd4610f39ef
MD5 5cd6b9cbe9365c0850deb3c51a91e086
BLAKE2b-256 c03e683a555d92fcac1e367d3ad2425c2ac89b6037a9538dcec9b50ad391cd2e

See more details on using hashes here.

Provenance

Supported by

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