Skip to content

Commit 9b88fdf

Browse files
committed
Fixed issue python#28633: segfault when concatenating bytes literal and f-string.
1 parent f46b782 commit 9b88fdf

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Lib/test/test_fstring.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def test_ast_compile_time_concat(self):
9292
exec(c)
9393
self.assertEqual(x[0], 'foo3')
9494

95+
def test_compile_time_concat_errors(self):
96+
self.assertAllRaise(SyntaxError,
97+
'cannot mix bytes and nonbytes literals',
98+
[r"""f'' b''""",
99+
r"""b'' f''""",
100+
])
101+
95102
def test_literal(self):
96103
self.assertEqual(f'', '')
97104
self.assertEqual(f'a', 'a')

Python/ast.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5147,7 +5147,8 @@ parsestrplus(struct compiling *c, const node *n)
51475147
/* Check that we're not mixing bytes with unicode. */
51485148
if (i != 0 && bytesmode != this_bytesmode) {
51495149
ast_error(c, n, "cannot mix bytes and nonbytes literals");
5150-
Py_DECREF(s);
5150+
/* s is NULL if the current string part is an f-string. */
5151+
Py_XDECREF(s);
51515152
goto error;
51525153
}
51535154
bytesmode = this_bytesmode;
@@ -5161,11 +5162,12 @@ parsestrplus(struct compiling *c, const node *n)
51615162
if (result < 0)
51625163
goto error;
51635164
} else {
5165+
/* A string or byte string. */
5166+
assert(s != NULL && fstr == NULL);
5167+
51645168
assert(bytesmode ? PyBytes_CheckExact(s) :
51655169
PyUnicode_CheckExact(s));
51665170

5167-
/* A string or byte string. */
5168-
assert(s != NULL && fstr == NULL);
51695171
if (bytesmode) {
51705172
/* For bytes, concat as we go. */
51715173
if (i == 0) {
@@ -5177,7 +5179,6 @@ parsestrplus(struct compiling *c, const node *n)
51775179
goto error;
51785180
}
51795181
} else {
5180-
assert(s != NULL && fstr == NULL);
51815182
/* This is a regular string. Concatenate it. */
51825183
if (FstringParser_ConcatAndDel(&state, s) < 0)
51835184
goto error;

0 commit comments

Comments
 (0)