(args: LocaleArgs, vm: &VirtualMachine)
| 260 | |
| 261 | #[pyfunction] |
| 262 | fn setlocale(args: LocaleArgs, vm: &VirtualMachine) -> PyResult { |
| 263 | let error = error(vm); |
| 264 | if cfg!(windows) && (args.category < LC_ALL || args.category > LC_TIME) { |
| 265 | return Err(vm.new_exception_msg(error, "unsupported locale setting".into())); |
| 266 | } |
| 267 | unsafe { |
| 268 | let result = match args.locale.flatten() { |
| 269 | None => libc::setlocale(args.category, ptr::null()), |
| 270 | Some(locale) => { |
| 271 | let locale_str = locale.as_str(); |
| 272 | // On Windows, validate encoding name length |
| 273 | #[cfg(windows)] |
| 274 | { |
| 275 | let valid = if args.category == LC_ALL { |
| 276 | check_locale_name_all(locale_str) |
| 277 | } else { |
| 278 | check_locale_name(locale_str) |
| 279 | }; |
| 280 | if !valid { |
| 281 | return Err( |
| 282 | vm.new_exception_msg(error, "unsupported locale setting".into()) |
| 283 | ); |
| 284 | } |
| 285 | } |
| 286 | let c_locale: CString = |
| 287 | CString::new(locale_str).map_err(|e| e.to_pyexception(vm))?; |
| 288 | libc::setlocale(args.category, c_locale.as_ptr()) |
| 289 | } |
| 290 | }; |
| 291 | if result.is_null() { |
| 292 | return Err(vm.new_exception_msg(error, "unsupported locale setting".into())); |
| 293 | } |
| 294 | pystr_from_raw_cstr(vm, result) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// Get the current locale encoding. |
| 299 | #[pyfunction] |
nothing calls this directly
no test coverage detected