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

Skip to content
Merged
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
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
Diff view
Prev Previous commit
clean up and additional error handling for NulError
  • Loading branch information
youknowone committed Feb 28, 2023
commit 38d22537080f71c6f4014cbaf21b1952731bc889
44 changes: 25 additions & 19 deletions stdlib/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ pub(crate) use _locale::make_module;
mod _locale {
use rustpython_vm::{
builtins::{PyDictRef, PyIntRef, PyListRef, PyStrRef, PyTypeRef},
convert::ToPyException,
function::OptionalArg,
PyObjectRef, PyResult, VirtualMachine,
};
use std::{
ffi::{CStr, CString},
ptr,
};

#[pyattr]
use libc::{
Expand All @@ -21,39 +26,36 @@ mod _locale {
T_FMT_AMPM, YESEXPR,
};

use std::{
ffi::{CStr, CString},
ptr,
};

#[pyattr(name = "CHAR_MAX")]
fn char_max(vm: &VirtualMachine) -> PyIntRef {
vm.ctx.new_int(libc::c_char::MAX)
}

unsafe fn copy_grouping(group: *mut libc::c_char, vm: &VirtualMachine) -> PyListRef {
unsafe fn copy_grouping(group: *const libc::c_char, vm: &VirtualMachine) -> PyListRef {
let mut group_vec: Vec<PyObjectRef> = Vec::new();
let mut ptr = group;
if group.is_null() {
return vm.ctx.new_list(group_vec);
}

let mut ptr = group;
while ![0_i8, libc::c_char::MAX].contains(&*ptr) {
let val = vm.ctx.new_int(*ptr);
group_vec.push(val.into());
ptr = ptr.offset(1);
ptr = ptr.add(1);
}
// https://github.com/python/cpython/blob/677320348728ce058fa3579017e985af74a236d4/Modules/_localemodule.c#L80
if !group_vec.is_empty() {
group_vec.push(vm.ctx.new_int(0i32).into());
group_vec.push(vm.ctx.new_int(0).into());
}
vm.ctx.new_list(group_vec)
}

unsafe fn _parse_ptr_to_str(vm: &VirtualMachine, raw_ptr: *const libc::c_char) -> PyResult {
unsafe fn pystr_from_raw_cstr(vm: &VirtualMachine, raw_ptr: *const libc::c_char) -> PyResult {
let slice = unsafe { CStr::from_ptr(raw_ptr) };
let cstr = slice
let string = slice
.to_str()
.expect("localeconv always return decodable string");

Ok(vm.new_pyobj(cstr))
Ok(vm.new_pyobj(string))
}

#[pyattr(name = "Error", once)]
Expand All @@ -74,7 +76,11 @@ mod _locale {

macro_rules! set_string_field {
($field:ident) => {{
result.set_item(stringify!($field), _parse_ptr_to_str(vm, (*lc).$field)?, vm)?
result.set_item(
stringify!($field),
pystr_from_raw_cstr(vm, (*lc).$field)?,
vm,
)?
}};
}

Expand Down Expand Up @@ -129,17 +135,17 @@ mod _locale {
unsafe {
let result = match args.locale.flatten() {
None => libc::setlocale(args.category, ptr::null()),
Some(l) => {
let l_str = CString::new(l.to_string()).expect("expect to be always converted");
let l_ptr = CStr::as_ptr(&l_str);
libc::setlocale(args.category, l_ptr)
Some(locale) => {
let c_locale: CString =
CString::new(locale.as_str()).map_err(|e| e.to_pyexception(vm))?;
libc::setlocale(args.category, c_locale.as_ptr())
}
};
if result.is_null() {
let error = error(vm);
return Err(vm.new_exception_msg(error, String::from("unsupported locale setting")));
}
_parse_ptr_to_str(vm, result)
pystr_from_raw_cstr(vm, result)
}
}
}
0