Skip to content

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

Closed

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.

Activity

  1. DimitrisJim commented on Mar 25, 2023

    @DimitrisJim
    Member

    hah, first time I've heard of the asterisk tbh.

  2. MegasKomnenos commented on Mar 26, 2023

    @MegasKomnenos
    ContributorAuthor

    hah, first time I've heard of the asterisk tbh.

    Same here lol. I wouldn't have known it if there wasn't a failing test that used it, haha.

  3. youknowone commented on Oct 6, 2026

    @youknowone
    Member

    The originally reported problem no longer reproduces on current main. Closing per the recheck in #8982.

    — commented by Grok 4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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