| 2106 | |
| 2107 | #[pyfunction] |
| 2108 | fn device_encoding(fd: i32, _vm: &VirtualMachine) -> PyResult<Option<String>> { |
| 2109 | if !isatty(fd) { |
| 2110 | return Ok(None); |
| 2111 | } |
| 2112 | |
| 2113 | cfg_if::cfg_if! { |
| 2114 | if #[cfg(any(target_os = "android", target_os = "redox"))] { |
| 2115 | Ok(Some("UTF-8".to_owned())) |
| 2116 | } else if #[cfg(windows)] { |
| 2117 | use windows_sys::Win32::System::Console; |
| 2118 | let cp = match fd { |
| 2119 | 0 => unsafe { Console::GetConsoleCP() }, |
| 2120 | 1 | 2 => unsafe { Console::GetConsoleOutputCP() }, |
| 2121 | _ => 0, |
| 2122 | }; |
| 2123 | |
| 2124 | Ok(Some(format!("cp{cp}"))) |
| 2125 | } else { |
| 2126 | let encoding = unsafe { |
| 2127 | let encoding = libc::nl_langinfo(libc::CODESET); |
| 2128 | if encoding.is_null() || encoding.read() == b'\0' as libc::c_char { |
| 2129 | "UTF-8".to_owned() |
| 2130 | } else { |
| 2131 | core::ffi::CStr::from_ptr(encoding).to_string_lossy().into_owned() |
| 2132 | } |
| 2133 | }; |
| 2134 | |
| 2135 | Ok(Some(encoding)) |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | #[pystruct_sequence_data] |
| 2141 | #[allow(dead_code)] |