GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Extend PyUnicode_FromFormat() · Issue #98836 · python/cpython

Extend PyUnicode_FromFormat() #98836

New issue
Copy link
New issue
Copy link
Closed
Closed
Extend PyUnicode_FromFormat()#98836
Copy link
Labels
topic-C-APItopic-unicodetype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Oct 29, 2022
Issue body actions

PyUnicode_FromFormat() and several other functions like PyErr_Format() support a subset of printf-like formatting with some extensions to support Python objects. It is a very limited subset, for example %x is supported, but %lx and %X are not.

I propose to add support of more printf features:

  • Support for conversion specifiers o (octal) and X (uppercase hexadecimal).
  • Support for length modifiers j (intmax_t) and t (ptrdiff_t).
  • Length modifiers are now applied to all integer conversions.
  • Support for wchar_t C strings (%ls and %lV).
  • Support for variable width and precision *.
  • Support for flag - (left alignment).

The following standard features are intentionally not implemented:

  • Support for floating point formatting. It is very rare in error messages and reprs, and you always can use sprintf to a fixed buffer.
  • Flags #, (a space) and +. # is ambiguous for octals: should we use prefix 0 (as in C) or 0o (as in Python)? The latter two flags are just rarely used (I initially implemented the support of them, but then removed, it is not worth).
  • Length modifiers h (signed char and unsigned char) and hh (short and unsigned short). Values of these types are automatically promoted to int or unsigned int, and you can use explicit conversion to int or unsigned int in case of ambiguity.
  • %lc. Since %c already accepts integers outside of the range 0-255, and the difference between int and wint_t is subtle, I am not sure that that there is any value of supporting it.
  • %n.
  • Width and precision are not supported in %c and %p.

Unlike to printf, unsupported modifiers (like %lc or %10c) raise SystemError instead of be silently ignored. It will allow to add new features without breaking accidentally working code.

  • PR: gh-98836: Extend PyUnicode_FromFormat() #98838

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-unicode
    topic-C-API
    on Oct 29, 2022
  2. serhiy-storchaka
    added a commit that references this issue on Oct 29, 2022

    pythongh-98836: Extend PyUnicode_FromFormat()

    aca2393
  3. serhiy-storchaka commented on Oct 29, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 29, 2022
    MemberAuthor
    More actions

    I am not sure that it is worth support ptrdiff_t if we supported intmax_t. Removing it would save us 8 lines of code. Support of char and short integers would cost 10 lines of code each. Either is cheap.

    I am ready to discuss inclusion or exclusion of other formatting features.

  4. serhiy-storchaka
    added a commit that references this issue on May 21, 2023

    gh-98836: Extend PyUnicode_FromFormat() (GH-98838)

    f3466bc
  5. serhiy-storchaka
    closed this as completedon May 21, 2023
  6. StanFromIreland
    marked PyUnicode_FromFormat(): add %X format #95849 as a duplicate of this issue on Aug 17, 2025
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

    topic-C-APItopic-unicodetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.