8000 Add localeconv function to locale module by minhrongcon2000 · Pull Request #4558 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Add localeconv function to locale module #4558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b3e39a2
Add localeconv function to locale module
minhrongcon2000 Feb 24, 2023
1ed1b29
Fix potential infinite loop
minhrongcon2000 Feb 24, 2023
7e9390c
Skip locale test
minhrongcon2000 Feb 24, 2023
b2c1e5f
Refactor and add platform def for localeconv
minhrongcon2000 Feb 24, 2023
0dd95e0
Add missing platform def on constant
minhrongcon2000 Feb 24, 2023
65f5036
Fix wrong format
minhrongcon2000 Feb 24, 2023
359c696
Fix platform dependent error build
minhrongcon2000 Feb 24, 2023
1533f7b
Add platform def at the top of locale module
minhrongcon2000 Feb 24, 2023
8000
09c750c
Refactor code
minhrongcon2000 Feb 24, 2023
844a30a
Fix mismatch typing
minhrongcon2000 Feb 24, 2023
26f103a
Use libc::c_char instead
minhrongcon2000 Feb 24, 2023
9374005
Fix skip test reason for locale
minhrongcon2000 Feb 24, 2023
757545f
Add setlocale function
minhrongcon2000 Feb 24, 2023
184f891
Merge commit '757545fe37c347c75a5734e0fa9ef9d13a3bcce8' into fix/add-…
minhrongcon2000 Feb 25, 2023
ecc21e8
Fix clippy requirements
minhrongcon2000 Feb 25, 2023
973d2b2
Fix rustfmt issues
minhrongcon2000 Feb 25, 2023
2d37933
Remove test_locale skip testcase
minhrongcon2000 Feb 25, 2023
082d1ef
Fix unittest
minhrongcon2000 Feb 25, 2023
f5730c4
Fix clippy issues
minhrongcon2000 Feb 25, 2023
31941a4
Clean up locale module code
minhrongcon2000 Feb 25, 2023
0c5ecb5
Remove setlocale test expected failure
minhrongcon2000 Feb 25, 2023
26f04e2
Skip format and strcol test case
minhrongcon2000 Feb 28, 2023
0268af5
Fix skpTest bug
minhrongcon2000 Feb 28, 2023
44a36af
Skip locale test for window
minhrongcon2000 Feb 28, 2023
9801409
Fix test locale skip convention
minhrongcon2000 Feb 28, 2023
fcfa35a
Revert original skip decorator and polish code
minhrongcon2000 Feb 28, 2023
a44a593
Allow test_strcoll_3303
minhrongcon2000 Feb 28, 2023
9e5be11
Expect failure instead of skip
minhrongcon2000 Feb 28, 2023
6291ca9
Update Lib/test/test_locale.py
youknowone Feb 28, 2023
38d2253
clean up and additional error handling for NulError
youknowone Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
Prev Previous commit
Next Next commit
Refactor code
  • Loading branch information
minhrongcon2000 committed Feb 24, 2023
commit 09c750ce8bee86bf0a162af7d3d1303be8e22b6a
5 changes: 1 addition & 4 deletions stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
"_posixsubprocess" => posixsubprocess::make_module,
"syslog" => syslog::make_module,
"mmap" => mmap::make_module,
"_locale" => locale::make_module,
}
#[cfg(all(unix, not(target_os = "redox")))]
{
Expand All @@ -160,9 +161,5 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
{
"_uuid" => uuid::make_module,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
"_locale" => locale::make_module,
}
}
}
68 changes: 26 additions & 42 deletions stdlib/src/locale.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[cfg(unix)]
pub(crate) use _locale::make_module;

#[cfg(any(target_os = "linux", target_os = "macos"))]
#[cfg(unix)]
#[pymodule]
mod _locale {
extern crate libc;
use rustpython_vm::{
builtins::{PyDictRef, PyIntRef, PyListRef},
PyObjectRef, PyResult, VirtualMachine,
Expand All @@ -28,11 +27,11 @@ mod _locale {
vm.ctx.new_int(libc::c_char::MAX)
}

unsafe fn _get_grouping(group: *mut c_char, vm: &VirtualMachine) -> PyListRef {
unsafe fn copy_grouping(group: *mut c_char, vm: &VirtualMachine) -> PyListRef {
let mut group_vec: Vec<PyObjectRef> = Vec::new();
let mut ptr = group;

while *ptr != (u8::MIN as i8) && *ptr != i8::MAX {
while ![0 as i8, c_char::MAX].contains(&*ptr) {
let val = vm.ctx.new_int(*ptr);
group_vec.push(val.into());
ptr = ptr.offset(1);
Expand All @@ -44,54 +43,39 @@ mod _locale {
let slice = unsafe { CStr::from_ptr(raw_ptr) };
let cstr = slice
.to_str()
.map(|s| s.to_owned())
.map_err(|e| vm.new_unicode_decode_error(format!("unable to decode: {e}")))?;
.expect("localeconv always return decodable string");

Ok(vm.new_pyobj(cstr))
}

#[pyfunction]
fn localeconv(vm: &VirtualMachine) -> PyResult<PyDictRef> {
let result = vm.ctx.new_dict();
macro_rules! set_string_field {
($field:expr) => {{
result.set_item(stringify!($field), _parse_ptr_to_str(vm, $field)?, vm)?
}};
}

macro_rules! set_int_field {
($field:expr) => {{
result.set_item(stringify!($field), vm.new_pyobj($field), vm)?
}};
}

macro_rules! set_group_field {
($field:expr) => {{
result.set_item(stringify!($field), _get_grouping($field, vm).into(), vm)?
}};
}

unsafe {
let lc = libc::localeconv();

let mon_grouping = (*lc).mon_grouping;
let int_frac_digits = (*lc).int_frac_digits;
let frac_digits = (*lc).frac_digits;
let p_cs_precedes = (*lc).p_cs_precedes;
let p_sep_by_space = (*lc).p_sep_by_space;
let n_cs_precedes = (*lc).n_cs_precedes;
let p_sign_posn = (*lc).p_sign_posn;
let n_sign_posn = (*lc).n_sign_posn;
let grouping = (*lc).grouping;
let decimal_point = (*lc).decimal_point;
let thousands_sep = (*lc).thousands_sep;
let int_curr_symbol = (*lc).int_curr_symbol;
let currency_symbol = (*lc).currency_symbol;
let mon_decimal_point = (*lc).mon_decimal_point;
let mon_thousands_sep = (*lc).mon_thousands_sep;
let n_sep_by_space = (*lc).n_sep_by_space;
let positive_sign = (*lc).positive_sign;
let negative_sign = (*lc).negative_sign;
macro_rules! set_string_field {
($field:ident) => {{
result.set_item(stringify!($field), _parse_ptr_to_str(vm, (*lc).$field)?, vm)?
}};
}

macro_rules! set_int_field {
($field:ident) => {{
result.set_item(stringify!($field), vm.new_pyobj((*lc).$field), vm)?
5FA0 }};
}

macro_rules! set_group_field {
($field:ident) => {{
result.set_item(
stringify!($field),
copy_grouping((*lc).$field, vm).into(),
vm,
)?
}};
}

set_group_field!(mon_grouping);
set_group_field!(grouping);
Expand Down
0