(file: OptionalArg<PyObjectRef>, vm: &VirtualMachine)
| 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; |
no test coverage detected