8000 Refactor code · RustPython/RustPython@09c750c · GitHub
[go: up one dir, main page]

Skip to content

Commit 09c750c

Browse files
Refactor code
1 parent 1533f7b commit 09c750c

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
lines changed

stdlib/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
139139
"_posixsubprocess" => posixsubprocess::make_module,
140140
"syslog" => syslog::make_module,
141141
"mmap" => mmap::make_module,
142+
"_locale" => locale::make_module,
142143
}
143144
#[cfg(all(unix, not(target_os = "redox")))]
144145
{
@@ -160,9 +161,5 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
160161
{
161162
"_uuid" => uuid::make_module,
162163
}
163-
#[cfg(any(target_os = "linux", target_os = "macos"))]
164-
{
165-
"_locale" => locale::make_module,
166-
}
167164
}
168165
}

stdlib/src/locale.rs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#[cfg(any(target_os = "linux", target_os = "macos"))]
1+
#[cfg(unix)]
22
pub(crate) use _locale::make_module;
33

4-
#[cfg(any(target_os = "linux", target_os = "macos"))]
4+
#[cfg(unix)]
55
#[pymodule]
66
mod _locale {
7-
extern crate libc;
87
use rustpython_vm::{
98
builtins::{PyDictRef, PyIntRef, PyListRef},
109
PyObjectRef, PyResult, VirtualMachine,
@@ -28,11 +27,11 @@ mod _locale {
2827
vm.ctx.new_int(libc::c_char::MAX)
2928
}
3029

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

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

5048
Ok(vm.new_pyobj(cstr))
5149
}
5250

5351
#[pyfunction]
5452
fn localeconv(vm: &VirtualMachine) -> PyResult<PyDictRef> {
5553
let result = vm.ctx.new_dict();
56-
macro_rules! set_string_field {
57-
($field:expr) => {{
58-
result.set_item(stringify!($field), _parse_ptr_to_str(vm, $field)?, vm)?
59-
}};
60-
}
61-
62-
macro_rules! set_int_field {
63-
($field:expr) => {{
64-
result.set_item(stringify!($field), vm.new_pyobj($field), vm)?
65-
}};
66-
}
67-
68-
macro_rules! set_group_field {
69-
($field:expr) => {{
70-
result.set_item(stringify!($field), _get_grouping($field, vm).into(), vm)?
71-
}};
72-
}
7354

7455
unsafe {
7556
let lc = libc::localeconv();
7657

77-
let mon_grouping = (*lc).mon_grouping;
78-
let int_frac_digits = (*lc).int_frac_digits;
79-
let frac_digits = (*lc).frac_digits;
80-
let p_cs_precedes = (*lc).p_cs_precedes;
81-
let p_sep_by_space = (*lc).p_sep_by_space;
82-
let n_cs_precedes = (*lc).n_cs_precedes;
83-
let p_sign_posn = (*lc).p_sign_posn;
84-
let n_sign_posn = (*lc).n_sign_posn;
85-
let grouping = (*lc).grouping;
86-
let decimal_point = (*lc).decimal_point;
87-
let thousands_sep = (*lc).thousands_sep;
88-
let int_curr_symbol = (*lc).int_curr_symbol;
89-
let currency_symbol = (*lc).currency_symbol;
90-
let mon_decimal_point = (*lc).mon_decimal_point;
91-
let mon_thousands_sep = (*lc).mon_thousands_sep;
92-
let n_sep_by_space = (*lc).n_sep_by_space;
93-
let positive_sign = (*lc).positive_sign;
94-
let negative_sign = (*lc).negative_sign;
58+
macro_rules! set_string_field {
59+
($field:ident) => {{
60+
result.set_item(stringify!($field), _parse_ptr_to_str(vm, (*lc).$field)?, vm)?
61+
}};
62+
}
63+
64+
macro_rules! set_int_field {
65+
($field:ident) => {{
66+
result.set_item(stringify!($field), vm.new_pyobj((*lc).$field), vm)?
67+
}};
68+
}
69+
70+
macro_rules! set_group_field {
71+
($field:ident) => {{
72+
result.set_item(
73+
stringify!($field),
74+
copy_grouping((*lc).$field, vm).into(),
75+
vm,
76+
)?
77+
}};
78+
}
9579

9680
set_group_field!(mon_grouping);
9781
set_group_field!(grouping);

0 commit comments

Comments
 (0)
0