Description
In string formatting, a print-f style format specifier can have its amount and precision formatted by variable value through use of star asterik(%*s).
In cformat_bytes and cformat_string, through try_update_quantity_from_tuple, try_update_quantity_from_element is used to update quantity if possible, but it makes the assumption that its behavior should be the same regardless of the sign value of the given element.
if let Some(i) = width_obj.payload::<PyInt>() {
let i = i.try_to_primitive::<i32>(vm)?.unsigned_abs();
Ok(CFormatQuantity::Amount(i as usize))
}However, that assumption is false, as an amount can be preceded by a negative sign, and it should make the resulting string left adjusted, according to format specifier rule.
https://docs.python.org/3/library/stdtypes.html#old-string-formatting
assert('%*s' % (-5, 'abc') == 'abc ')This issue makes this test to fail.
I think this issue can be resolved by checking the sign of the element and adding the minus sign accordingly.
I'm working on the fix at the moment.
Activity
Fix issue RustPython#4762
Fix RustPython#4762
DimitrisJim commentedon Mar 25, 2023
hah, first time I've heard of the asterisk tbh.
MegasKomnenos commentedon Mar 26, 2023
Same here lol. I wouldn't have known it if there wasn't a failing test that used it, haha.
youknowone commentedon Oct 6, 2026
The originally reported problem no longer reproduces on current main. Closing per the recheck in #8982.
— commented by Grok 4.6