Centralize cbor dumping and loading, for better control.

This commit is contained in:
Itamar Turner-Trauring 2024-01-24 07:55:57 -05:00
parent 2cc9ceabcd
commit 1db76da2e2
4 changed files with 14 additions and 10 deletions

View File

@ -26,8 +26,6 @@ from attrs import define, asdict, frozen, field
from eliot import start_action, register_exception_extractor
from eliot.twisted import DeferredContext
# TODO Make sure to import Python version?
from cbor2 import loads, dumps
from pycddl import Schema
from collections_extended import RangeMap
from werkzeug.datastructures import Range, ContentRange
@ -65,6 +63,7 @@ from ..util.hashutil import timing_safe_compare
from ..util.deferredutil import async_to_deferred
from ..util.tor_provider import _Provider as TorProvider
from ..util.cputhreadpool import defer_to_thread
from ..util.cbor import dumps, loads
try:
from txtorcon import Tor # type: ignore

View File

@ -57,8 +57,6 @@ from hyperlink import DecodedURL
from cryptography.x509 import load_pem_x509_certificate
# TODO Make sure to use pure Python versions?
import cbor2
from pycddl import Schema, ValidationError as CDDLValidationError
from .server import StorageServer
from .http_common import (
@ -75,7 +73,8 @@ from ..util.hashutil import timing_safe_compare
from ..util.base32 import rfc3548_alphabet
from ..util.deferredutil import async_to_deferred
from ..util.cputhreadpool import defer_to_thread
from allmydata.interfaces import BadWriteEnablerError
from ..util import cbor
from ..interfaces import BadWriteEnablerError
class ClientSecretsException(Exception):
@ -647,7 +646,7 @@ async def read_encoded(
# Typically deserialization to Python will not release the GIL, and
# indeed as of Jan 2023 cbor2 didn't have any code to release the GIL
# in the decode path. As such, running it in a different thread has no benefit.
return cbor2.load(request.content)
return cbor.load(request.content)
class HTTPServer(BaseApp):
@ -695,7 +694,7 @@ class HTTPServer(BaseApp):
if accept.best == CBOR_MIME_TYPE:
request.setHeader("Content-Type", CBOR_MIME_TYPE)
f = TemporaryFile()
cbor2.dump(data, f) # type: ignore
cbor.dump(data, f) # type: ignore
def read_data(offset: int, length: int) -> bytes:
f.seek(offset)

View File

@ -24,7 +24,6 @@ from contextlib import contextmanager
from os import urandom
from typing import Union, Callable, Tuple, Iterable
from queue import Queue
from cbor2 import dumps
from pycddl import ValidationError as CDDLValidationError
from hypothesis import assume, given, strategies as st, settings as hypothesis_settings
from fixtures import Fixture, TempDir, MonkeyPatch
@ -41,8 +40,8 @@ from werkzeug import routing
from werkzeug.exceptions import NotFound as WNotFound
from testtools.matchers import Equals
from zope.interface import implementer
import cbor2
from ..util.cbor import dumps, loads
from ..util.deferredutil import async_to_deferred
from ..util.cputhreadpool import disable_thread_pool_for_test
from .common import SyncTestCase
@ -1845,5 +1844,5 @@ class MutableSharedTests(SharedImmutableMutableTestsMixin, SyncTestCase):
for size in range(0, 65535*2, 17):
self.assertEqual(
size,
len(cbor2.loads(cbor2.dumps(b"\12" * size)))
len(loads(dumps(b"\12" * size)))
)

View File

@ -0,0 +1,7 @@
"""
Unified entry point for CBOR encoding and decoding.
"""
from cbor2 import dumps, loads, dump, load
__all__ = ["dumps", "loads", "dump", "load"]