Skip to content

cformat star asterisk should behave differently when given negative sign #4762

@MegasKomnenos

Description

@MegasKomnenos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-stdlibC-bugSomething isn't workingz-ca-2023Tag to track contrubution-academy 2023

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions