MCPcopy Index your code
hub / github.com/RustPython/RustPython / retry_op_with_frozenset

Method retry_op_with_frozenset

crates/vm/src/builtins/set.rs:468–500  ·  view source on GitHub ↗

Run operation, on failure, if item is a set/set subclass, convert it into a frozenset and try the operation again. Propagates original error on failure to convert and restores item in KeyError on failure (remove).

(
        &self,
        item: &PyObject,
        vm: &VirtualMachine,
        op: F,
    )

Source from the content-addressed store, hash-verified

466 // into a frozenset and try the operation again. Propagates original error
467 // on failure to convert and restores item in KeyError on failure (remove).
468 fn retry_op_with_frozenset<T, F>(
469 &self,
470 item: &PyObject,
471 vm: &VirtualMachine,
472 op: F,
473 ) -> PyResult<T>
474 where
475 F: Fn(&PyObject, &VirtualMachine) -> PyResult<T>,
476 {
477 op(item, vm).or_else(|original_err| {
478 item.downcast_ref::<PySet>()
479 // Keep original error around.
480 .ok_or(original_err)
481 .and_then(|set| {
482 op(
483 &PyFrozenSet {
484 inner: set.inner.copy(),
485 ..Default::default()
486 }
487 .into_pyobject(vm),
488 vm,
489 )
490 // If operation raised KeyError, report original set (set.remove)
491 .map_err(|op_err| {
492 if op_err.fast_isinstance(vm.ctx.exceptions.key_error) {
493 vm.new_key_error(item.to_owned())
494 } else {
495 op_err
496 }
497 })
498 })
499 })
500 }
501}
502
503fn extract_set(obj: &PyObject) -> Option<&PySetInner> {

Callers 3

containsMethod · 0.80
removeMethod · 0.80
discardMethod · 0.80

Calls 5

fast_isinstanceMethod · 0.80
new_key_errorMethod · 0.80
into_pyobjectMethod · 0.45
copyMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected