• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

agronholm / cbor2 / 25236661772

01 May 2026 11:40AM UTC coverage: 94.324% (+0.7%) from 93.596%
25236661772

push

github

agronholm
Added the allow_duplicate_keys decoding parameter

Closes #283.

45 of 46 new or added lines in 2 files covered. (97.83%)

55 existing lines in 3 files now uncovered.

2310 of 2449 relevant lines covered (94.32%)

1288.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.79
/rust/decoder.rs
1
use crate::_cbor2::{BREAK_MARKER, SYS_MAXSIZE, UNDEFINED};
2
use crate::decoder::DecoderResult::{
3
    BeginFrame, CompleteFrame, ContinueFrame, Shareable, SharedReference, StringNamespace,
4
    StringReference, StringValue, Value,
5
};
6
#[cfg(not(Py_3_15))]
7
use crate::types::FrozenDict;
8
use crate::types::{
9
    CBORDecodeEOF, CBORDecodeError, CBORSimpleValue, CBORTag, DECIMAL_TYPE, FRACTION_TYPE,
10
    IPV4ADDRESS_TYPE, IPV4INTERFACE_TYPE, IPV4NETWORK_TYPE, IPV6ADDRESS_TYPE, IPV6INTERFACE_TYPE,
11
    IPV6NETWORK_TYPE, UUID_TYPE,
12
};
13
use crate::utils::{PyImportable, create_exc_from, raise_exc_from};
14
use half::f16;
15
use pyo3::exceptions::{PyException, PyLookupError, PyTypeError, PyValueError};
16
use pyo3::prelude::*;
17
use pyo3::sync::PyOnceLock;
18
use pyo3::types::{
19
    PyBytes, PyCFunction, PyComplex, PyDict, PyFrozenSet, PyInt, PyList, PyListMethods, PyMapping,
20
    PySet, PyString, PyTuple,
21
};
22
use pyo3::{IntoPyObjectExt, Py, PyAny, PyErrArguments, intern, pyclass};
23
use std::fmt::{Display, Formatter};
24
use std::mem::{replace, take};
25

26
const IMMUTABLE_ATTR: &str = "_cbor2_immutable";
27
const NAME_ATTR: &str = "_cbor2_name";
28
const SEEK_CUR: u8 = 1;
29

30
static DATE_FROMISOFORMAT: PyImportable = PyImportable::new("datetime", "date.fromisoformat");
31
static DATE_FROMORDINAL: PyImportable = PyImportable::new("datetime", "date.fromordinal");
32
static DATETIME_FROMISOFORMAT: PyImportable =
33
    PyImportable::new("datetime", "datetime.fromisoformat");
34
static DATETIME_FROMTIMESTAMP: PyImportable =
35
    PyImportable::new("datetime", "datetime.fromtimestamp");
36
static EMAIL_PARSER: PyImportable = PyImportable::new("email.parser", "Parser");
37
static INCREMENTAL_UTF8_DECODER: PyOnceLock<Py<PyAny>> = PyOnceLock::new();
38
static INT_FROMBYTES: PyImportable = PyImportable::new("builtins", "int.from_bytes");
39
static IPADDRESS_FUNC: PyImportable = PyImportable::new("ipaddress", "ip_address");
40
static IPNETWORK_FUNC: PyImportable = PyImportable::new("ipaddress", "ip_network");
41
static IPINTERFACE_FUNC: PyImportable = PyImportable::new("ipaddress", "ip_interface");
42
static RE_COMPILE: PyImportable = PyImportable::new("re", "compile");
43
static UTC: PyImportable = PyImportable::new("datetime", "timezone.utc");
44
#[cfg(Py_3_15)]
45
static FROZEN_DICT: PyImportable = PyImportable::new("builtins", "frozendict");
46

47
enum DecoderResult<'a> {
48
    BeginFrame(
49
        Box<DecoderCallback<'a>>,
50
        bool,
51
        Option<Bound<'a, PyAny>>,
52
        DisplayName<'a>,
53
    ),
54
    ContinueFrame(bool),
55
    CompleteFrame(Bound<'a, PyAny>),
56
    Value(Bound<'a, PyAny>),
57
    StringValue(Bound<'a, PyAny>, usize),
58
    StringNamespace,
59
    StringReference(usize),
60
    Shareable,
61
    SharedReference(usize),
62
}
63

64
enum DisplayName<'a> {
65
    String(&'static str),
66
    SemanticTag(usize),
67
    PythonName(Bound<'a, PyAny>),
68
}
69

70
impl<'a> Display for DisplayName<'a> {
71
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
516✔
72
        match self {
516✔
73
            DisplayName::String(s) => f.write_str(s),
492✔
74
            DisplayName::SemanticTag(tagnum) => write!(f, "semantic tag {}", tagnum),
12✔
75
            DisplayName::PythonName(obj) => write!(f, "{}", obj),
12✔
76
        }
77
    }
516✔
78
}
79

80
type DecoderCallback<'py> =
81
    dyn 'py + FnMut(Bound<'py, PyAny>, bool) -> PyResult<DecoderResult<'py>>;
82

83
struct StackFrame<'py> {
84
    immutable: bool,
85
    decoder_callback: Option<Box<DecoderCallback<'py>>>,
86
    shareable_index: Option<usize>,
87
    typename: DisplayName<'py>,
88
    contains_string_namespace: bool,
89
}
90

91
/// Decorates a function to be a two-stage decoder.
92
///
93
/// :param name: the name displayed in a :exc:`CBORDecodeError` raised by the decoder
94
///     (e.g. "error decoding thingamajig") where name='thingamajig`)
95
/// :param immutable: :data:`True` if the item sent to the decoder should be decoded as immutable
96
#[pyfunction]
97
#[pyo3(signature = (func=None, /, *, name=None, immutable=false))]
98
pub fn shareable_decoder<'py>(
120✔
99
    py: Python<'py>,
120✔
100
    func: Option<Py<PyAny>>,
120✔
101
    name: Option<Py<PyString>>,
120✔
102
    immutable: bool,
120✔
103
) -> PyResult<Bound<'py, PyAny>> {
120✔
104
    match func {
120✔
105
        None => PyCFunction::new_closure(
60✔
106
            py,
60✔
107
            None,
60✔
108
            None,
60✔
109
            move |args: &Bound<'_, PyTuple>,
110
                  _kwargs: Option<&Bound<'_, PyDict>>|
111
                  -> PyResult<Py<PyAny>> {
60✔
112
                let py = args.py();
60✔
113
                let func = args.get_item(0)?;
60✔
114
                let name = name.as_ref().map(|x| x.clone_ref(py));
60✔
115
                shareable_decoder(py, Some(func.unbind()), name, immutable).map(Bound::unbind)
60✔
116
            },
60✔
117
        )
118
        .map(|f| f.into_any()),
60✔
119
        Some(func) => {
60✔
120
            let bound_func = func.bind(py);
60✔
121
            if !bound_func.is_callable() {
60✔
122
                return Err(PyTypeError::new_err(format!("{func} is not callable")));
×
123
            }
60✔
124
            bound_func.setattr(intern!(py, NAME_ATTR), name)?;
60✔
125
            bound_func.setattr(intern!(py, IMMUTABLE_ATTR), immutable)?;
60✔
126
            Ok(bound_func.clone().into_any())
60✔
127
        }
128
    }
129
}
120✔
130

131
fn require_tuple<'py>(value: Bound<'py, PyAny>, length: usize) -> PyResult<Bound<'py, PyTuple>> {
800✔
132
    let array: Bound<'py, PyTuple> = value
800✔
133
        .cast_into()
800✔
134
        .map_err(|_| PyTypeError::new_err("input value must be an array"))?;
800✔
135
    if array.len() != length {
764✔
136
        return Err(PyValueError::new_err(format!(
×
137
            "expected an array with exactly {length} elements"
×
138
        )));
×
139
    }
764✔
140
    Ok(array)
764✔
141
}
800✔
142

143
/// The CBORDecoder class implements a fully featured `CBOR`_ decoder with
144
/// several extensions for handling shared references, big integers, rational
145
/// numbers and so on. Typically, the class is not used directly, but the
146
/// :func:`load` and :func:`loads` functions are called to indirectly construct
147
/// and use the class.
148
///
149
/// When the class is constructed manually, the main entry point is :meth:`decode`.
150
///
151
/// :param fp: the file to read from (any file-like object opened for reading in binary mode)
152
/// :param tag_hook:
153
///     callable that takes 2 arguments: the decoder instance, and the :class:`.CBORTag`
154
///     to be decoded. This callback is invoked for any tags for which there is no
155
///     built-in decoder. The return value is substituted for the :class:`.CBORTag`
156
///     object in the deserialized output
157
/// :param object_hook:
158
///     callable that takes 2 arguments: the decoder instance, and a dictionary. This
159
///     callback is invoked for each deserialized :class:`dict` object. The return value
160
///     is substituted for the dict in the deserialized output.
161
/// :param semantic_decoders:
162
///     An optional mapping for overriding the decoding for select semantic tags.
163
///     The value is a mapping of semantic tags (integers) to callables that take
164
///     the decoder instance as the sole argument.
165
/// :param str_errors:
166
///     determines how to handle Unicode decoding errors (see the `Error Handlers`_
167
///     section in the standard library documentation for details)
168
/// :param read_size: minimum number of bytes to read at once
169
///     (ignored if ``fp`` is not seekable)
170
/// :param max_depth:
171
///     maximum allowed depth for nested containers
172
/// :param allow_indefinite:
173
///     if :data:`False`, raise a :exc:`CBORDecodeError` when encountering an indefinite-length
174
///     string or container in the input stream
175
/// :param allow_duplicate_keys:
176
///     if :data:`False`, raise a :exc:`CBORDecodeError` when a map key that has already been
177
///     decoded in the same map is encountered
178
///
179
/// .. _CBOR: https://cbor.io/
180
#[pyclass(module = "cbor2")]
181
pub struct CBORDecoder {
182
    fp: Option<Py<PyAny>>,
183
    tag_hook: Option<Py<PyAny>>,
184
    object_hook: Option<Py<PyAny>>,
185
    semantic_decoders: Option<Py<PyMapping>>,
186
    str_errors: Option<Py<PyString>>,
187
    #[pyo3(get)]
188
    read_size: usize,
189
    #[pyo3(get)]
190
    max_depth: usize,
191
    #[pyo3(get)]
192
    allow_indefinite: bool,
193
    #[pyo3(get)]
194
    allow_duplicate_keys: bool,
195

196
    read_method: Option<Py<PyAny>>,
197
    buffer: Option<Py<PyBytes>>,
198
    read_position: usize,
199
    available_bytes: usize,
200
    fp_is_seekable: bool,
201
}
202

203
impl CBORDecoder {
204
    pub fn new_internal(
4,536✔
205
        py: Python<'_>,
4,536✔
206
        fp: Option<&Bound<'_, PyAny>>,
4,536✔
207
        buffer: Option<Bound<PyBytes>>,
4,536✔
208
        tag_hook: Option<&Bound<'_, PyAny>>,
4,536✔
209
        object_hook: Option<&Bound<'_, PyAny>>,
4,536✔
210
        semantic_decoders: Option<&Bound<'_, PyMapping>>,
4,536✔
211
        str_errors: &str,
4,536✔
212
        read_size: usize,
4,536✔
213
        max_depth: usize,
4,536✔
214
        allow_indefinite: bool,
4,536✔
215
        allow_duplicate_keys: bool,
4,536✔
216
    ) -> PyResult<Self> {
4,536✔
217
        let available_bytes = if let Some(buffer) = buffer.as_ref() {
4,536✔
218
            buffer.len()?
4,128✔
219
        } else {
220
            0
408✔
221
        };
222
        let bound_str_errors = PyString::new(py, str_errors);
4,536✔
223
        let mut this = Self {
4,536✔
224
            fp: None,
4,536✔
225
            tag_hook: None,
4,536✔
226
            object_hook: None,
4,536✔
227
            str_errors: None,
4,536✔
228
            read_size,
4,536✔
229
            max_depth,
4,536✔
230
            allow_indefinite,
4,536✔
231
            allow_duplicate_keys,
4,536✔
232
            semantic_decoders: semantic_decoders.map(|d| d.clone().unbind()),
4,536✔
233
            read_method: None,
4,536✔
234
            buffer: buffer.map(Bound::unbind),
4,536✔
235
            read_position: 0,
236
            available_bytes,
4,536✔
237
            fp_is_seekable: false,
238
        };
239
        if let Some(fp) = fp {
4,536✔
240
            this.set_fp(fp)?
408✔
241
        };
4,128✔
242
        this.set_tag_hook(tag_hook)?;
4,512✔
243
        this.set_object_hook(object_hook)?;
4,500✔
244
        this.set_str_errors(&bound_str_errors)?;
4,488✔
245
        Ok(this)
4,476✔
246
    }
4,536✔
247

248
    fn read_from_fp<'py>(
384✔
249
        &mut self,
384✔
250
        py: Python<'py>,
384✔
251
        minimum_amount: usize,
384✔
252
    ) -> PyResult<(Bound<'py, PyBytes>, usize)> {
384✔
253
        let read_size: usize = if self.fp_is_seekable {
384✔
254
            self.read_size
252✔
255
        } else {
256
            1
132✔
257
        };
258
        let bytes_to_read = minimum_amount.max(read_size);
384✔
259
        let num_read_bytes = if let Some(read) = self.read_method.as_ref() {
384✔
260
            let bytes_from_fp: Bound<PyBytes> =
276✔
261
                read.bind(py).call1((&bytes_to_read,))?.cast_into()?;
276✔
262
            let num_read_bytes = bytes_from_fp.len()?;
276✔
263
            if num_read_bytes >= minimum_amount {
276✔
264
                return Ok((bytes_from_fp, num_read_bytes));
228✔
265
            }
48✔
266
            num_read_bytes
48✔
267
        } else {
268
            0
108✔
269
        };
270
        Err(CBORDecodeEOF::new_err(format!(
156✔
271
            "premature end of stream (expected to read at least {minimum_amount} \
156✔
272
                 bytes, got {num_read_bytes} instead)"
156✔
273
        )))
156✔
274
    }
384✔
275

276
    fn read_exact<const N: usize>(&mut self, py: Python<'_>) -> PyResult<[u8; N]> {
27,364✔
277
        if self.available_bytes == 0 {
27,364✔
278
            // No buffer
279
            let (new_bytes, amount_read) = self.read_from_fp(py, N)?;
240✔
280
            self.read_position = N;
216✔
281
            self.available_bytes = amount_read - N;
216✔
282
            self.buffer = Some(new_bytes.unbind());
216✔
283
            Ok(self.buffer.as_ref().unwrap().as_bytes(py)[..N].try_into()?)
216✔
284
        } else if self.available_bytes < N {
27,124✔
285
            // Combine the remnants of the partial buffer with new data read from the file
286
            let needed_bytes = N - self.available_bytes;
×
287
            let mut concatenated_buffer: Vec<u8> = self.buffer.take().unwrap().extract(py)?;
×
288
            let (new_bytes, amount_read) = self.read_from_fp(py, needed_bytes)?;
×
289
            concatenated_buffer.extend_from_slice(&new_bytes[..needed_bytes]);
×
290
            self.buffer = Some(new_bytes.unbind());
×
291
            self.available_bytes = amount_read - needed_bytes;
×
292
            self.read_position = needed_bytes;
×
293
            Ok(concatenated_buffer.try_into().unwrap())
×
294
        } else {
295
            // Return a slice from the existing bytes object
296
            let slice: [u8; N] = self.buffer.as_ref().unwrap().bind(py).as_bytes()
27,124✔
297
                [self.read_position..self.read_position + N]
27,124✔
298
                .try_into()?;
27,124✔
299
            self.available_bytes -= N;
27,124✔
300
            self.read_position += N;
27,124✔
301
            Ok(slice)
27,124✔
302
        }
303
    }
27,364✔
304

305
    fn read_major_and_subtype(&mut self, py: Python<'_>) -> PyResult<(u8, u8)> {
22,236✔
306
        let initial_byte = self.read_exact::<1>(py)?[0];
22,236✔
307
        let major_type = initial_byte >> 5;
22,212✔
308
        let subtype = initial_byte & 31;
22,212✔
309
        Ok((major_type, subtype))
22,212✔
310
    }
22,236✔
311

312
    fn decode_length_finite(&mut self, py: Python<'_>, subtype: u8) -> PyResult<usize> {
7,468✔
313
        match self.decode_length(py, subtype)? {
7,468✔
314
            Some(length) => Ok(length),
7,432✔
315
            None => Err(CBORDecodeError::new_err(
24✔
316
                "indefinite length not allowed here",
24✔
317
            )),
24✔
318
        }
319
    }
7,468✔
320
    //
321
    // Decoders for major tags (0-7)
322
    //
323

324
    /// Decode the length of the next item.
325
    ///
326
    /// This is a low-level operation that may be needed by custom decoder callbacks.
327
    ///
328
    /// :param subtype:
329
    /// :return: the length of the item, or :data:`None` to indicate an indefinite-length item
330
    fn decode_length(&mut self, py: Python<'_>, subtype: u8) -> PyResult<Option<usize>> {
19,704✔
331
        let length = match subtype {
19,704✔
332
            ..24 => Some(subtype as usize),
19,704✔
333
            24 => Some(self.read_exact::<1>(py)?[0] as usize),
2,088✔
334
            25 => Some(u16::from_be_bytes(self.read_exact(py)?) as usize),
1,160✔
335
            26 => Some(u32::from_be_bytes(self.read_exact(py)?) as usize),
300✔
336
            27 => Some(u64::from_be_bytes(self.read_exact(py)?) as usize),
284✔
337
            31 => {
338
                if !self.allow_indefinite {
384✔
339
                    return Err(CBORDecodeError::new_err(
12✔
340
                        "encountered indefinite length but it has been disabled",
12✔
341
                    ));
12✔
342
                }
372✔
343
                None
372✔
344
            }
345
            _ => {
346
                return Err(CBORDecodeError::new_err(format!(
12✔
347
                    "unknown unsigned integer subtype 0x{subtype:x}"
12✔
348
                )));
12✔
349
            }
350
        };
351
        Ok(length)
19,680✔
352
    }
19,704✔
353

354
    fn decode_uint<'py>(&mut self, py: Python<'py>, subtype: u8) -> PyResult<DecoderResult<'py>> {
3,684✔
355
        // Major tag 0
356
        let uint = self.decode_length_finite(py, subtype)?;
3,684✔
357
        Ok(Value(uint.into_bound_py_any(py)?))
3,672✔
358
    }
3,684✔
359

360
    fn decode_negint<'py>(&mut self, py: Python<'py>, subtype: u8) -> PyResult<DecoderResult<'py>> {
464✔
361
        // Major tag 1
362
        let uint = self.decode_length_finite(py, subtype)?;
464✔
363
        let signed_int = -(uint as i128) - 1;
464✔
364
        Ok(Value(signed_int.into_bound_py_any(py)?))
464✔
365
    }
464✔
366

367
    fn decode_bytestring<'py>(
1,128✔
368
        &mut self,
1,128✔
369
        py: Python<'py>,
1,128✔
370
        subtype: u8,
1,128✔
371
    ) -> PyResult<DecoderResult<'py>> {
1,128✔
372
        // Major tag 2
373
        match self.decode_length(py, subtype)? {
1,128✔
374
            None => {
375
                // Indefinite length
376
                let mut bytes = PyBytes::new(py, b"");
72✔
377
                let sys_maxsize = *SYS_MAXSIZE.get(py).unwrap();
72✔
378
                loop {
379
                    let (major_type, subtype) = self.read_major_and_subtype(py)?;
120✔
380
                    match (major_type, subtype) {
120✔
381
                        (2, _) => {
382
                            let length = self.decode_length_finite(py, subtype)?;
84✔
383
                            if length > sys_maxsize {
72✔
384
                                return Err(CBORDecodeError::new_err(format!(
12✔
385
                                    "chunk too long in an indefinite bytestring chunk: {length}"
12✔
386
                                )));
12✔
387
                            }
60✔
388
                            let chunk = self.read(py, length)?;
60✔
389
                            bytes = bytes.add(chunk)?.cast_into()?;
48✔
390
                        }
391
                        (7, 31) => break Ok(Value(bytes.into_any())), // break marker
12✔
392
                        _ => {
393
                            return Err(CBORDecodeError::new_err(format!(
24✔
394
                                "non-byte string (major type {major_type}) found in indefinite \
24✔
395
                                    length byte string"
24✔
396
                            )));
24✔
397
                        }
398
                    }
399
                }
400
            }
401
            Some(length) if length <= 65536 => {
1,056✔
402
                let bytes = self.read(py, length)?;
1,032✔
403
                Ok(StringValue(PyBytes::new(py, &bytes).into_any(), length))
996✔
404
            }
405
            Some(length) => {
24✔
406
                // Incrementally read the bytestring, in chunks of 65536 bytes
407
                let mut bytes = PyBytes::new(py, b"");
24✔
408
                let mut remaining_length = length;
24✔
409
                while remaining_length > 0 {
48✔
410
                    let chunk_size = remaining_length.min(65536);
36✔
411
                    let chunk = self.read(py, chunk_size)?;
36✔
412
                    remaining_length -= chunk_size;
24✔
413
                    bytes = bytes.add(chunk)?.cast_into()?;
24✔
414
                }
415
                Ok(StringValue(bytes.into_any(), length))
12✔
416
            }
417
        }
418
    }
1,128✔
419

420
    fn decode_string<'py>(&mut self, py: Python<'py>, subtype: u8) -> PyResult<DecoderResult<'py>> {
2,280✔
421
        // Major tag 3
422
        match self.decode_length(py, subtype)? {
2,280✔
423
            None => {
424
                // Indefinite length
425
                let mut string = PyString::new(py, "");
96✔
426
                loop {
427
                    let (major_type, subtype) = self.read_major_and_subtype(py)?;
168✔
428
                    let sys_maxsize = *SYS_MAXSIZE.get(py).unwrap();
168✔
429
                    match (major_type, subtype) {
168✔
430
                        (3, _) => {
431
                            let length = self.decode_length_finite(py, subtype)?;
120✔
432
                            if length > sys_maxsize {
108✔
433
                                return Err(CBORDecodeError::new_err(format!(
12✔
434
                                    "chunk too long in an indefinite text string chunk: {length}"
12✔
435
                                )));
12✔
436
                            }
96✔
437
                            let bytes = self.read(py, length)?;
96✔
438
                            let decoded = match self.str_errors.as_ref() {
84✔
439
                                None => PyString::from_bytes(py, bytes.as_slice()),
84✔
440
                                Some(str_errors) => bytes
×
441
                                    .into_bound_py_any(py)?
×
442
                                    .call_method1(
×
443
                                        intern!(py, "decode"),
×
444
                                        (intern!(py, "utf-8"), str_errors),
×
445
                                    )
446
                                    .and_then(|string| string.cast_into().map_err(PyErr::from)),
×
447
                            }?;
12✔
448
                            string = string.add(decoded)?.cast_into()?;
72✔
449
                        }
450
                        (7, 31) => break Ok(Value(string.into_any())), // break marker
24✔
451
                        _ => {
452
                            return Err(CBORDecodeError::new_err(format!(
24✔
453
                                "non-text string (major type {major_type}) found in indefinite \
24✔
454
                                    length text string"
24✔
455
                            )));
24✔
456
                        }
457
                    }
458
                }
459
            }
460
            Some(length) if length <= 65536 => {
2,172✔
461
                let bytes = self.read(py, length)?;
2,124✔
462
                let decoded_string: Bound<'_, PyAny> = match self.str_errors.as_ref() {
2,076✔
463
                    None => PyString::from_bytes(py, bytes.as_slice())?.into_any(),
2,052✔
464
                    Some(str_errors) => bytes.into_bound_py_any(py)?.call_method1(
24✔
465
                        intern!(py, "decode"),
24✔
466
                        (intern!(py, "utf-8"), str_errors.bind(py)),
24✔
UNCOV
467
                    )?,
×
468
                };
469
                Ok(StringValue(decoded_string, length))
2,040✔
470
            }
471
            Some(mut length) => {
48✔
472
                // Incrementally decode the string, in chunks of 65536 bytes
473
                let decoder_class = INCREMENTAL_UTF8_DECODER
48✔
474
                    .get_or_try_init(py, || -> PyResult<Py<PyAny>> {
48✔
475
                        let decoder = py
12✔
476
                            .import("codecs")?
12✔
477
                            .getattr("lookup")?
12✔
478
                            .call1(("utf-8",))?
12✔
479
                            .getattr("incrementaldecoder")?;
12✔
480
                        Ok(decoder.unbind())
12✔
481
                    })?
12✔
482
                    .bind(py);
48✔
483
                let decoder = match self.str_errors.as_ref() {
48✔
484
                    None => decoder_class.call0()?,
24✔
485
                    Some(str_errors) => decoder_class.call1((str_errors,))?,
24✔
486
                };
487
                let mut string = PyString::new(py, "").into_any();
48✔
488
                while length > 0 {
168✔
489
                    let chunk_size = length.min(65536);
120✔
490
                    let chunk = self.read(py, chunk_size)?;
120✔
491
                    length -= chunk_size;
120✔
492
                    let is_final_chunk = length == 0;
120✔
493
                    let decoded_chunk =
120✔
494
                        decoder.call_method1(intern!(py, "decode"), (chunk, is_final_chunk))?;
120✔
495
                    string = string.add(decoded_chunk)?;
120✔
496
                }
497
                Ok(StringValue(string.into_any(), length))
48✔
498
            }
499
        }
500
    }
2,280✔
501

502
    fn decode_array<'py>(
7,036✔
503
        &mut self,
7,036✔
504
        py: Python<'py>,
7,036✔
505
        subtype: u8,
7,036✔
506
        immutable: bool,
7,036✔
507
    ) -> PyResult<DecoderResult<'py>> {
7,036✔
508
        // Major tag 4
509
        let optional_length = self.decode_length(py, subtype)?;
7,036✔
510
        if immutable {
7,036✔
511
            let mut items: Vec<Bound<'py, PyAny>> = Vec::new();
1,192✔
512
            let callback: Box<DecoderCallback<'py>> = if let Some(length) = optional_length {
1,192✔
513
                if length == 0 {
1,168✔
514
                    return Ok(Value(PyTuple::empty(py).into_any()));
24✔
515
                }
1,144✔
516

517
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
2,440✔
518
                    items.push(item);
2,440✔
519
                    if items.len() == length {
2,440✔
520
                        Ok(CompleteFrame(
521
                            PyTuple::new(py, take(&mut items))?.into_any(),
1,096✔
522
                        ))
523
                    } else {
524
                        Ok(ContinueFrame(false))
1,344✔
525
                    }
526
                })
2,440✔
527
            } else {
528
                let break_marker = BREAK_MARKER.get(py).unwrap().bind(py);
24✔
529
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
72✔
530
                    if item.is(break_marker) {
72✔
531
                        Ok(CompleteFrame(
532
                            PyTuple::new(py, take(&mut items))?.into_any(),
12✔
533
                        ))
534
                    } else {
535
                        items.push(item);
60✔
536
                        Ok(ContinueFrame(false))
60✔
537
                    }
538
                })
72✔
539
            };
540
            Ok(BeginFrame(
1,168✔
541
                callback,
1,168✔
542
                false,
1,168✔
543
                None,
1,168✔
544
                DisplayName::String("array"),
1,168✔
545
            ))
1,168✔
546
        } else {
547
            let mut list = PyList::empty(py);
5,844✔
548
            let container = list.clone().into_any();
5,844✔
549
            let callback: Box<DecoderCallback<'py>> = if let Some(length) = optional_length {
5,844✔
550
                if length == 0 {
5,748✔
551
                    return Ok(Value(PyList::empty(py).into_any()));
104✔
552
                }
5,644✔
553

554
                Box::new(move |item, _immutable: bool| {
5,644✔
555
                    list.append(item)?;
2,056✔
556
                    if list.len() == length {
2,056✔
557
                        Ok(CompleteFrame(
676✔
558
                            replace(&mut list, PyList::empty(py)).into_any(),
676✔
559
                        ))
676✔
560
                    } else {
561
                        Ok(ContinueFrame(false))
1,380✔
562
                    }
563
                })
2,056✔
564
            } else {
565
                let break_marker = BREAK_MARKER.get(py).unwrap().bind(py);
96✔
566
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
564✔
567
                    if item.is(break_marker) {
564✔
568
                        Ok(CompleteFrame(
96✔
569
                            replace(&mut list, PyList::empty(py)).into_any(),
96✔
570
                        ))
96✔
571
                    } else {
572
                        list.append(item)?;
468✔
573
                        Ok(ContinueFrame(false))
468✔
574
                    }
575
                })
564✔
576
            };
577
            Ok(BeginFrame(
5,740✔
578
                callback,
5,740✔
579
                false,
5,740✔
580
                Some(container),
5,740✔
581
                DisplayName::String("array"),
5,740✔
582
            ))
5,740✔
583
        }
584
    }
7,036✔
585

586
    fn decode_map<'py>(
1,792✔
587
        &mut self,
1,792✔
588
        py: Python<'py>,
1,792✔
589
        subtype: u8,
1,792✔
590
        immutable: bool,
1,792✔
591
    ) -> PyResult<DecoderResult<'py>> {
1,792✔
592
        // Major tag 5
593

594
        #[cfg(Py_3_15)]
595
        fn create_frozen_dict<'py>(
12✔
596
            py: Python<'py>,
12✔
597
            items: Vec<(Bound<'py, PyAny>, Bound<'py, PyAny>)>,
12✔
598
        ) -> PyResult<Bound<'py, PyAny>> {
12✔
599
            FROZEN_DICT
12✔
600
                .get(py)?
12✔
601
                .call1((items,))?
12✔
602
                .cast_into()
12✔
603
                .map_err(|e| PyErr::from(e))
12✔
604
        }
12✔
605
        #[cfg(not(Py_3_15))]
606
        fn create_frozen_dict<'py>(
132✔
607
            py: Python<'py>,
132✔
608
            items: Vec<(Bound<'py, PyAny>, Bound<'py, PyAny>)>,
132✔
609
        ) -> PyResult<Bound<'py, PyAny>> {
132✔
610
            FrozenDict::from_items(py, items).map(|dict| dict.into_any())
132✔
611
        }
132✔
612

613
        #[inline]
614
        fn maybe_call_object_hook<'py>(
1,588✔
615
            py: Python<'py>,
1,588✔
616
            dict: Bound<'py, PyAny>,
1,588✔
617
            object_hook: Option<&Py<PyAny>>,
1,588✔
618
            immutable: bool,
1,588✔
619
        ) -> PyResult<Bound<'py, PyAny>> {
1,588✔
620
            if let Some(object_hook) = object_hook {
1,588✔
621
                object_hook.bind(py).call1((dict, immutable))
24✔
622
            } else {
623
                Ok(dict)
1,564✔
624
            }
625
        }
1,588✔
626

627
        let object_hook = self.object_hook.as_ref().map(|hook| hook.clone_ref(py));
1,792✔
628
        let allow_duplicate_keys = self.allow_duplicate_keys;
1,792✔
629
        let length_or_none = self.decode_length(py, subtype)?;
1,792✔
630

631
        // Return immediately if this is an empty dict
632
        if let Some(length) = length_or_none
1,792✔
633
            && length == 0
1,732✔
634
        {
635
            let container: Bound<'py, PyAny> = if immutable {
256✔
UNCOV
636
                create_frozen_dict(py, Vec::new())?
×
637
            } else {
638
                PyDict::new(py).into_any()
256✔
639
            };
640
            let transformed =
256✔
641
                maybe_call_object_hook(py, container, object_hook.as_ref(), immutable)?;
256✔
642
            return Ok(Value(transformed));
256✔
643
        };
1,536✔
644

645
        let mut key: Option<Bound<'py, PyAny>> = None;
1,536✔
646
        if immutable {
1,536✔
647
            let seen_keys: Option<Bound<'py, PySet>> = if allow_duplicate_keys {
300✔
648
                None
276✔
649
            } else {
650
                Some(PySet::empty(py)?)
24✔
651
            };
652
            let check_duplicate = move |key: &Bound<'py, PyAny>| -> PyResult<()> {
300✔
653
                let seen = seen_keys.as_ref().unwrap();
48✔
654
                if seen.contains(key)? {
48✔
655
                    let repr = key.repr()?;
24✔
656
                    return Err(CBORDecodeError::new_err(format!(
24✔
657
                        "Duplicate map key: {}",
658
                        repr.to_str()?
24✔
659
                    )));
660
                }
24✔
661
                seen.add(key.clone())
24✔
662
            };
48✔
663

664
            let mut items: Vec<(Bound<'py, PyAny>, Bound<'py, PyAny>)> = Vec::new();
300✔
665
            let callback: Box<DecoderCallback<'py>> = if let Some(length) = length_or_none {
300✔
666
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
396✔
667
                    if let Some(key) = key.take() {
396✔
668
                        if !allow_duplicate_keys {
192✔
669
                            check_duplicate(&key)?;
24✔
670
                        }
168✔
671
                        items.push((key, item));
180✔
672
                        if items.len() == length {
180✔
673
                            let transformed = maybe_call_object_hook(
144✔
674
                                py,
144✔
675
                                create_frozen_dict(py, take(&mut items))?,
144✔
676
                                object_hook.as_ref(),
144✔
677
                                immutable,
144✔
UNCOV
678
                            )?;
×
679
                            return Ok(CompleteFrame(transformed));
144✔
680
                        }
36✔
681
                        Ok(ContinueFrame(true))
36✔
682
                    } else {
683
                        key = Some(item);
204✔
684
                        Ok(ContinueFrame(false))
204✔
685
                    }
686
                })
396✔
687
            } else {
688
                let break_marker = BREAK_MARKER.get(py).unwrap().bind(py);
12✔
689
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
48✔
690
                    if item.is(break_marker) {
48✔
691
                        let container = create_frozen_dict(py, take(&mut items))?;
×
692
                        let transformed = maybe_call_object_hook(
×
693
                            py,
×
694
                            container.into_any(),
×
695
                            object_hook.as_ref(),
×
696
                            immutable,
×
697
                        )?;
×
698
                        Ok(CompleteFrame(transformed))
×
699
                    } else if let Some(key) = key.take() {
48✔
700
                        if !allow_duplicate_keys {
24✔
701
                            check_duplicate(&key)?;
24✔
NEW
702
                        }
×
703
                        items.push((key, item));
12✔
704
                        Ok(ContinueFrame(true))
12✔
705
                    } else {
706
                        key = Some(item);
24✔
707
                        Ok(ContinueFrame(false))
24✔
708
                    }
709
                })
48✔
710
            };
711
            Ok(BeginFrame(callback, true, None, DisplayName::String("map")))
300✔
712
        } else {
713
            fn check_duplicate(key: &Bound<PyAny>, dict: &Bound<PyDict>) -> PyResult<()> {
48✔
714
                if dict.contains(key)? {
48✔
715
                    let repr = key.repr()?;
24✔
716
                    return Err(CBORDecodeError::new_err(format!(
24✔
717
                        "Duplicate map key: {}",
718
                        repr.to_str()?
24✔
719
                    )));
720
                }
24✔
721
                Ok(())
24✔
722
            }
48✔
723

724
            let mut dict = PyDict::new(py);
1,236✔
725
            let container = dict.clone().into_any();
1,236✔
726
            let callback: Box<DecoderCallback<'py>> = if let Some(length) = length_or_none {
1,236✔
727
                let mut count = 0usize;
1,188✔
728
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
3,536✔
729
                    if let Some(key) = key.take() {
3,536✔
730
                        if !allow_duplicate_keys {
1,768✔
731
                            check_duplicate(&key, &dict)?;
24✔
732
                        }
1,744✔
733
                        dict.set_item(&key, item)?;
1,756✔
734
                        count += 1;
1,756✔
735
                        if count == length {
1,756✔
736
                            let dict = replace(&mut dict, PyDict::new(py));
1,152✔
737
                            let transformed = maybe_call_object_hook(
1,152✔
738
                                py,
1,152✔
739
                                dict.into_any(),
1,152✔
740
                                object_hook.as_ref(),
1,152✔
741
                                immutable,
1,152✔
742
                            )?;
12✔
743
                            return Ok(CompleteFrame(transformed));
1,140✔
744
                        }
604✔
745
                        Ok(ContinueFrame(true))
604✔
746
                    } else {
747
                        key = Some(item);
1,768✔
748
                        Ok(ContinueFrame(false))
1,768✔
749
                    }
750
                })
3,536✔
751
            } else {
752
                let break_marker = BREAK_MARKER.get(py).unwrap().bind(py);
48✔
753
                Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
204✔
754
                    if item.is(break_marker) {
204✔
755
                        let dict = replace(&mut dict, PyDict::new(py));
36✔
756
                        let transformed = maybe_call_object_hook(
36✔
757
                            py,
36✔
758
                            dict.into_any(),
36✔
759
                            object_hook.as_ref(),
36✔
760
                            immutable,
36✔
UNCOV
761
                        )?;
×
762
                        Ok(CompleteFrame(transformed))
36✔
763
                    } else if let Some(key) = key.take() {
168✔
764
                        if !allow_duplicate_keys {
84✔
765
                            check_duplicate(&key, &dict)?;
24✔
766
                        }
60✔
767
                        dict.set_item(&key, item)?;
72✔
768
                        Ok(ContinueFrame(true))
72✔
769
                    } else {
770
                        key = Some(item);
84✔
771
                        Ok(ContinueFrame(false))
84✔
772
                    }
773
                })
204✔
774
            };
775
            Ok(BeginFrame(
1,236✔
776
                callback,
1,236✔
777
                true,
1,236✔
778
                Some(container),
1,236✔
779
                DisplayName::String("map"),
1,236✔
780
            ))
1,236✔
781
        }
782
    }
1,792✔
783

784
    fn decode_semantic<'py>(
3,116✔
785
        &mut self,
3,116✔
786
        py: Python<'py>,
3,116✔
787
        subtype: u8,
3,116✔
788
        immutable: bool,
3,116✔
789
    ) -> PyResult<DecoderResult<'py>> {
3,116✔
790
        let tagnum = self.decode_length_finite(py, subtype)?;
3,116✔
791
        if let Some(semantic_decoders) = &self.semantic_decoders {
3,116✔
792
            match semantic_decoders.bind(py).get_item(tagnum) {
120✔
793
                Ok(decoder) => {
96✔
794
                    let name = decoder.getattr_opt(intern!(py, NAME_ATTR))?;
96✔
795

796
                    // If these attributes are present, this callable was decorated with
797
                    // @shareable_decoder
798
                    return if let Some(name) = name {
96✔
799
                        let require_immutable: bool = decoder
60✔
800
                            .getattr_opt(intern!(py, IMMUTABLE_ATTR))?
60✔
801
                            .map(|x| x.is_truthy())
60✔
802
                            .transpose()?
60✔
803
                            .unwrap_or(false);
60✔
804
                        let retval = decoder.call1((immutable,))?;
60✔
805
                        let tuple: Bound<'_, PyTuple> = retval.cast_into()?;
60✔
806
                        if tuple.len() != 2 {
60✔
UNCOV
807
                            return Err(CBORDecodeError::new_err(format!(
×
UNCOV
808
                                "{decoder} returned a tuple of {} items, expected 2",
×
809
                                tuple.len()
×
810
                            )));
×
811
                        }
60✔
812
                        let container: Bound<'_, PyAny> = tuple.get_item(0)?.cast_into()?;
60✔
813
                        let callback: Bound<'_, PyAny> = tuple.get_item(1)?.cast_into()?;
60✔
814
                        Ok(BeginFrame(
815
                            Box::new(
60✔
816
                                move |item, _immutable: bool| -> PyResult<DecoderResult<'py>> {
60✔
817
                                    callback.call1((item,)).map(CompleteFrame)
60✔
818
                                },
60✔
819
                            ),
820
                            require_immutable,
60✔
821
                            if container.is_none() {
60✔
822
                                None
48✔
823
                            } else {
824
                                Some(container)
12✔
825
                            },
826
                            if name.is_none() {
60✔
827
                                DisplayName::SemanticTag(tagnum)
12✔
828
                            } else {
829
                                DisplayName::PythonName(name.clone())
48✔
830
                            },
831
                        ))
832
                    } else {
833
                        let callback =
36✔
834
                            move |item, new_immutable: bool| -> PyResult<DecoderResult<'py>> {
36✔
835
                                decoder.call1((item, new_immutable)).map(CompleteFrame)
36✔
836
                            };
36✔
837
                        Ok(BeginFrame(
36✔
838
                            Box::new(callback),
36✔
839
                            immutable,
36✔
840
                            None,
36✔
841
                            DisplayName::SemanticTag(tagnum),
36✔
842
                        ))
36✔
843
                    };
844
                }
845
                Err(e) if e.is_instance_of::<PyLookupError>(py) => {}
24✔
UNCOV
846
                Err(e) => return Err(e),
×
847
            }
848
        };
2,996✔
849

850
        // No semantic decoder lookup map – fall back to the hard coded switchboard
851
        let (callback, typename): (Box<DecoderCallback<'py>>, &str) = match tagnum {
3,020✔
852
            0 => (
488✔
853
                Box::new(Self::decode_datetime_string),
488✔
854
                "string-form datetime",
488✔
855
            ),
488✔
856
            1 => (Box::new(Self::decode_epoch_datetime), "epoch-form datetime"),
60✔
857
            2 => (Box::new(Self::decode_positive_bignum), "positive bignum"),
168✔
858
            3 => (Box::new(Self::decode_negative_bignum), "negative bignum"),
40✔
859
            4 => (Box::new(Self::decode_fraction), "decimal fraction"),
280✔
860
            5 => (Box::new(Self::decode_bigfloat), "bigfloat"),
24✔
861
            25 => (Box::new(Self::decode_stringref), "string reference"),
96✔
862
            28 => return Ok(Shareable),
240✔
863
            29 => (Box::new(Self::decode_sharedref), "shared reference"),
180✔
864
            30 => (Box::new(Self::decode_rational), "rational"),
244✔
865
            35 => (Box::new(Self::decode_regexp), "regular expression"),
36✔
866
            36 => (Box::new(Self::decode_mime), "MIME message"),
36✔
867
            37 => (Box::new(Self::decode_uuid), "UUID"),
208✔
868
            52 => (Box::new(Self::decode_ipv4), "IPv4 address"),
60✔
869
            54 => (Box::new(Self::decode_ipv6), "IPv6 address"),
60✔
870
            100 => (Box::new(Self::decode_epoch_date), "epoch-form date"),
12✔
871
            256 => return Ok(StringNamespace),
36✔
872
            258 => return self.decode_set(py, immutable),
200✔
873
            260 => (Box::new(Self::decode_ipaddress), "IP address"),
84✔
874
            261 => (Box::new(Self::decode_ipnetwork), "IP network"),
84✔
875
            1004 => (Box::new(Self::decode_date_string), "string-form date"),
12✔
876
            43000 => (Box::new(Self::decode_complex), "complex number"),
252✔
877
            55799 => (
24✔
878
                Box::new(Self::decode_self_describe_cbor),
24✔
879
                "self-described CBOR value",
24✔
880
            ),
24✔
881
            _ => {
882
                // For a tag with no designated decoder, check if we have a tag hook, and call
883
                // that with the tag object, using its return value as the decoded value.
884
                let tag = CBORTag::new(tagnum.into_bound_py_any(py)?, py.None().into_bound(py))?;
96✔
885
                let bound_tag = Bound::new(py, tag)?.into_any();
96✔
886
                let container = bound_tag.clone();
96✔
887
                let mut tag_hook = self
96✔
888
                    .tag_hook
96✔
889
                    .as_ref()
96✔
890
                    .map(|hook| hook.clone_ref(py).into_bound(py));
96✔
891
                let callback = Box::new(move |item: Bound<'py, PyAny>, _immutable: bool| {
96✔
892
                    let tag: &Bound<'py, CBORTag> = bound_tag.cast()?;
84✔
893
                    tag.borrow_mut().value = item.unbind();
84✔
894
                    if let Some(tag_hook) = tag_hook.take() {
84✔
895
                        tag_hook.call1((&bound_tag, immutable)).map(CompleteFrame)
60✔
896
                    } else {
897
                        Ok(CompleteFrame(bound_tag.clone()))
24✔
898
                    }
899
                });
84✔
900
                return Ok(BeginFrame(
96✔
901
                    callback,
96✔
902
                    true,
96✔
903
                    Some(container),
96✔
904
                    DisplayName::SemanticTag(tagnum),
96✔
905
                ));
96✔
906
            }
907
        };
908
        Ok(BeginFrame(
2,448✔
909
            callback,
2,448✔
910
            true,
2,448✔
911
            None,
2,448✔
912
            DisplayName::String(typename),
2,448✔
913
        ))
2,448✔
914
    }
3,116✔
915

916
    fn decode_special<'py>(
2,424✔
917
        &mut self,
2,424✔
918
        py: Python<'py>,
2,424✔
919
        subtype: u8,
2,424✔
920
    ) -> PyResult<DecoderResult<'py>> {
2,424✔
921
        // Major tag 7
922
        match subtype {
2,424✔
923
            0..20 => {
2,424✔
924
                let value = subtype.into_pyobject(py)?;
72✔
925
                CBORSimpleValue::new(value)?.into_bound_py_any(py)
72✔
926
            }
927
            20 => Ok(false.into_bound_py_any(py)?),
120✔
928
            21 => Ok(true.into_bound_py_any(py)?),
120✔
929
            22 => Ok(py.None().into_bound_py_any(py)?),
600✔
930
            23 => Ok(UNDEFINED.get(py).unwrap().into_bound_py_any(py)?),
24✔
931
            24 => {
932
                let value = self.read_exact::<1>(py)?[0];
84✔
933
                if value < 0x20 {
84✔
934
                    return Err(CBORDecodeError::new_err(
36✔
935
                        "invalid two-byte sequence for simple value",
36✔
936
                    ));
36✔
937
                }
48✔
938
                CBORSimpleValue::new(value.into_pyobject(py)?)?.into_bound_py_any(py)
48✔
939
            }
940
            25 => {
941
                let bytes = self.read_exact::<2>(py)?;
648✔
942
                f16::from_be_bytes(bytes).to_f32().into_bound_py_any(py)
648✔
943
            }
944
            26 => {
945
                let bytes = self.read_exact::<4>(py)?;
108✔
946
                f32::from_be_bytes(bytes).into_bound_py_any(py)
108✔
947
            }
948
            27 => {
949
                let bytes = self.read_exact::<8>(py)?;
456✔
950
                f64::from_be_bytes(bytes).into_bound_py_any(py)
456✔
951
            }
952
            31 => Ok(BREAK_MARKER.get(py).unwrap().into_bound_py_any(py)?),
156✔
953
            _ => Err(CBORDecodeError::new_err(format!(
36✔
954
                "undefined reserved major type 7 subtype 0x{subtype:x}"
36✔
955
            ))),
36✔
956
        }
957
        .map(Value)
2,388✔
958
    }
2,424✔
959

960
    //
961
    // Decoders for semantic tags (major tag 6)
962
    //
963

964
    fn decode_datetime_string<'py>(
488✔
965
        value: Bound<'py, PyAny>,
488✔
966
        _immutable: bool,
488✔
967
    ) -> PyResult<DecoderResult<'py>> {
488✔
968
        // Semantic tag 0
969
        let py = value.py();
488✔
970
        let value_type = value.get_type();
488✔
971
        let mut datetime_str: Bound<'py, PyString> = value.cast_into().map_err(|e| {
488✔
UNCOV
972
            create_exc_from(
×
UNCOV
973
                py,
×
974
                CBORDecodeError::new_err(format!(
×
975
                    "expected string for tag, got {} instead",
976
                    value_type
977
                )),
978
                Some(PyErr::from(e)),
×
979
            )
980
        })?;
×
981

982
        // Python 3.10 has impaired parsing of the ISO format:
983
        // * It doesn't handle the standard "Z" suffix
984
        // * It doesn't handle the fractional seconds part having fewer than 6 digits
985
        if py.version_info() <= (3, 10) {
488✔
986
            // Convert Z to +00:00
987
            let mut temp_str = datetime_str.to_string().replacen("Z", "+00:00", 1);
136✔
988

989
            // Pad any microseconds part with zeros
990
            if let Some((first, second)) = temp_str.split_once('.')
136✔
991
                && let Some(index) = second.find(|c: char| !c.is_numeric())
688✔
992
            {
993
                let (mut micros, tz_part) = second.split_at(index);
94✔
994
                // Cut off excess zeroes from the start of the microseconds part
995
                if micros.len() >= 6 {
94✔
996
                    micros = &micros[..6];
79✔
997
                }
79✔
998

999
                // Reconstitute the datetime string, right-padding the microseconds part
1000
                // with zeroes
1001
                temp_str = format!("{first}.{micros:0<6}{tz_part}");
94✔
1002
            }
42✔
1003

1004
            datetime_str = temp_str.into_pyobject(py)?;
136✔
1005
        }
352✔
1006

1007
        DATETIME_FROMISOFORMAT
488✔
1008
            .get(py)?
488✔
1009
            .call1((&datetime_str,))
488✔
1010
            .map(CompleteFrame)
488✔
1011
    }
488✔
1012

1013
    fn decode_epoch_datetime(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
60✔
1014
        // Semantic tag 1
1015
        let py = value.py();
60✔
1016
        let utc = UTC.get(py)?;
60✔
1017
        DATETIME_FROMTIMESTAMP
60✔
1018
            .get(py)?
60✔
1019
            .call1((value, utc))
60✔
1020
            .map(CompleteFrame)
60✔
1021
    }
60✔
1022

1023
    fn decode_positive_bignum(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
144✔
1024
        // Semantic tag 2
1025
        let py = value.py();
144✔
1026
        INT_FROMBYTES
144✔
1027
            .get(py)?
144✔
1028
            .call1((value, intern!(py, "big")))
144✔
1029
            .map(CompleteFrame)
144✔
1030
    }
144✔
1031

1032
    fn decode_negative_bignum(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
40✔
1033
        // Semantic tag 3
1034
        let py = value.py();
40✔
1035
        let int = INT_FROMBYTES.get(py)?.call1((value, intern!(py, "big")))?;
40✔
1036
        int.neg()?.add(-1).map(CompleteFrame)
40✔
1037
    }
40✔
1038

1039
    fn decode_fraction(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
280✔
1040
        // Semantic tag 4
1041
        let py = value.py();
280✔
1042
        let tuple = require_tuple(value, 2)?;
280✔
1043
        let decimal_class = DECIMAL_TYPE.get(py)?;
268✔
1044
        {
1045
            let exp = tuple.get_item(0)?;
268✔
1046
            let sig_tuple = decimal_class
268✔
1047
                .call1((tuple.get_item(1)?,))?
268✔
1048
                .call_method0(intern!(py, "as_tuple"))?
268✔
1049
                .cast_into::<PyTuple>()?;
268✔
1050
            let sign = sig_tuple.get_item(0)?;
268✔
1051
            let digits = sig_tuple.get_item(1)?;
268✔
1052
            let args_tuple = PyTuple::new(py, [sign, digits, exp])?;
268✔
1053
            decimal_class.call1((args_tuple,)).map(CompleteFrame)
268✔
1054
        }
1055
    }
280✔
1056

1057
    fn decode_bigfloat(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
24✔
1058
        // Semantic tag 5
1059
        let py = value.py();
24✔
1060
        let tuple = require_tuple(value, 2)?;
24✔
1061
        let decimal_class = DECIMAL_TYPE.get(py)?;
12✔
1062
        {
1063
            let exp = decimal_class.call1((tuple.get_item(0)?,))?;
12✔
1064
            let sig = decimal_class.call1((tuple.get_item(1)?,))?;
12✔
1065
            let exp = PyInt::new(py, 2).pow(exp, py.None())?;
12✔
1066
            sig.mul(exp).map(CompleteFrame)
12✔
1067
        }
1068
    }
24✔
1069

1070
    fn decode_stringref(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
96✔
1071
        // Semantic tag 25
1072
        let index: usize = value.extract()?;
96✔
1073
        Ok(StringReference(index))
96✔
1074
    }
96✔
1075

1076
    fn decode_sharedref(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
180✔
1077
        // Semantic tag 29
1078
        let index: usize = value.extract()?;
180✔
1079
        Ok(SharedReference(index))
180✔
1080
    }
180✔
1081

1082
    fn decode_rational(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
244✔
1083
        // Semantic tag 30
1084
        let py = value.py();
244✔
1085
        let tuple = require_tuple(value, 2)?;
244✔
1086
        FRACTION_TYPE.get(py)?.call1(tuple).map(CompleteFrame)
232✔
1087
    }
244✔
1088

1089
    fn decode_regexp(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
36✔
1090
        // Semantic tag 35
1091
        RE_COMPILE
36✔
1092
            .get(value.py())?
36✔
1093
            .call1((value,))
36✔
1094
            .map(CompleteFrame)
36✔
1095
    }
36✔
1096

1097
    fn decode_mime(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
24✔
1098
        // Semantic tag 36
1099
        let py = value.py();
24✔
1100
        let parser = EMAIL_PARSER.get(py)?.call0()?;
24✔
1101
        parser
24✔
1102
            .call_method1(intern!(py, "parsestr"), (value,))
24✔
1103
            .map(CompleteFrame)
24✔
1104
    }
24✔
1105

1106
    fn decode_uuid(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
208✔
1107
        // Semantic tag 37
1108
        let py = value.py();
208✔
1109
        let kwargs = PyDict::new(py);
208✔
1110
        kwargs.set_item(intern!(py, "bytes"), value)?;
208✔
1111
        UUID_TYPE
208✔
1112
            .get(py)?
208✔
1113
            .call((), Some(&kwargs))
208✔
1114
            .map(CompleteFrame)
208✔
1115
    }
208✔
1116

1117
    fn decode_ipv4(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
60✔
1118
        // Semantic tag 52
1119
        let py = value.py();
60✔
1120
        let addr = if let Ok(bytes) = value.cast::<PyBytes>() {
60✔
1121
            // The decoded value was a bytestring, so this is an IPv4 address
1122
            IPV4ADDRESS_TYPE.get(py)?.call1((bytes,))?
36✔
1123
        } else if let Ok(tuple) = value.cast_into::<PyTuple>()
24✔
1124
            && tuple.len() == 2
24✔
1125
        {
1126
            // The decoded value was a 2-item array. Check the types of the elements:
1127
            // (int, bytes) -> network
1128
            // (bytes, int) -> interface
1129
            let first_item = tuple.get_item(0)?;
24✔
1130
            let second_item = tuple.get_item(1)?;
24✔
1131
            if let Ok(prefix) = first_item.cast::<PyInt>()
24✔
1132
                && let Ok(address) = second_item.cast::<PyBytes>()
12✔
1133
            {
1134
                let mut address_vec: Vec<u8> = address.extract()?;
12✔
1135
                address_vec.resize(4, 0);
12✔
1136
                IPV4NETWORK_TYPE.get(py)?.call1(((address_vec, prefix),))?
12✔
1137
            } else if let Ok(address) = first_item.cast::<PyBytes>()
12✔
1138
                && let Ok(prefix) = second_item.cast::<PyInt>()
12✔
1139
            {
1140
                IPV4INTERFACE_TYPE.get(py)?.call1(((address, prefix),))?
12✔
1141
            } else {
UNCOV
1142
                return Err(CBORDecodeError::new_err("invalid types in input array"));
×
1143
            }
1144
        } else {
UNCOV
1145
            return Err(CBORDecodeError::new_err(
×
UNCOV
1146
                "input value must be a bytestring or an array of 2 elements",
×
1147
            ));
×
1148
        };
1149
        Ok(CompleteFrame(addr))
60✔
1150
    }
60✔
1151

1152
    fn decode_ipv6(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
60✔
1153
        // Semantic tag 54
1154
        let py = value.py();
60✔
1155
        let ipv6addr_class = IPV6ADDRESS_TYPE.get(py)?;
60✔
1156
        let addr = if let Ok(bytes) = value.cast::<PyBytes>() {
60✔
1157
            // The decoded value was a bytestring, so this is an IPv6 address
1158
            ipv6addr_class.call1((bytes,))?
24✔
1159
        } else if let Ok(tuple) = value.cast_into::<PyTuple>()
36✔
1160
            && (2..=3).contains(&tuple.len())
36✔
1161
        {
1162
            // The decoded value was a 2-item (or 3 with zone ID) array.
1163
            // Check the types of the elements:
1164
            // (int, bytes) -> network
1165
            // (bytes, int) -> interface
1166
            let first_item = tuple.get_item(0)?;
36✔
1167
            let second_item = tuple.get_item(1)?;
36✔
1168
            let zone_id = tuple.get_item(2).ok();
36✔
1169
            let (class, addr_bytes, prefix) = if let Ok(prefix) = first_item.cast::<PyInt>()
36✔
1170
                && let Ok(address) = second_item.cast::<PyBytes>()
12✔
1171
            {
1172
                let mut address_vec: Vec<u8> = address.extract()?;
12✔
1173
                address_vec.resize(16, 0);
12✔
1174
                Ok((
1175
                    IPV6NETWORK_TYPE.get(py)?,
12✔
1176
                    PyBytes::new(py, address_vec.as_slice()),
12✔
1177
                    prefix,
12✔
1178
                ))
1179
            } else if let Ok(address) = first_item.cast_into::<PyBytes>()
24✔
1180
                && let Ok(prefix) = second_item.cast::<PyInt>()
24✔
1181
            {
1182
                Ok((IPV6INTERFACE_TYPE.get(py)?, address, prefix))
24✔
1183
            } else {
UNCOV
1184
                Err(CBORDecodeError::new_err("invalid types in input array"))
×
UNCOV
1185
            }?;
×
1186
            let addr_obj = ipv6addr_class.call1((addr_bytes,))?;
36✔
1187

1188
            // Format the zone ID suffix if a zone ID was included
1189
            // (bytes or integer as the last item of a 3-tuple)
1190
            let zone_id_suffix = if let Some(zone_id) = zone_id {
36✔
1191
                if let Ok(zone_id_bytes) = zone_id.cast::<PyBytes>() {
24✔
1192
                    let zone_id_str = String::from_utf8(zone_id_bytes.as_bytes().to_vec())?;
12✔
1193
                    format!("%{zone_id_str}")
12✔
1194
                } else if let Ok(zone_id_int) = zone_id.cast::<PyInt>() {
12✔
1195
                    format!("%{zone_id_int}")
12✔
1196
                } else {
UNCOV
1197
                    return Err(CBORDecodeError::new_err(
×
UNCOV
1198
                        "zone ID must be an integer or a bytestring",
×
1199
                    ));
×
1200
                }
1201
            } else {
1202
                String::default()
12✔
1203
            };
1204

1205
            let formatted_addr = format!("{addr_obj}{zone_id_suffix}/{prefix}");
36✔
1206
            class.call1((formatted_addr,))?
36✔
1207
        } else {
UNCOV
1208
            return Err(CBORDecodeError::new_err(
×
UNCOV
1209
                "input value must be a bytestring or an array of 2 elements",
×
1210
            ));
×
1211
        };
1212
        Ok(CompleteFrame(addr))
60✔
1213
    }
60✔
1214

1215
    fn decode_epoch_date(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
12✔
1216
        // Semantic tag 100
1217
        let py = value.py();
12✔
1218
        let value = value.extract::<i32>()? + 719163;
12✔
1219
        DATE_FROMORDINAL.get(py)?.call1((value,)).map(CompleteFrame)
12✔
1220
    }
12✔
1221

1222
    fn decode_ipaddress(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
84✔
1223
        // Semantic tag 260 (deprecated)
1224
        let py = value.py();
84✔
1225
        let value = value.cast_into::<PyBytes>()?;
84✔
1226
        let addr_obj = match value.len()? {
72✔
1227
            4 | 16 => IPADDRESS_FUNC.get(py)?.call1((value,)),
48✔
1228
            6 => Ok(Bound::new(py, CBORTag::new_internal(260, value.into_any()))?.into_any()), // MAC address
12✔
1229
            length => Err(CBORDecodeError::new_err(format!(
12✔
1230
                "invalid IP address length ({length})"
12✔
1231
            ))),
12✔
1232
        }?;
12✔
1233
        Ok(CompleteFrame(addr_obj))
60✔
1234
    }
84✔
1235

1236
    fn decode_ipnetwork<'py>(
84✔
1237
        value: Bound<'py, PyAny>,
84✔
1238
        _immutable: bool,
84✔
1239
    ) -> PyResult<DecoderResult<'py>> {
84✔
1240
        // Semantic tag 261 (deprecated)
1241
        let py = value.py();
84✔
1242
        let value: Bound<'py, PyMapping> = value.cast_into()?;
84✔
1243
        let length = value.len()?;
84✔
1244
        if length != 1 {
84✔
1245
            return Err(CBORDecodeError::new_err(format!(
12✔
1246
                "invalid input map length for IP network: {}",
12✔
1247
                length
12✔
1248
            )));
12✔
1249
        }
72✔
1250
        let first_item = value.items()?.get_item(0)?;
72✔
1251
        let mask_length = first_item.get_item(1)?;
72✔
1252
        if !mask_length.is_exact_instance_of::<PyInt>() {
72✔
1253
            return Err(CBORDecodeError::new_err(format!(
12✔
1254
                "invalid mask length for IP network: {mask_length}"
12✔
1255
            )));
12✔
1256
        }
60✔
1257

1258
        let addr_obj = match IPNETWORK_FUNC.get(py)?.call1((&first_item,)) {
60✔
1259
            Ok(ip_network) => Ok(ip_network),
48✔
1260
            Err(e) => {
12✔
1261
                // A CompleteFrameError may indicate that the bytestring has host bits set, so try parsing
1262
                // it as an IP interface instead
1263
                if e.is_instance_of::<PyValueError>(py) {
12✔
1264
                    IPINTERFACE_FUNC.get(py)?.call1((first_item,))
12✔
1265
                } else {
UNCOV
1266
                    Err(e)
×
1267
                }
1268
            }
UNCOV
1269
        }?;
×
1270
        Ok(CompleteFrame(addr_obj))
60✔
1271
    }
84✔
1272

1273
    fn decode_date_string(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
12✔
1274
        // Semantic tag 1004
1275
        let py = value.py();
12✔
1276
        let date = DATE_FROMISOFORMAT.get(py)?.call1((value,))?;
12✔
1277
        Ok(CompleteFrame(date))
12✔
1278
    }
12✔
1279

1280
    fn decode_complex(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
252✔
1281
        // Semantic tag 43000
1282
        let py = value.py();
252✔
1283
        let tuple = require_tuple(value, 2)?;
252✔
1284
        let real: f64 = tuple.get_item(0)?.extract()?;
252✔
1285
        let imag: f64 = tuple.get_item(1)?.extract()?;
252✔
1286
        Ok(CompleteFrame(
252✔
1287
            PyComplex::from_doubles(py, real, imag).into_any(),
252✔
1288
        ))
252✔
1289
    }
252✔
1290

1291
    fn decode_self_describe_cbor(value: Bound<PyAny>, _immutable: bool) -> PyResult<DecoderResult> {
24✔
1292
        // Semantic tag 55799
1293
        Ok(CompleteFrame(value))
24✔
1294
    }
24✔
1295

1296
    fn decode_set<'py>(
200✔
1297
        &mut self,
200✔
1298
        py: Python<'py>,
200✔
1299
        immutable: bool,
200✔
1300
    ) -> PyResult<DecoderResult<'py>> {
200✔
1301
        // Semantic tag 258
1302
        let mut set_or_none = if immutable {
200✔
1303
            None
36✔
1304
        } else {
1305
            Some(PySet::empty(py)?.into_any())
164✔
1306
        };
1307
        let container = set_or_none.clone();
200✔
1308
        let callback = move |item: Bound<'py, PyAny>, _immutable: bool| {
200✔
1309
            let container: Bound<'py, PyAny> = if let Some(set) = set_or_none.take() {
188✔
1310
                set.call_method1(intern!(py, "update"), (item,))?;
152✔
1311
                set.into_any()
152✔
1312
            } else {
1313
                let tuple = item.cast_into::<PyTuple>()?;
36✔
1314
                PyFrozenSet::new(py, tuple)?.into_any()
36✔
1315
            };
1316
            Ok(CompleteFrame(container))
188✔
1317
        };
188✔
1318
        Ok(BeginFrame(
200✔
1319
            Box::new(callback),
200✔
1320
            true,
200✔
1321
            container,
200✔
1322
            DisplayName::String("set"),
200✔
1323
        ))
200✔
1324
    }
200✔
1325
}
1326

UNCOV
1327
#[pymethods]
×
1328
impl CBORDecoder {
1329
    #[new]
1330
    #[pyo3(signature = (
1331
        fp,
1332
        *,
1333
        tag_hook = None,
1334
        object_hook = None,
1335
        semantic_decoders = None,
1336
        str_errors = "strict",
1337
        read_size = 4096,
1338
        max_depth = 400,
1339
        allow_indefinite = true,
1340
        allow_duplicate_keys = true,
1341
    ))]
1342
    pub fn new(
408✔
1343
        py: Python<'_>,
408✔
1344
        fp: &Bound<'_, PyAny>,
408✔
1345
        tag_hook: Option<&Bound<'_, PyAny>>,
408✔
1346
        object_hook: Option<&Bound<'_, PyAny>>,
408✔
1347
        semantic_decoders: Option<&Bound<'_, PyMapping>>,
408✔
1348
        str_errors: &str,
408✔
1349
        read_size: usize,
408✔
1350
        max_depth: usize,
408✔
1351
        allow_indefinite: bool,
408✔
1352
        allow_duplicate_keys: bool,
408✔
1353
    ) -> PyResult<Self> {
408✔
1354
        Self::new_internal(
408✔
1355
            py,
408✔
1356
            Some(fp),
408✔
1357
            None,
408✔
1358
            tag_hook,
408✔
1359
            object_hook,
408✔
1360
            semantic_decoders,
408✔
1361
            str_errors,
408✔
1362
            read_size,
408✔
1363
            max_depth,
408✔
1364
            allow_indefinite,
408✔
1365
            allow_duplicate_keys,
408✔
1366
        )
1367
    }
408✔
1368

1369
    #[getter]
UNCOV
1370
    fn fp(&self, py: Python<'_>) -> Option<Py<PyAny>> {
×
UNCOV
1371
        self.fp.as_ref().map(|fp| fp.clone_ref(py))
×
1372
    }
×
1373

1374
    #[setter]
1375
    fn set_fp(&mut self, fp: &Bound<'_, PyAny>) -> PyResult<()> {
420✔
1376
        let result = fp.call_method0("readable");
420✔
1377
        if let Ok(readable) = &result
420✔
1378
            && readable.is_truthy()?
408✔
1379
        {
1380
            self.fp_is_seekable = fp.call_method0("seekable")?.is_truthy()?;
396✔
1381
            let fp = fp.clone();
396✔
1382
            self.read_method = Some(fp.getattr("read")?.unbind());
396✔
1383
            self.fp = Some(fp.unbind());
396✔
1384
            self.available_bytes = 0;
396✔
1385
            self.read_position = 0;
396✔
1386
            self.buffer = None;
396✔
1387
            Ok(())
396✔
1388
        } else {
1389
            raise_exc_from(
24✔
1390
                fp.py(),
24✔
1391
                PyValueError::new_err("fp must be a readable file-like object"),
24✔
1392
                result.err(),
24✔
1393
            )
1394
        }
1395
    }
420✔
1396

1397
    #[getter]
1398
    fn tag_hook(&self, py: Python<'_>) -> Option<Py<PyAny>> {
12✔
1399
        self.tag_hook
12✔
1400
            .as_ref()
12✔
1401
            .map(|tag_hook| tag_hook.clone_ref(py))
12✔
1402
    }
12✔
1403

1404
    #[setter]
1405
    fn set_tag_hook(&mut self, tag_hook: Option<&Bound<'_, PyAny>>) -> PyResult<()> {
4,512✔
1406
        if let Some(tag_hook) = tag_hook {
4,512✔
1407
            if !tag_hook.is_callable() {
132✔
1408
                return Err(PyErr::new::<PyTypeError, _>(
12✔
1409
                    "tag_hook must be callable or None",
12✔
1410
                ));
12✔
1411
            }
120✔
1412

1413
            self.tag_hook = Some(tag_hook.clone().unbind());
120✔
1414
        } else {
4,380✔
1415
            self.tag_hook = None;
4,380✔
1416
        }
4,380✔
1417
        Ok(())
4,500✔
1418
    }
4,512✔
1419

1420
    #[getter]
1421
    fn object_hook(&self, py: Python<'_>) -> Option<Py<PyAny>> {
12✔
1422
        self.object_hook
12✔
1423
            .as_ref()
12✔
1424
            .map(|object_hook| object_hook.clone_ref(py))
12✔
1425
    }
12✔
1426

1427
    #[setter]
1428
    fn set_object_hook(&mut self, object_hook: Option<&Bound<'_, PyAny>>) -> PyResult<()> {
4,500✔
1429
        if let Some(object_hook) = object_hook {
4,500✔
1430
            if !object_hook.is_callable() {
48✔
1431
                return Err(PyErr::new::<PyTypeError, _>(
12✔
1432
                    "object_hook must be callable or None",
12✔
1433
                ));
12✔
1434
            }
36✔
1435

1436
            self.object_hook = Some(object_hook.clone().unbind());
36✔
1437
        } else {
4,452✔
1438
            self.object_hook = None;
4,452✔
1439
        }
4,452✔
1440
        Ok(())
4,488✔
1441
    }
4,500✔
1442

1443
    #[getter]
1444
    fn str_errors(&self, py: Python<'_>) -> Py<PyString> {
60✔
1445
        if let Some(str_errors) = self.str_errors.as_ref() {
60✔
1446
            str_errors.clone_ref(py)
48✔
1447
        } else {
1448
            intern!(py, "strict").clone().unbind()
12✔
1449
        }
1450
    }
60✔
1451

1452
    #[setter]
1453
    fn set_str_errors(&mut self, str_errors: &Bound<'_, PyString>) -> PyResult<()> {
4,488✔
1454
        let as_string: &str = str_errors.extract()?;
4,488✔
1455
        self.str_errors = match as_string {
4,488✔
1456
            "strict" => None,
4,488✔
1457
            "ignore" | "replace" | "backslashreplace" | "surrogateescape" => {
108✔
1458
                Some(str_errors.clone().unbind())
96✔
1459
            }
1460
            _ => {
1461
                return Err(PyValueError::new_err(format!(
12✔
1462
                    "invalid str_errors value: '{str_errors}'"
12✔
1463
                )));
12✔
1464
            }
1465
        };
1466
        Ok(())
4,476✔
1467
    }
4,488✔
1468

1469
    /// Read bytes from the data stream.
1470
    ///
1471
    /// :param amount: the number of bytes to read
1472
    #[pyo3(signature = (amount, /))]
1473
    fn read(&mut self, py: Python<'_>, amount: usize) -> PyResult<Vec<u8>> {
3,504✔
1474
        if amount == 0 {
3,504✔
1475
            return Ok(Vec::default());
160✔
1476
        }
3,344✔
1477

1478
        if self.available_bytes == 0 {
3,344✔
1479
            // No buffer
1480
            let (new_bytes, amount_read) = self.read_from_fp(py, amount)?;
72✔
1481
            self.read_position = amount;
12✔
1482
            self.available_bytes = amount_read - amount;
12✔
1483
            let new_buffer = new_bytes.as_bytes()[..amount].to_vec();
12✔
1484
            self.buffer = Some(new_bytes.unbind());
12✔
1485
            Ok(new_buffer)
12✔
1486
        } else if self.available_bytes < amount {
3,272✔
1487
            // Combine the remnants of the partial buffer with new data read from the file
1488
            let needed_bytes = amount - self.available_bytes;
72✔
1489
            let mut concatenated_buffer: Vec<u8> =
72✔
1490
                self.buffer.take().unwrap().as_bytes(py).to_vec();
72✔
1491
            let (new_bytes, amount_read) = self.read_from_fp(py, needed_bytes)?;
72✔
UNCOV
1492
            concatenated_buffer.extend_from_slice(&new_bytes[..needed_bytes]);
×
UNCOV
1493
            self.buffer = Some(new_bytes.unbind());
×
1494
            self.available_bytes = amount_read - needed_bytes;
×
1495
            self.read_position = needed_bytes;
×
1496
            Ok(concatenated_buffer)
×
1497
        } else {
1498
            // Return a slice from the existing bytes object
1499
            let vec = self.buffer.as_ref().unwrap().as_bytes(py)
3,200✔
1500
                [self.read_position..self.read_position + amount]
3,200✔
1501
                .to_vec();
3,200✔
1502
            self.available_bytes -= amount;
3,200✔
1503
            self.read_position += amount;
3,200✔
1504
            Ok(vec)
3,200✔
1505
        }
1506
    }
3,504✔
1507

1508
    /// Decode the next value from the stream.
1509
    ///
1510
    /// :param immutable: if :data:`True`, decode the next item as an immutable type
1511
    ///     (e.g. :class:`tuple` instead of a :class:`list`), if possible
1512
    /// :return: the decoded object
1513
    /// :raises CBORDecodeError: if there is any problem decoding the stream
1514
    #[pyo3(signature = (*, immutable = false))]
1515
    pub fn decode<'py>(&mut self, py: Python<'py>, immutable: bool) -> PyResult<Bound<'py, PyAny>> {
4,356✔
1516
        let mut frames: Vec<StackFrame> = Vec::new();
4,356✔
1517

1518
        fn add_frame<'a>(
11,560✔
1519
            frames: &mut Vec<StackFrame<'a>>,
11,560✔
1520
            max_depth: usize,
11,560✔
1521
            frame: StackFrame<'a>,
11,560✔
1522
        ) -> PyResult<()> {
11,560✔
1523
            if frames.len() == max_depth {
11,560✔
1524
                return Err(CBORDecodeError::new_err(format!(
24✔
1525
                    "maximum container nesting depth ({max_depth}) exceeded",
24✔
1526
                )));
24✔
1527
            }
11,536✔
1528

1529
            frames.push(frame);
11,536✔
1530
            Ok(())
11,536✔
1531
        }
11,560✔
1532

1533
        fn wrap_exception(py: Python<'_>, err: PyErr, typename: &DisplayName) -> PyErr {
636✔
1534
            if err.is_instance_of::<CBORDecodeEOF>(py) {
636✔
1535
                err
120✔
1536
            } else if err.is_instance_of::<CBORDecodeError>(py) {
516✔
1537
                CBORDecodeError::new_err(format!(
276✔
1538
                    "error decoding {}: {}",
1539
                    typename,
1540
                    err.arguments(py)
276✔
1541
                ))
1542
            } else {
1543
                create_exc_from(
240✔
1544
                    py,
240✔
1545
                    CBORDecodeError::new_err(format!("error decoding {}", typename)),
240✔
1546
                    Some(err),
240✔
1547
                )
1548
            }
1549
        }
636✔
1550

1551
        let mut shareables: Vec<Option<Bound<'py, PyAny>>> = Vec::new();
4,356✔
1552
        let mut string_namespaces: Vec<Vec<Bound<'py, PyAny>>> = Vec::new();
4,356✔
1553
        let mut value: Option<Bound<'py, PyAny>> = None;
4,356✔
1554
        let mut current_immutable: bool = immutable;
4,356✔
1555
        loop {
1556
            let result: PyResult<DecoderResult<'py>> = if let Some(previous_value) = value.take() {
34,128✔
1557
                // Call the decoder callback of the last frame
1558
                let frame = frames.last_mut().unwrap();
12,180✔
1559
                if let Some(decoder_callback) = frame.decoder_callback.as_mut() {
12,180✔
1560
                    decoder_callback(previous_value, frame.immutable)
12,096✔
1561
                        .map_err(|e| wrap_exception(py, e, &frame.typename))
12,096✔
1562
                } else if frame.contains_string_namespace {
84✔
1563
                    string_namespaces
12✔
1564
                        .pop()
12✔
1565
                        .expect("no string namespaces to pop from");
12✔
1566
                    Ok(CompleteFrame(previous_value))
12✔
1567
                } else if let Some(shareable_index) = frame.shareable_index {
72✔
1568
                    shareables[shareable_index].get_or_insert_with(|| previous_value.clone());
72✔
1569
                    Ok(CompleteFrame(previous_value))
72✔
1570
                } else {
UNCOV
1571
                    panic!("no decoder callback, shareable index or string namespace");
×
1572
                }
1573
            } else {
1574
                let (major_type, subtype) = self.read_major_and_subtype(py)?;
21,948✔
1575
                match major_type {
21,924✔
1576
                    0 => self.decode_uint(py, subtype),
3,684✔
1577
                    1 => self.decode_negint(py, subtype),
464✔
1578
                    2 => self.decode_bytestring(py, subtype),
1,128✔
1579
                    3 => self.decode_string(py, subtype),
2,280✔
1580
                    4 => self.decode_array(py, subtype, current_immutable),
7,036✔
1581
                    5 => self.decode_map(py, subtype, current_immutable),
1,792✔
1582
                    6 => self.decode_semantic(py, subtype, current_immutable),
3,116✔
1583
                    7 => self.decode_special(py, subtype),
2,424✔
UNCOV
1584
                    _ => Err(CBORDecodeError::new_err(format!(
×
UNCOV
1585
                        "invalid major type: {major_type}"
×
1586
                    ))),
×
1587
                }
1588
                .map_err(|e| {
21,924✔
1589
                    let typename = match major_type {
360✔
1590
                        0 => "unsigned integer",
12✔
UNCOV
1591
                        1 => "negative integer",
×
1592
                        2 => "byte string",
108✔
1593
                        3 => "text string",
168✔
UNCOV
1594
                        4 => "array",
×
UNCOV
1595
                        5 => "map",
×
1596
                        6 => "semantic tag",
×
1597
                        7 => "special value",
72✔
1598
                        _ => unreachable!("invalid major types should have been handled earlier"),
×
1599
                    };
1600
                    wrap_exception(py, e, &DisplayName::String(typename))
360✔
1601
                })
360✔
1602
            };
1603

1604
            match result {
33,468✔
1605
                Ok(BeginFrame(callback, requested_immutable, container, typename)) => {
11,284✔
1606
                    if let Some(frame) = frames.last_mut()
11,284✔
1607
                        && let Some(container) = container
8,784✔
1608
                        && let Some(shareable_index) = frame.shareable_index
5,896✔
1609
                    {
156✔
1610
                        frames.pop();
156✔
1611
                        shareables[shareable_index] = Some(container.clone());
156✔
1612
                    }
11,128✔
1613
                    current_immutable = current_immutable || requested_immutable;
11,284✔
1614
                    add_frame(
11,284✔
1615
                        &mut frames,
11,284✔
1616
                        self.max_depth,
11,284✔
1617
                        StackFrame {
11,284✔
1618
                            immutable: current_immutable,
11,284✔
1619
                            decoder_callback: Some(callback),
11,284✔
1620
                            shareable_index: None,
11,284✔
1621
                            typename,
11,284✔
1622
                            contains_string_namespace: false,
11,284✔
1623
                        },
11,284✔
1624
                    )?;
24✔
1625
                }
1626
                Ok(ContinueFrame(require_immutable)) => {
6,056✔
1627
                    // If require_immutable is true, the next value must be immutable
1628
                    // Otherwise, restore the immutable flag to the previous value
1629
                    current_immutable = if frames.len() >= 2 {
6,056✔
1630
                        frames.get(frames.len() - 2).unwrap().immutable
3,036✔
1631
                    } else {
1632
                        immutable
3,020✔
1633
                    } || require_immutable;
4,376✔
1634
                    frames.last_mut().unwrap().immutable = current_immutable;
6,056✔
1635
                }
1636
                Ok(CompleteFrame(new_value)) => {
5,572✔
1637
                    frames
5,572✔
1638
                        .pop()
5,572✔
1639
                        .expect("received frame completion but there are no frames on the stack");
5,572✔
1640
                    current_immutable = frames.last().map_or(immutable, |frame| frame.immutable);
5,572✔
1641
                    value = Some(new_value);
5,572✔
1642
                }
1643
                Ok(Value(new_value)) => {
6,908✔
1644
                    value = Some(new_value);
6,908✔
1645
                }
6,908✔
1646
                Ok(StringNamespace) => {
1647
                    add_frame(
36✔
1648
                        &mut frames,
36✔
1649
                        self.max_depth,
36✔
1650
                        StackFrame {
36✔
1651
                            immutable: current_immutable,
36✔
1652
                            decoder_callback: None,
36✔
1653
                            shareable_index: None,
36✔
1654
                            typename: DisplayName::String("string namespace"),
36✔
1655
                            contains_string_namespace: true,
36✔
1656
                        },
36✔
UNCOV
1657
                    )?;
×
1658
                    string_namespaces.push(Vec::new());
36✔
1659
                }
1660
                Ok(StringValue(string, length)) => {
3,096✔
1661
                    // Conditionally add the string to the innermost string namespace
1662
                    if let Some(namespace) = string_namespaces.last_mut()
3,096✔
1663
                        && match namespace.len() {
48✔
1664
                            0..24 => length >= 3,
48✔
UNCOV
1665
                            24..256 => length >= 4,
×
UNCOV
1666
                            256..65536 => length >= 5,
×
1667
                            65536..4294967296 => length >= 6,
×
1668
                            _ => length >= 11,
×
1669
                        }
1670
                    {
48✔
1671
                        namespace.push(string.clone());
48✔
1672
                    }
3,048✔
1673
                    value = Some(string);
3,096✔
1674
                }
1675
                Ok(StringReference(index)) => {
96✔
1676
                    frames
96✔
1677
                        .pop()
96✔
1678
                        .expect("  received string reference but there are no frames on the stack");
96✔
1679
                    if let Some(namespace) = string_namespaces.last() {
96✔
1680
                        if let Some(string) = namespace.get(index) {
84✔
1681
                            value = Some(string.clone());
72✔
1682
                        } else {
72✔
1683
                            return Err(CBORDecodeError::new_err(format!(
12✔
1684
                                "string reference {index} not found"
12✔
1685
                            )));
12✔
1686
                        }
1687
                    } else {
1688
                        return Err(CBORDecodeError::new_err(
12✔
1689
                            "string reference outside of namespace",
12✔
1690
                        ));
12✔
1691
                    }
1692
                    current_immutable = frames
72✔
1693
                        .last()
72✔
1694
                        .map_or(current_immutable, |frame| frame.immutable);
72✔
1695
                }
1696
                Ok(Shareable) => {
1697
                    add_frame(
240✔
1698
                        &mut frames,
240✔
1699
                        self.max_depth,
240✔
1700
                        StackFrame {
240✔
1701
                            immutable: current_immutable,
240✔
1702
                            decoder_callback: None,
240✔
1703
                            shareable_index: Some(shareables.len()),
240✔
1704
                            typename: DisplayName::String("shareable value"),
240✔
1705
                            contains_string_namespace: false,
240✔
1706
                        },
240✔
UNCOV
1707
                    )?;
×
1708
                    shareables.push(None);
240✔
1709
                }
1710
                Ok(SharedReference(index)) => {
180✔
1711
                    frames
180✔
1712
                        .pop()
180✔
1713
                        .expect("received shared reference but there are no frames on the stack");
180✔
1714
                    value = match shareables.get(index) {
180✔
1715
                        Some(Some(value)) => Some(value.clone()),
144✔
1716
                        Some(None) => {
1717
                            return Err(CBORDecodeError::new_err(format!(
12✔
1718
                                "shared value {index} has not been initialized"
12✔
1719
                            )));
12✔
1720
                        }
1721
                        None => {
1722
                            return Err(CBORDecodeError::new_err(format!(
24✔
1723
                                "shared reference {index} not found"
24✔
1724
                            )));
24✔
1725
                        }
1726
                    };
1727
                    current_immutable = frames
144✔
1728
                        .last()
144✔
1729
                        .map_or(current_immutable, |frame| frame.immutable);
144✔
1730
                }
1731
                Err(err) => {
636✔
1732
                    // If an Exception was raised, wrap it in a CBORDecodeError
1733
                    // If a ValueError was raised, wrap it in a CBORDecodeError
1734
                    return if err.is_instance_of::<CBORDecodeError>(py) {
636✔
1735
                        Err(err)
636✔
UNCOV
1736
                    } else if err.is_instance_of::<PyValueError>(py)
×
UNCOV
1737
                        || err.is_instance_of::<PyException>(py)
×
1738
                    {
1739
                        Err(create_exc_from(
×
1740
                            py,
×
1741
                            CBORDecodeError::new_err(err.to_string()),
×
1742
                            Some(err),
×
1743
                        ))
×
1744
                    } else {
1745
                        Err(err)
×
1746
                    };
1747
                }
1748
            }
1749

1750
            if frames.is_empty() {
33,384✔
1751
                // If fp was seekable and excess data has been read, empty the buffer and
1752
                // rewind the file
1753
                if self.available_bytes > 0
3,612✔
1754
                    && let Some(fp) = &self.fp
24✔
1755
                {
1756
                    let offset = -(self.available_bytes as isize);
24✔
1757
                    fp.call_method1(py, intern!(py, "seek"), (offset, SEEK_CUR))?;
24✔
1758
                    self.buffer = None;
24✔
1759
                    self.available_bytes = 0;
24✔
1760
                    self.read_position = 0;
24✔
1761
                }
3,588✔
1762
                return Ok(value.expect("stack is empty but final return value is missing"));
3,612✔
1763
            }
29,772✔
1764
        }
1765
    }
4,356✔
1766
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc