From 93951a4a08d14aa9deca1b1d9e4dc1afd2684534 Mon Sep 17 00:00:00 2001 From: Maxwell Bernstein Date: Wed, 18 Mar 2020 16:47:24 -0700 Subject: [PATCH 1/2] bpo-39985: Make string.Formatter with empty field name default to 0 This causes string.Formatter to behave like str.format in cases like `"[0]".format((1, 2, 3))`, where `[0]` implicitly refers to the first argument to `str.format`. --- Lib/string.py | 5 +++++ Lib/test/test_string.py | 1 + 2 files changed, 6 insertions(+) diff --git a/Lib/string.py b/Lib/string.py index 489777b10c25df7..6bad050eef5ca34 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -222,8 +222,13 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, def get_value(self, key, args, kwargs): if isinstance(key, int): + # "1234[x]" return args[key] + elif key == "": + # "[x]" + return args[0] else: + # "abcd[x]" return kwargs[key] diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index 0be28fdb609eaee..bca5d165ab83a74 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -96,6 +96,7 @@ def test_index_lookup(self): fmt = string.Formatter() lookup = ["eggs", "and", "spam"] self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') + self.assertEqual(fmt.format("{[2]}{1[0]}", lookup, (1, 2, 3)), 'spam1') with self.assertRaises(IndexError): fmt.format("{0[2]}{0[0]}", []) with self.assertRaises(KeyError): From a6bc0ef6cec9419620c26373688a02f12953d13b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 19 Mar 2020 00:20:32 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst diff --git a/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst b/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst new file mode 100644 index 000000000000000..951e76acceaaef8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst @@ -0,0 +1 @@ +Make string.Formatter behave like str.format with anonymous subscripts. \ No newline at end of file