From 037b2c72a1d5b54a5508a58ab2044628a7a39fa4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 10 Jan 2022 09:03:27 -0600 Subject: [PATCH] py/objstr: Support '{:08}'.format("Jan") like Python 3.10. 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 --- py/objstr.c | 2 +- tests/basics/string_format_cp310.py | 9 +++++++++ tests/basics/string_format_cp310.py.exp | 4 ++++ tests/basics/string_format_error.py | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 tests/basics/string_format_cp310.py create mode 100644 tests/basics/string_format_cp310.py.exp diff --git a/py/objstr.c b/py/objstr.c index 7d7f0e1dfa..321bb058dc 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -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) { diff --git a/tests/basics/string_format_cp310.py b/tests/basics/string_format_cp310.py new file mode 100644 index 0000000000..77295330be --- /dev/null +++ b/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") diff --git a/tests/basics/string_format_cp310.py.exp b/tests/basics/string_format_cp310.py.exp new file mode 100644 index 0000000000..1c26473d83 --- /dev/null +++ b/tests/basics/string_format_cp310.py.exp @@ -0,0 +1,4 @@ +{:0s} >ab< +{:06s} >ab0000< +{:<06s} >ab0000< +{:>06s} >0000ab< diff --git a/tests/basics/string_format_error.py b/tests/basics/string_format_error.py index 708348d59f..74dc6c52ed 100644 --- a/tests/basics/string_format_error.py +++ b/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')