Skip to content

baseline to start locale support for formatting :n #4609

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ def test_float__format__locale(self):
self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n'))
self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n'))

@unittest.skip("TODO: RustPython format code n is not integrated with locale")
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
def test_int__format__locale(self):
# test locale support for __format__ code 'n' for integers
Expand Down
23 changes: 19 additions & 4 deletions common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use num_bigint::{BigInt, Sign};
use num_traits::{cast::ToPrimitive, Signed};
use std::{cmp, str::FromStr};

pub struct Locale;
pub type LocaleFunc = fn() -> Locale;

trait FormatParse {
fn parse(text: &str) -> (Option<Self>, &str)
where
Expand Down Expand Up @@ -312,6 +315,7 @@ impl FormatSpec {
inter: i32,
sep: char,
disp_digit_cnt: i32,
locale_info: Option<LocaleFunc>, // will be used to calculate numbers here
) -> String {
// Don't add separators to the floating decimal point of numbers
let mut parts = magnitude_str.splitn(2, '.');
Expand Down Expand Up @@ -391,7 +395,12 @@ impl FormatSpec {
}
}

fn add_magnitude_separators(&self, magnitude_str: String, prefix: &str) -> String {
fn add_magnitude_separators(
&self,
magnitude_str: String,
prefix: &str,
locale_info: Option<LocaleFunc>,
) -> String {
match &self.grouping_option {
Some(fg) => {
let sep = match fg {
Expand All @@ -407,6 +416,7 @@ impl FormatSpec {
inter,
sep,
disp_digit_cnt,
locale_info,
)
}
None => magnitude_str,
Expand Down Expand Up @@ -487,7 +497,7 @@ impl FormatSpec {
FormatSign::MinusOrSpace => " ",
}
};
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str);
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str, None);
self.format_sign_and_align(
unsafe { &BorrowedStr::from_ascii_unchecked(magnitude_str.as_bytes()) },
sign_str,
Expand All @@ -503,7 +513,11 @@ impl FormatSpec {
}
}

pub fn format_int(&self, num: &BigInt) -> Result<String, FormatSpecError> {
pub fn format_int(
&self,
num: &BigInt,
locale_info: Option<LocaleFunc>,
) -> Result<String, FormatSpecError> {
self.validate_format(FormatType::Decimal)?;
let magnitude = num.abs();
let prefix = if self.alternate_form {
Expand Down Expand Up @@ -559,7 +573,8 @@ impl FormatSpec {
},
};
let sign_prefix = format!("{sign_str}{prefix}");
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str, &sign_prefix);
let magnitude_str =
self.add_magnitude_separators(raw_magnitude_str, &sign_prefix, locale_info);
self.format_sign_and_align(
&BorrowedStr::from_bytes(magnitude_str.as_bytes()),
&sign_prefix,
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,9 @@ impl PyInt {

#[pymethod(magic)]
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
let locale_func = None; // TODO: place proper get_locale_info wrapper
FormatSpec::parse(spec.as_str())
.and_then(|format_spec| format_spec.format_int(&self.value))
.and_then(|format_spec| format_spec.format_int(&self.value, locale_func))
.map_err(|err| err.into_pyexception(vm))
}

Expand Down