Skip to content

format() does not support locales for 'n' presentation type #5181

Closed

Description

@dchiquito

Feature

There are a number of options available for formatting integers using the format() API. The default is 'd'. There is a closely related alternative formatting 'n' which works the same, but uses the locale settings to insert the correct separators. For example, format(123456789, 'n') == '123,456,789' (I am in the US, so my locale specifies , instead of .).

The current implementation does not include the separator, and therefore test_locale in test_format.py is failing.

I see two ways forward:

  1. Make RustPython/Parser/format aware of libc::locale. This would involve adding a dependency on libc.
  2. In int.rs, get the locale info and pass it to rustpython_parse::FormatSpec. This would involve adding an alternative constructor to the RustPython/Parser/format FormatSpec that also accepts locale info.

Python Documentation or reference to CPython source code

https://docs.python.org/3/library/string.html#formatspec, specifically the 'n' presentation type.

Activity

  1. added
    C-compatA discrepancy between RustPython and CPython
    on Feb 24, 2024
  2. youknowone commented on Feb 26, 2024

    @youknowone
    Member

    If anyone interested in, this is a guideline PR: #4609

  3. youknowone commented on Oct 7, 2026

    @youknowone
    Member

    format(..., "n") now follows the active locale's thousands separator and decimal point.

    On current main, locale.setlocale(locale.LC_ALL, "en_US.UTF-8") gives format(1234567, "n") == "1,234,567" and format(1234.5, "n") == "1,234.5". With de_DE.UTF-8 it is "1.234.567" / "1.234,5". Lib/test/test_format.py::test_locale and Lib/test/test_types.py test_*__format__locale no longer carry expectedFailure.

    Closing as resolved. Duplicate tracking: #4613.

    — 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

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions