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
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
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
Next Next commit
Add setlocale function
  • Loading branch information
minhrongcon2000 committed Feb 25, 2023
commit 757545fe37c347c75a5734e0fa9ef9d13a3bcce8
54 changes: 51 additions & 3 deletions stdlib/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub(crate) use _locale::make_module;
#[pymodule]
mod _locale {
use rustpython_vm::{
builtins::{PyDictRef, PyIntRef, PyListRef},
PyObjectRef, PyResult, VirtualMachine,
builtins::{PyDictRef, PyIntRef, PyListRef, PyStrRef, PyTypeRef},
PyObjectRef, PyResult, VirtualMachine, function::OptionalArg,
};

#[pyattr]
Expand All @@ -20,7 +20,7 @@ mod _locale {
T_FMT_AMPM, YESEXPR,
};

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

#[pyattr(name = "CHAR_MAX")]
fn char_max(vm: &VirtualMachine) -> PyIntRef {
Expand All @@ -47,6 +47,15 @@ mod _locale {

Ok(vm.new_pyobj(cstr))
}

#[pyattr(name="Error")]
fn error(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"locale",
"locale.Error",
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
)
}

#[pyfunction]
fn localeconv(vm: &VirtualMachine) -> PyResult<PyDictRef> {
Expand Down Expand Up @@ -98,4 +107,43 @@ mod _locale {
}
Ok(result)
}

#[derive(FromArgs)]
struct LocaleArgs {
#[pyarg(any)]
category: i32,
#[pyarg(any, optional)]
locale: OptionalArg<Option<PyStrRef>>
}

#[pyfunction]
fn setlocale(args: LocaleArgs, vm: &VirtualMachine) -> PyResult {
unsafe {
let result = match args.locale {
OptionalArg::Missing => {
let null_ptr: *const i8 = ptr::null();
libc::setlocale(args.category, null_ptr)
},
OptionalArg::Present(locale) => match locale {
None => {
let null_ptr: *const i8 = ptr::null();
libc::setlocale(args.category, null_ptr)
},
Some(l) => {
let mut l_str = l.to_string();
if l_str.is_empty() {
l_str.push_str("\0");
}
let l_ptr = l_str.as_ptr() as *const i8;
libc::setlocale(args.category, l_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)
}
}
}
0