From 8a223b81782bf58bb5f307782597c855d2beea6f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 13 Mar 2023 20:37:16 +0900 Subject: [PATCH] Fix unix localeconv to refer libc and additional clean up --- stdlib/src/lib.rs | 3 ++- stdlib/src/locale.rs | 46 +++++++------------------------------------- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/stdlib/src/lib.rs b/stdlib/src/lib.rs index d5f66f32b4..33ba6d875d 100644 --- a/stdlib/src/lib.rs +++ b/stdlib/src/lib.rs @@ -15,6 +15,7 @@ mod dis; mod gc; mod hashlib; mod json; +#[cfg(not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")))] mod locale; mod math; #[cfg(unix)] @@ -160,7 +161,7 @@ pub fn get_module_inits() -> impl Iterator, StdlibInit { "_uuid" => uuid::make_module, } - #[cfg(any(unix, windows, not(any(target_os = "ios", target_os = "android", target_arch="wasm32"))))] + #[cfg(not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")))] { "_locale" => locale::make_module, } diff --git a/stdlib/src/locale.rs b/stdlib/src/locale.rs index 412dec1b6e..f0c88d688d 100644 --- a/stdlib/src/locale.rs +++ b/stdlib/src/locale.rs @@ -1,11 +1,6 @@ -#[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) -))] pub(crate) use _locale::make_module; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(windows)] #[repr(C)] struct lconv { decimal_point: *mut libc::c_char, @@ -34,16 +29,14 @@ struct lconv { int_n_sign_posn: libc::c_char, } -#[cfg(not(target_arch = "wasm32"))] +#[cfg(windows)] extern "C" { fn localeconv() -> *mut lconv; } -#[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) -))] +#[cfg(unix)] +use libc::localeconv; + #[pymodule] mod _locale { use rustpython_vm::{ @@ -71,11 +64,6 @@ mod _locale { #[pyattr] use libc::{LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; - #[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) - ))] #[pyattr(name = "CHAR_MAX")] fn char_max(vm: &VirtualMachine) -> PyIntRef { vm.ctx.new_int(libc::c_char::MAX) @@ -108,11 +96,6 @@ mod _locale { Ok(vm.new_pyobj(string)) } - #[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) - ))] #[pyattr(name = "Error", once)] fn error(vm: &VirtualMachine) -> PyTypeRef { vm.ctx.new_exception_type( @@ -122,11 +105,6 @@ mod _locale { ) } - #[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) - ))] #[pyfunction] fn strcoll(string1: PyStrRef, string2: PyStrRef, vm: &VirtualMachine) -> PyResult { let cstr1 = CString::new(string1.as_str()).map_err(|e| e.to_pyexception(vm))?; @@ -134,11 +112,6 @@ mod _locale { Ok(vm.new_pyobj(unsafe { libc::strcoll(cstr1.as_ptr(), cstr2.as_ptr()) })) } - #[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) - ))] #[pyfunction] fn strxfrm(string: PyStrRef, vm: &VirtualMachine) -> PyResult { // https://github.com/python/cpython/blob/eaae563b6878aa050b4ad406b67728b6b066220e/Modules/_localemodule.c#L390-L442 @@ -154,8 +127,8 @@ mod _locale { Ok(vm.new_pyobj(String::from_utf8(buff).expect("strxfrm returned invalid utf-8 string"))) } - #[pyfunction(name = "localeconv")] - fn _localeconv(vm: &VirtualMachine) -> PyResult { + #[pyfunction] + fn localeconv(vm: &VirtualMachine) -> PyResult { let result = vm.ctx.new_dict(); unsafe { @@ -216,11 +189,6 @@ mod _locale { locale: OptionalArg>, } - #[cfg(any( - unix, - windows, - not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")) - ))] #[pyfunction] fn setlocale(args: LocaleArgs, vm: &VirtualMachine) -> PyResult { let error = error(vm);