Convert to OsPathOrFd (path or file descriptor)
(
&self,
obj: PyObjectRef,
vm: &VirtualMachine,
)
| 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( |
no test coverage detected