Skip to content

py/objstr: Add 20 missing CPython str methods.#33

Open
mattytrentini wants to merge 4 commits into
masterfrom
str-missing-methods
Open

py/objstr: Add 20 missing CPython str methods.#33
mattytrentini wants to merge 4 commits into
masterfrom
str-missing-methods

Conversation

@mattytrentini

@mattytrentini mattytrentini commented Mar 4, 2026

Copy link
Copy Markdown
Owner

Summary

Adds the following str methods, gated behind new config flags at the EXTRA_FEATURES level: capitalize, swapcase, title, istitle, casefold, ljust, rjust, zfill, translate, maketrans, removeprefix, removesuffix, isalnum, isascii, isdecimal, isidentifier, isnumeric, isprintable.

Add expandtabs and format_map at FULL_FEATURES level.

The str-only methods (casefold, maketrans, isdecimal, isidentifier, isnumeric, isprintable, format_map) are excluded from the bytes and bytearray method tables. ljust, rjust, and zfill are added under the existing MICROPY_PY_BUILTINS_STR_CENTER flag.

These methods are only enabled for the EXTRA_ and FULL_FEATURES configurations and they do incur a non-trivial bump in size (2-3KB ish). However, it does mean that it's easier to port Python applications and seems reasonable for the larger builds.

Testing

The appropriate tests have been extended. Namely everything in tests/basics/string_*.

Trade-offs and Alternatives

These methods could simply not be added; they can all be worked around. But I've seen enough work-arounds to know that they're pretty commonly used and would be appreciated on builds that can afford the space.

Generative AI

I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the code and the description above.

@github-actions

github-actions Bot commented Mar 4, 2026

Copy link
Copy Markdown

Code size report:

Reference:  README: Add a section describing MicroPython's values. [4625f97]
Comparison: esp8266: Disable new str methods to fit in flash. [merge of b1714e3]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64: +6424 +0.750% standard[incl +768(data)]
      stm32: +9184 +2.329% PYBV10[incl +396(bss)]
      esp32: +2428 +0.139% ESP32_GENERIC[incl +640(data)]
     mimxrt: +2424 +0.629% TEENSY40
        rp2: +2104 +0.229% RPI_PICO_W
       samd: +2476 +0.901% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32: +2468 +0.541% VIRT_RV32

@mattytrentini
mattytrentini force-pushed the str-missing-methods branch 4 times, most recently from beba221 to bd41b60 Compare March 23, 2026 00:42
Add the following str methods, gated behind new config flags at
EXTRA_FEATURES level: capitalize, swapcase, title, istitle, casefold,
ljust, rjust, zfill, translate, maketrans, removeprefix, removesuffix,
isalnum, isascii, isdecimal, isidentifier, isnumeric, isprintable.

Add expandtabs and format_map at FULL_FEATURES level.

The str-only methods (casefold, maketrans, isdecimal, isidentifier,
isnumeric, isprintable, format_map) are excluded from the bytes and
bytearray method tables.  ljust, rjust, and zfill are added under the
existing MICROPY_PY_BUILTINS_STR_CENTER flag.

No code-size increase on the minimal variant.

Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
Merge ljust/rjust into a shared str_justify helper, merge
removeprefix/removesuffix into a shared str_removefix helper, and alias
isnumeric to isdecimal (both delegate to unichar_isdigit).

Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
Test TypeError for translate/format_map with non-dict arguments and
ValueError for maketrans with mismatched argument lengths.

Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
The ESP8266 port is too constrained to include the new EXTRA_FEATURES
level string methods. Disable capitalize, title, translate, removefix,
and is_checks to keep the build within flash limits.

Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
@mattytrentini
mattytrentini force-pushed the master branch 3 times, most recently from b188f5b to 552fd21 Compare June 29, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant