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

Function get_fd_from_file_opt

crates/stdlib/src/faulthandler.rs:799–831  ·  view source on GitHub ↗
(file: OptionalArg<PyObjectRef>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

797 }
798
799 fn get_fd_from_file_opt(file: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<i32> {
800 match file {
801 OptionalArg::Present(f) if !vm.is_none(&f) => {
802 // Check if it's an integer (file descriptor)
803 if let Ok(fd) = f.try_to_value::<i32>(vm) {
804 if fd < 0 {
805 return Err(vm.new_value_error("file is not a valid file descriptor"));
806 }
807 return Ok(fd);
808 }
809 // Try to get fileno() from file object
810 let fileno = vm.call_method(&f, "fileno", ())?;
811 let fd: i32 = fileno.try_to_value(vm)?;
812 if fd < 0 {
813 return Err(vm.new_value_error("file is not a valid file descriptor"));
814 }
815 // Try to flush the file
816 let _ = vm.call_method(&f, "flush", ());
817 Ok(fd)
818 }
819 _ => {
820 // file=None or file not passed: fall back to sys.stderr
821 let stderr = vm.sys_module.get_attr("stderr", vm)?;
822 if vm.is_none(&stderr) {
823 return Err(vm.new_runtime_error("sys.stderr is None"));
824 }
825 let fileno = vm.call_method(&stderr, "fileno", ())?;
826 let fd: i32 = fileno.try_to_value(vm)?;
827 let _ = vm.call_method(&stderr, "flush", ());
828 Ok(fd)
829 }
830 }
831 }
832
833 fn watchdog_thread(state: WatchdogHandle) {
834 let (lock, cvar) = &*state;

Callers 4

dump_tracebackFunction · 0.85
enableFunction · 0.85
dump_traceback_laterFunction · 0.85
registerFunction · 0.85

Calls 5

try_to_valueMethod · 0.80
ErrClass · 0.50
is_noneMethod · 0.45
call_methodMethod · 0.45
get_attrMethod · 0.45

Tested by

no test coverage detected