Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved global decoder dicts to decoder object #225

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 48 additions & 49 deletions cbor2/_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _decode(self, immutable: bool = False, unshared: bool = False) -> Any:
initial_byte = self.read(1)[0]
major_type = initial_byte >> 5
subtype = initial_byte & 31
decoder = major_decoders[major_type]
decoder = self.major_decoders[major_type]
return decoder(self, subtype)
finally:
if immutable:
Expand Down Expand Up @@ -454,7 +454,7 @@ def decode_map(self, subtype: int) -> Mapping[Any, Any]:
def decode_semantic(self, subtype: int) -> Any:
# Major tag 6
tagnum = self._decode_length(subtype)
if semantic_decoder := semantic_decoders.get(tagnum):
if semantic_decoder := self.semantic_decoders.get(tagnum):
return semantic_decoder(self)

tag = CBORTag(tagnum, None)
Expand All @@ -473,7 +473,7 @@ def decode_special(self, subtype: int) -> Any:

# Major tag 7
try:
return special_decoders[subtype](self)
return self.special_decoders[subtype](self)
except KeyError as e:
raise CBORDecodeValueError(
"Undefined Reserved major type 7 subtype 0x%x" % subtype
Expand Down Expand Up @@ -736,52 +736,51 @@ def decode_float32(self) -> float:
def decode_float64(self) -> float:
return self.set_shareable(cast(float, struct.unpack(">d", self.read(8))[0]))


major_decoders: dict[int, Callable[[CBORDecoder, int], Any]] = {
0: CBORDecoder.decode_uint,
1: CBORDecoder.decode_negint,
2: CBORDecoder.decode_bytestring,
3: CBORDecoder.decode_string,
4: CBORDecoder.decode_array,
5: CBORDecoder.decode_map,
6: CBORDecoder.decode_semantic,
7: CBORDecoder.decode_special,
}

special_decoders: dict[int, Callable[[CBORDecoder], Any]] = {
20: lambda self: False,
21: lambda self: True,
22: lambda self: None,
23: lambda self: undefined,
24: CBORDecoder.decode_simple_value,
25: CBORDecoder.decode_float16,
26: CBORDecoder.decode_float32,
27: CBORDecoder.decode_float64,
31: lambda self: break_marker,
}

semantic_decoders: dict[int, Callable[[CBORDecoder], Any]] = {
0: CBORDecoder.decode_datetime_string,
1: CBORDecoder.decode_epoch_datetime,
2: CBORDecoder.decode_positive_bignum,
3: CBORDecoder.decode_negative_bignum,
4: CBORDecoder.decode_fraction,
5: CBORDecoder.decode_bigfloat,
25: CBORDecoder.decode_stringref,
28: CBORDecoder.decode_shareable,
29: CBORDecoder.decode_sharedref,
30: CBORDecoder.decode_rational,
35: CBORDecoder.decode_regexp,
36: CBORDecoder.decode_mime,
37: CBORDecoder.decode_uuid,
100: CBORDecoder.decode_epoch_date,
256: CBORDecoder.decode_stringref_namespace,
258: CBORDecoder.decode_set,
260: CBORDecoder.decode_ipaddress,
261: CBORDecoder.decode_ipnetwork,
1004: CBORDecoder.decode_date_string,
55799: CBORDecoder.decode_self_describe_cbor,
}
major_decoders: dict[int, Callable[[CBORDecoder, int], Any]] = {
0: decode_uint,
1: decode_negint,
2: decode_bytestring,
3: decode_string,
4: decode_array,
5: decode_map,
6: decode_semantic,
7: decode_special,
}

special_decoders: dict[int, Callable[[CBORDecoder], Any]] = {
20: lambda self: False,
21: lambda self: True,
22: lambda self: None,
23: lambda self: undefined,
24: decode_simple_value,
25: decode_float16,
26: decode_float32,
27: decode_float64,
31: lambda self: break_marker,
}

semantic_decoders: dict[int, Callable[[CBORDecoder], Any]] = {
0: decode_datetime_string,
1: decode_epoch_datetime,
2: decode_positive_bignum,
3: decode_negative_bignum,
4: decode_fraction,
5: decode_bigfloat,
25: decode_stringref,
28: decode_shareable,
29: decode_sharedref,
30: decode_rational,
35: decode_regexp,
36: decode_mime,
37: decode_uuid,
100: decode_epoch_date,
256: decode_stringref_namespace,
258: decode_set,
260: decode_ipaddress,
261: decode_ipnetwork,
1004: decode_date_string,
55799: decode_self_describe_cbor,
}


def loads(
Expand Down