summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-11-26 17:13:57 (GMT)
committerGitHub <noreply@github.com>2023-11-26 17:13:57 (GMT)
commitfb202af4470d6051a69bb9d2f44d7e8a1c99eb4f (patch)
tree6f643f213d787093de62274fe645ccb0963e8f79
parent418d585febd280e779274f313f910613ac1a1c30 (diff)
downloadcpython-fb202af4470d6051a69bb9d2f44d7e8a1c99eb4f.zip
cpython-fb202af4470d6051a69bb9d2f44d7e8a1c99eb4f.tar.gz
cpython-fb202af4470d6051a69bb9d2f44d7e8a1c99eb4f.tar.bz2
gh-99606: Make code generated for an empty f-string identical to that of a normal empty string (#112407)
-rw-r--r--Lib/test/test_fstring.py10
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2023-11-25-20-36-38.gh-issue-99606.fDY5hK.rst2
-rw-r--r--Python/compile.c8
3 files changed, 18 insertions, 2 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index da0160d..27c7f70 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -8,6 +8,7 @@
# Unicode identifiers in tests is allowed by PEP 3131.
import ast
+import dis
import os
import re
import types
@@ -1738,5 +1739,14 @@ print(f'''{{
self.assertIn(rb'\1', stdout)
self.assertEqual(len(stderr.strip().splitlines()), 2)
+ def test_fstring_without_formatting_bytecode(self):
+ # f-string without any formatting should emit the same bytecode
+ # as a normal string. See gh-99606.
+ def get_code(s):
+ return [(i.opname, i.oparg) for i in dis.get_instructions(s)]
+
+ for s in ["", "some string"]:
+ self.assertEqual(get_code(f"'{s}'"), get_code(f"f'{s}'"))
+
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-25-20-36-38.gh-issue-99606.fDY5hK.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-25-20-36-38.gh-issue-99606.fDY5hK.rst
new file mode 100644
index 0000000..adc0e3a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-25-20-36-38.gh-issue-99606.fDY5hK.rst
@@ -0,0 +1,2 @@
+Make code generated for an empty f-string identical to the code of an empty
+normal string.
diff --git a/Python/compile.c b/Python/compile.c
index 8b1eef7..8b9e2f0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -5042,8 +5042,12 @@ compiler_joined_str(struct compiler *c, expr_ty e)
}
else {
VISIT_SEQ(c, expr, e->v.JoinedStr.values);
- if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
- ADDOP_I(c, loc, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
+ if (value_count > 1) {
+ ADDOP_I(c, loc, BUILD_STRING, value_count);
+ }
+ else if (value_count == 0) {
+ _Py_DECLARE_STR(empty, "");
+ ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty)));
}
}
return SUCCESS;