Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb202af

Browse files
authored
gh-99606: Make code generated for an empty f-string identical to that of a normal empty string (#112407)
1 parent418d585 commitfb202af

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

‎Lib/test/test_fstring.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# Unicode identifiers in tests is allowed by PEP 3131.
99

1010
importast
11+
importdis
1112
importos
1213
importre
1314
importtypes
@@ -1738,5 +1739,14 @@ def test_syntax_warning_infinite_recursion_in_file(self):
17381739
self.assertIn(rb'\1',stdout)
17391740
self.assertEqual(len(stderr.strip().splitlines()),2)
17401741

1742+
deftest_fstring_without_formatting_bytecode(self):
1743+
# f-string without any formatting should emit the same bytecode
1744+
# as a normal string. See gh-99606.
1745+
defget_code(s):
1746+
return [(i.opname,i.oparg)foriindis.get_instructions(s)]
1747+
1748+
forsin ["","some string"]:
1749+
self.assertEqual(get_code(f"'{s}'"),get_code(f"f'{s}'"))
1750+
17411751
if__name__=='__main__':
17421752
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make code generated for an empty f-string identical to the code of an empty
2+
normal string.

‎Python/compile.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5042,8 +5042,12 @@ compiler_joined_str(struct compiler *c, expr_ty e)
50425042
}
50435043
else {
50445044
VISIT_SEQ(c,expr,e->v.JoinedStr.values);
5045-
if (asdl_seq_LEN(e->v.JoinedStr.values)!=1) {
5046-
ADDOP_I(c,loc,BUILD_STRING,asdl_seq_LEN(e->v.JoinedStr.values));
5045+
if (value_count>1) {
5046+
ADDOP_I(c,loc,BUILD_STRING,value_count);
5047+
}
5048+
elseif (value_count==0) {
5049+
_Py_DECLARE_STR(empty,"");
5050+
ADDOP_LOAD_CONST_NEW(c,loc,Py_NewRef(&_Py_STR(empty)));
50475051
}
50485052
}
50495053
returnSUCCESS;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp