py/objstr: Add 20 missing CPython str methods.#33
Open
mattytrentini wants to merge 4 commits into
Open
Conversation
|
Code size report: |
mattytrentini
force-pushed
the
str-missing-methods
branch
from
March 4, 2026 03:25
5a74a8e to
0f97484
Compare
mattytrentini
force-pushed
the
str-missing-methods
branch
4 times, most recently
from
March 23, 2026 00:42
beba221 to
bd41b60
Compare
mattytrentini
force-pushed
the
str-missing-methods
branch
from
April 1, 2026 11:39
6ebddd3 to
ca97181
Compare
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
force-pushed
the
str-missing-methods
branch
from
April 1, 2026 11:42
ca97181 to
b1714e3
Compare
mattytrentini
force-pushed
the
master
branch
3 times, most recently
from
June 29, 2026 04:34
b188f5b to
552fd21
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the following str methods, gated behind new config flags at the
EXTRA_FEATURESlevel: 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_FEATURESlevel.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_CENTERflag.These methods are only enabled for the
EXTRA_andFULL_FEATURESconfigurations 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.