Skip to content

Commit 10c9db6

Browse files
authored
Merge pull request RustPython#4392 from yt2b/fix_prefix_format
Prefix format with padding doesn't work correctly.
2 parents f2bab80 + c8c5abe commit 10c9db6

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

extra_tests/snippets/builtin_format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ def test_zero_padding():
3333
assert f"{0x1f5a5:c}" == "🖥"
3434
assert_raises(ValueError, "{:+c}".format, 1, _msg="Sign not allowed with integer format specifier 'c'")
3535
assert_raises(ValueError, "{:#c}".format, 1, _msg="Alternate form (#) not allowed with integer format specifier 'c'")
36+
assert f"{256:#010x}" == "0x00000100"
37+
assert f"{256:0=#10x}" == "0x00000100"
38+
assert f"{256:0>#10x}" == "000000x100"
39+
assert f"{256:0^#10x}" == "000x100000"
40+
assert f"{256:0<#10x}" == "0x10000000"
41+
assert f"{512:+#010x}" == "+0x0000200"
42+
assert f"{512:0=+#10x}" == "+0x0000200"
43+
assert f"{512:0>+#10x}" == "0000+0x200"
44+
assert f"{512:0^+#10x}" == "00+0x20000"
45+
assert f"{512:0<+#10x}" == "+0x2000000"

vm/src/format.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,7 @@ impl FormatSpec {
504504
},
505505
None => self.format_int_radix(magnitude, 10),
506506
};
507-
let magnitude_string = format!(
508-
"{}{}",
509-
prefix,
510-
self.add_magnitude_separators(raw_magnitude_string_result?)
511-
);
512-
507+
let magnitude_string = self.add_magnitude_separators(raw_magnitude_string_result?);
513508
let format_sign = self.sign.unwrap_or(FormatSign::Minus);
514509
let sign_str = match num.sign() {
515510
Sign::Minus => "-",
@@ -519,8 +514,8 @@ impl FormatSpec {
519514
FormatSign::MinusOrSpace => " ",
520515
},
521516
};
522-
523-
self.format_sign_and_align(&magnitude_string, sign_str, FormatAlign::Right)
517+
let sign_prefix = format!("{}{}", sign_str, prefix);
518+
self.format_sign_and_align(&magnitude_string, &sign_prefix, FormatAlign::Right)
524519
}
525520

526521
pub(crate) fn format_string(&self, s: &str) -> Result<String, &'static str> {

0 commit comments

Comments
 (0)