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

Function setlocale

crates/stdlib/src/locale.rs:262–296  ·  view source on GitHub ↗
(args: LocaleArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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]

Callers

nothing calls this directly

Calls 11

check_locale_name_allFunction · 0.85
check_locale_nameFunction · 0.85
newFunction · 0.85
pystr_from_raw_cstrFunction · 0.85
new_exception_msgMethod · 0.80
errorFunction · 0.70
ErrClass · 0.50
flattenMethod · 0.45
as_strMethod · 0.45
to_pyexceptionMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected