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

Method try_from_object

crates/vm/src/stdlib/_io.rs:70–91  ·  view source on GitHub ↗
(vm: &VirtualMachine, obj: PyObjectRef)

Source from the content-addressed store, hash-verified

68
69impl TryFromObject for Fildes {
70 fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
71 use crate::builtins::int;
72 let int = match obj.downcast::<int::PyInt>() {
73 Ok(i) => i,
74 Err(obj) => {
75 let fileno_meth = vm.get_attribute_opt(obj, "fileno")?.ok_or_else(|| {
76 vm.new_type_error("argument must be an int, or have a fileno() method.")
77 })?;
78 fileno_meth
79 .call((), vm)?
80 .downcast()
81 .map_err(|_| vm.new_type_error("fileno() returned a non-integer"))?
82 }
83 };
84 let fd = int.try_to_primitive(vm)?;
85 if fd < 0 {
86 return Err(vm.new_value_error(format!(
87 "file descriptor cannot be a negative integer ({fd})"
88 )));
89 }
90 Ok(Self(fd))
91 }
92}
93
94#[cfg(unix)]

Callers

nothing calls this directly

Calls 13

SelfFunction · 0.85
ok_or_elseMethod · 0.80
get_attribute_optMethod · 0.80
downcastMethod · 0.80
try_to_primitiveMethod · 0.80
ErrClass · 0.50
callMethod · 0.45
is_noneMethod · 0.45
as_wtf8Method · 0.45
is_utf8Method · 0.45
to_ownedMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected