Skip to content

Commit

Permalink
py/objstr: Support '{:08}'.format("Jan") like Python 3.10.
Browse files Browse the repository at this point in the history
The new test has an .exp file, because it is not compatible with Python 3.9
and lower.

See CPython version of the issue at https://bugs.python.org/issue27772

Signed-off-by: Jeff Epler <jepler@gmail.com>
  • Loading branch information
jepler authored and dpgeorge committed Jan 19, 2022
1 parent 5e50656 commit 037b2c7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion py/objstr.c
Expand Up @@ -1163,7 +1163,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
s++;
}
if (*s == '0') {
if (!align) {
if (!align && arg_looks_numeric(arg)) {
align = '=';
}
if (!fill) {
Expand Down
9 changes: 9 additions & 0 deletions tests/basics/string_format_cp310.py
@@ -0,0 +1,9 @@
# Python 3.10+ functionality test for {} format string

def test(fmt, *args):
print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')

test("{:0s}", "ab")
test("{:06s}", "ab")
test("{:<06s}", "ab")
test("{:>06s}", "ab")
4 changes: 4 additions & 0 deletions tests/basics/string_format_cp310.py.exp
@@ -0,0 +1,4 @@
{:0s} >ab<
{:06s} >ab0000<
{:<06s} >ab0000<
{:>06s} >0000ab<
2 changes: 1 addition & 1 deletion tests/basics/string_format_error.py
@@ -1,7 +1,7 @@
# tests for errors in {} format string

try:
'{0:0}'.format('zzz')
'{0:=}'.format('zzz')
except (ValueError):
print('ValueError')

Expand Down

0 comments on commit 037b2c7

Please sign in to comment.