1
- #[ cfg( any ( target_os = "linux" , target_os = "macos" ) ) ]
1
+ #[ cfg( unix ) ]
2
2
pub ( crate ) use _locale:: make_module;
3
3
4
- #[ cfg( any ( target_os = "linux" , target_os = "macos" ) ) ]
4
+ #[ cfg( unix ) ]
5
5
#[ pymodule]
6
6
mod _locale {
7
- extern crate libc;
8
7
use rustpython_vm:: {
9
8
builtins:: { PyDictRef , PyIntRef , PyListRef } ,
10
9
PyObjectRef , PyResult , VirtualMachine ,
@@ -28,11 +27,11 @@ mod _locale {
28
27
vm. ctx . new_int ( libc:: c_char:: MAX )
29
28
}
30
29
31
- unsafe fn _get_grouping ( group : * mut c_char , vm : & VirtualMachine ) -> PyListRef {
30
+ unsafe fn copy_grouping ( group : * mut c_char , vm : & VirtualMachine ) -> PyListRef {
32
31
let mut group_vec: Vec < PyObjectRef > = Vec :: new ( ) ;
33
32
let mut ptr = group;
34
33
35
- while * ptr != ( u8 :: MIN as i8 ) && * ptr != i8 :: MAX {
34
+ while ! [ 0 as i8 , c_char :: MAX ] . contains ( & * ptr ) {
36
35
let val = vm. ctx . new_int ( * ptr) ;
37
36
group_vec. push ( val. into ( ) ) ;
38
37
ptr = ptr. offset ( 1 ) ;
@@ -44,54 +43,39 @@ mod _locale {
44
43
let slice = unsafe { CStr :: from_ptr ( raw_ptr) } ;
45
44
let cstr = slice
46
45
. 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" ) ;
49
47
50
48
Ok ( vm. new_pyobj ( cstr) )
51
49
}
52
50
53
51
#[ pyfunction]
54
52
fn localeconv ( vm : & VirtualMachine ) -> PyResult < PyDictRef > {
55
53
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
- }
73
54
74
55
unsafe {
75
56
let lc = libc:: localeconv ( ) ;
76
57
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
+ }
95
79
96
80
set_group_field ! ( mon_grouping) ;
97
81
set_group_field ! ( grouping) ;
0 commit comments