MCPcopy Create free account
hub / github.com/RustPython/RustPython / _adapt

Function _adapt

crates/stdlib/src/_sqlite3.rs:730–772  ·  view source on GitHub ↗
(obj: &PyObject, proto: PyTypeRef, alt: F, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

728 }
729
730 fn _adapt<F>(obj: &PyObject, proto: PyTypeRef, alt: F, vm: &VirtualMachine) -> PyResult
731 where
732 F: FnOnce(&PyObject) -> PyResult,
733 {
734 let proto = proto.into_object();
735 let key = vm
736 .ctx
737 .new_tuple(vec![obj.class().to_owned().into(), proto.clone()]);
738
739 if let Some(adapter) = adapters().get_item_opt(key.as_object(), vm)? {
740 return adapter.call((obj,), vm);
741 }
742 if let Ok(adapter) = proto.get_attr("__adapt__", vm) {
743 match adapter.call((obj,), vm) {
744 Ok(val) => {
745 if !vm.is_none(&val) {
746 return Ok(val);
747 }
748 }
749 Err(exc) => {
750 if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
751 return Err(exc);
752 }
753 }
754 }
755 }
756 if let Ok(adapter) = obj.get_attr("__conform__", vm) {
757 match adapter.call((proto,), vm) {
758 Ok(val) => {
759 if !vm.is_none(&val) {
760 return Ok(val);
761 }
762 }
763 Err(exc) => {
764 if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
765 return Err(exc);
766 }
767 }
768 }
769 }
770
771 alt(obj)
772 }
773
774 #[pyfunction]
775 fn adapt(

Callers 2

adaptFunction · 0.85
bind_parameterMethod · 0.85

Calls 10

adaptersFunction · 0.85
get_item_optMethod · 0.80
fast_isinstanceMethod · 0.80
ErrClass · 0.50
into_objectMethod · 0.45
new_tupleMethod · 0.45
as_objectMethod · 0.45
callMethod · 0.45
get_attrMethod · 0.45
is_noneMethod · 0.45

Tested by

no test coverage detected