-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
youknowone
merged 30 commits into
RustPython:main
from
minhrongcon2000:fix/add-localeconv-function
Feb 28, 2023
+169
−4
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 1ed1b29
Fix potential infinite loop
minhrongcon2000 7e9390c
Skip locale test
minhrongcon2000 b2c1e5f
Refactor and add platform def for localeconv
minhrongcon2000 0dd95e0
Add missing platform def on constant
minhrongcon2000 65f5036
Fix wrong format
minhrongcon2000 359c696
Fix platform dependent error build
minhrongcon2000 1533f7b
Add platform def at the top of locale module
minhrongcon2000 09c750c
Refactor code
minhrongcon2000 844a30a
Fix mismatch typing
minhrongcon2000 26f103a
Use libc::c_char instead
minhrongcon2000 9374005
Fix skip test reason for locale
minhrongcon2000 757545f
Add setlocale function
minhrongcon2000 184f891
Merge commit '757545fe37c347c75a5734e0fa9ef9d13a3bcce8' into fix/add-…
minhrongcon2000 ecc21e8
Fix clippy requirements
minhrongcon2000 973d2b2
Fix rustfmt issues
minhrongcon2000 2d37933
Remove test_locale skip testcase
minhrongcon2000 082d1ef
Fix unittest
minhrongcon2000 f5730c4
Fix clippy issues
minhrongcon2000 31941a4
Clean up locale module code
minhrongcon2000 0c5ecb5
Remove setlocale test expected failure
minhrongcon2000 26f04e2
Skip format and strcol test case
minhrongcon2000 0268af5
Fix skpTest bug
minhrongcon2000 44a36af
Skip locale test for window
minhrongcon2000 9801409
Fix test locale skip convention
minhrongcon2000 fcfa35a
Revert original skip decorator and polish code
minhrongcon2000 a44a593
Allow test_strcoll_3303
minhrongcon2000 9e5be11
Expect failure instead of skip
minhrongcon2000 6291ca9
Update Lib/test/test_locale.py
youknowone 38d2253
clean up and additional error handling for NulError
youknowone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Clean up locale module code
- Loading branch information
commit 31941a46b7336a43253d5182748f6a167f30ca19
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,10 @@ mod _locale { | |
| T_FMT_AMPM, YESEXPR, | ||
| }; | ||
|
|
||
| use std::{ffi::CStr, ptr}; | ||
| use std::{ | ||
| ffi::{CStr, CString}, | ||
| ptr, | ||
| }; | ||
|
|
||
| #[pyattr(name = "CHAR_MAX")] | ||
| fn char_max(vm: &VirtualMachine) -> PyIntRef { | ||
|
|
@@ -44,7 +47,7 @@ mod _locale { | |
| vm.ctx.new_list(group_vec) | ||
| } | ||
|
|
||
| unsafe fn _parse_ptr_to_str(vm: &VirtualMachine, raw_ptr: *mut libc::c_char) -> PyResult { | ||
| unsafe fn _parse_ptr_to_str(vm: &VirtualMachine, raw_ptr: *const libc::c_char) -> PyResult { | ||
| let slice = unsafe { CStr::from_ptr(raw_ptr) }; | ||
| let cstr = slice | ||
| .to_str() | ||
|
|
@@ -124,25 +127,13 @@ mod _locale { | |
| #[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) | ||
| 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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not always successful |
||
| let l_ptr = CStr::as_ptr(&l_str); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because CString implements works exactly same way. |
||
| libc::setlocale(args.category, l_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('\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); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all of these constants are available on every platform. When I encountered this sort of problem in the past, I looked up each item individually in the
libcrepository to see which platforms support that item. (See #3840, for example.)I realized back then (and I still maintain) that this process would prove to be tedious and error-prone. Perhaps I should look into using the
autocfgcrate instead, but that's ultimately beyond the scope of this PR.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried with Window locale.h this afternoon and found out that Window API uses different naming convention (prefix with W, if I remember). Hence, if we would like to fix it, I'm afraid that we need to implement our own locale interaction. I will do it in another PR once this module takes shape.
Right now, I put this module limit to unix system only!