Skip to content

Commit af04e3e

Browse files
committed
add format precision for string
1 parent 4f14fc7 commit af04e3e

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Lib/test/test_ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def test_iter_fields(self):
939939
self.assertEqual(d.pop('func').id, 'foo')
940940
self.assertEqual(d, {'keywords': [], 'args': []})
941941

942-
# TODO: RUSTPYTHON; redundant kind for Contant node
942+
# TODO: RUSTPYTHON; redundant kind for Constant node
943943
@unittest.expectedFailure
944944
def test_iter_child_nodes(self):
945945
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')

Lib/test/test_fstring.py

-2
Original file line numberDiff line numberDiff line change
@@ -1021,8 +1021,6 @@ def test_leading_trailing_spaces(self):
10211021
self.assertEqual(f'expr={ {x: y for x, y in [(1, 2), ]} }',
10221022
'expr={1: 2}')
10231023

1024-
# TODO: RUSTPYTHON string should be cut at 3 chars
1025-
@unittest.expectedFailure
10261024
def test_not_equal(self):
10271025
# There's a special test for this because there's a special
10281026
# case in the f-string parser to look for != as not ending an

vm/src/format.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,14 @@ impl FormatSpec {
515515

516516
pub(crate) fn format_string(&self, s: &str) -> Result<String, &'static str> {
517517
match self.format_type {
518-
Some(FormatType::String) | None => self.format_sign_and_align(s, "", FormatAlign::Left),
518+
Some(FormatType::String) | None => self
519+
.format_sign_and_align(s, "", FormatAlign::Left)
520+
.map(|mut value| {
521+
if let Some(precision) = self.precision {
522+
value.truncate(precision);
523+
}
524+
value
525+
}),
519526
_ => Err("Unknown format code for object of type 'str'"),
520527
}
521528
}
@@ -944,7 +951,7 @@ impl<'a> FromTemplate<'a> for FormatString {
944951
let mut cur_text: &str = text;
945952
let mut parts: Vec<FormatPart> = Vec::new();
946953
while !cur_text.is_empty() {
947-
// Try to parse both literals and bracketed format parts util we
954+
// Try to parse both literals and bracketed format parts until we
948955
// run out of text
949956
cur_text = FormatString::parse_literal(cur_text)
950957
.or_else(|_| FormatString::parse_spec(cur_text))

0 commit comments

Comments
 (0)