| 298 | /// Get the current locale encoding. |
| 299 | #[pyfunction] |
| 300 | fn getencoding() -> String { |
| 301 | #[cfg(windows)] |
| 302 | { |
| 303 | // On Windows, use GetACP() to get the ANSI code page |
| 304 | let acp = unsafe { GetACP() }; |
| 305 | format!("cp{}", acp) |
| 306 | } |
| 307 | #[cfg(not(windows))] |
| 308 | { |
| 309 | // On Unix, use nl_langinfo(CODESET) or fallback to UTF-8 |
| 310 | #[cfg(all( |
| 311 | unix, |
| 312 | not(any(target_os = "ios", target_os = "android", target_os = "redox")) |
| 313 | ))] |
| 314 | { |
| 315 | unsafe { |
| 316 | let codeset = libc::nl_langinfo(libc::CODESET); |
| 317 | if !codeset.is_null() |
| 318 | && let Ok(s) = CStr::from_ptr(codeset).to_str() |
| 319 | && !s.is_empty() |
| 320 | { |
| 321 | return s.to_string(); |
| 322 | } |
| 323 | } |
| 324 | "UTF-8".to_string() |
| 325 | } |
| 326 | #[cfg(any(target_os = "ios", target_os = "android", target_os = "redox"))] |
| 327 | { |
| 328 | "UTF-8".to_string() |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |