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

Method try_from_borrowed_object

crates/vm/src/sliceable.rs:261–285  ·  view source on GitHub ↗
(
        vm: &VirtualMachine,
        obj: &PyObject,
        type_name: &str,
    )

Source from the content-addressed store, hash-verified

259
260impl SequenceIndex {
261 pub fn try_from_borrowed_object(
262 vm: &VirtualMachine,
263 obj: &PyObject,
264 type_name: &str,
265 ) -> PyResult<Self> {
266 if let Some(i) = obj.downcast_ref::<PyInt>() {
267 // TODO: number protocol
268 i.try_to_primitive(vm)
269 .map_err(|_| vm.new_index_error("cannot fit 'int' into an index-sized integer"))
270 .map(Self::Int)
271 } else if let Some(slice) = obj.downcast_ref::<PySlice>() {
272 slice.to_saturated(vm).map(Self::Slice)
273 } else if let Some(i) = obj.try_index_opt(vm) {
274 // TODO: __index__ for indices is no more supported?
275 i?.try_to_primitive(vm)
276 .map_err(|_| vm.new_index_error("cannot fit 'int' into an index-sized integer"))
277 .map(Self::Int)
278 } else {
279 Err(vm.new_type_error(format!(
280 "{} indices must be integers or slices or classes that override __index__ operator, not '{}'",
281 type_name,
282 obj.class()
283 )))
284 }
285 }
286}
287
288pub trait SequenceIndexOp {

Callers

nothing calls this directly

Calls 5

try_to_primitiveMethod · 0.80
to_saturatedMethod · 0.80
try_index_optMethod · 0.80
ErrClass · 0.50
mapMethod · 0.45

Tested by

no test coverage detected