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

Method try_path_or_fd

crates/vm/src/ospath.rs:77–103  ·  view source on GitHub ↗

Convert to OsPathOrFd (path or file descriptor)

(
        &self,
        obj: PyObjectRef,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

75
76 /// Convert to OsPathOrFd (path or file descriptor)
77 pub(crate) fn try_path_or_fd<'fd>(
78 &self,
79 obj: PyObjectRef,
80 vm: &VirtualMachine,
81 ) -> PyResult<OsPathOrFd<'fd>> {
82 // Handle fd (before __fspath__ check, like CPython)
83 if let Some(int) = obj.try_index_opt(vm) {
84 // Warn if bool is used as a file descriptor
85 if obj
86 .class()
87 .is(crate::builtins::bool_::PyBool::static_type())
88 {
89 crate::stdlib::_warnings::warn(
90 vm.ctx.exceptions.runtime_warning,
91 "bool is used as a file descriptor".to_owned(),
92 1,
93 vm,
94 )?;
95 }
96 let fd = int?.try_to_primitive(vm)?;
97 return unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }
98 .map(OsPathOrFd::Fd)
99 .map_err(|e| e.into_pyexception(vm));
100 }
101
102 self.try_path_inner(obj, true, vm).map(OsPathOrFd::Path)
103 }
104
105 /// Convert to OsPath only (no fd support)
106 fn try_path_inner(

Callers 1

try_from_objectMethod · 0.80

Calls 9

try_index_optMethod · 0.80
isMethod · 0.80
try_to_primitiveMethod · 0.80
try_path_innerMethod · 0.80
warnFunction · 0.70
classMethod · 0.45
to_ownedMethod · 0.45
mapMethod · 0.45
into_pyexceptionMethod · 0.45

Tested by

no test coverage detected