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

Method try_from_object

crates/vm/src/object/ext.rs:162–183  ·  view source on GitHub ↗
(vm: &VirtualMachine, obj: PyObjectRef)

Source from the content-addressed store, hash-verified

160
161impl<T: PyPayload> TryFromObject for PyRefExact<T> {
162 fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
163 let target_cls = T::class(&vm.ctx);
164 let cls = obj.class();
165 if cls.is(target_cls) {
166 let obj = obj
167 .downcast()
168 .map_err(|obj| vm.new_downcast_runtime_error(target_cls, &obj))?;
169 Ok(Self { inner: obj })
170 } else if cls.fast_issubclass(target_cls) {
171 Err(vm.new_type_error(format!(
172 "Expected an exact instance of '{}', not a subclass '{}'",
173 target_cls.name(),
174 cls.name(),
175 )))
176 } else {
177 Err(vm.new_type_error(format!(
178 "Expected type '{}', not '{}'",
179 target_cls.name(),
180 cls.name(),
181 )))
182 }
183 }
184}
185
186impl<T: PyPayload> Deref for PyRefExact<T> {

Callers

nothing calls this directly

Calls 6

isMethod · 0.80
downcastMethod · 0.80
fast_issubclassMethod · 0.80
ErrClass · 0.50
classMethod · 0.45

Tested by

no test coverage detected