diff options
author | Eric V. Smith <eric@trueblade.com> | 2016-11-07 22:54:01 (GMT) |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2016-11-07 22:54:01 (GMT) |
commit | 9b88fdf4f09b77d866a3591c9e64b4e8a5b25920 (patch) | |
tree | 0dfd16d53f10e3c6768c95c0d28916e09ef6685a | |
parent | f46b7823e6c64c1e0b6dd67643a45c71f5900b05 (diff) | |
download | cpython-9b88fdf4f09b77d866a3591c9e64b4e8a5b25920.zip cpython-9b88fdf4f09b77d866a3591c9e64b4e8a5b25920.tar.gz cpython-9b88fdf4f09b77d866a3591c9e64b4e8a5b25920.tar.bz2 |
Fixed issue #28633: segfault when concatenating bytes literal and f-string.
-rw-r--r-- | Lib/test/test_fstring.py | 7 | ||||
-rw-r--r-- | Python/ast.c | 9 |
2 files changed, 12 insertions, 4 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 086bf67..8205083 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -92,6 +92,13 @@ f'{a * x()}'""" exec(c) self.assertEqual(x[0], 'foo3') + def test_compile_time_concat_errors(self): + self.assertAllRaise(SyntaxError, + 'cannot mix bytes and nonbytes literals', + [r"""f'' b''""", + r"""b'' f''""", + ]) + def test_literal(self): self.assertEqual(f'', '') self.assertEqual(f'a', 'a') diff --git a/Python/ast.c b/Python/ast.c index 91e7d01..fcd1563 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -5147,7 +5147,8 @@ parsestrplus(struct compiling *c, const node *n) /* Check that we're not mixing bytes with unicode. */ if (i != 0 && bytesmode != this_bytesmode) { ast_error(c, n, "cannot mix bytes and nonbytes literals"); - Py_DECREF(s); + /* s is NULL if the current string part is an f-string. */ + Py_XDECREF(s); goto error; } bytesmode = this_bytesmode; @@ -5161,11 +5162,12 @@ parsestrplus(struct compiling *c, const node *n) if (result < 0) goto error; } else { + /* A string or byte string. */ + assert(s != NULL && fstr == NULL); + assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s)); - /* A string or byte string. */ - assert(s != NULL && fstr == NULL); if (bytesmode) { /* For bytes, concat as we go. */ if (i == 0) { @@ -5177,7 +5179,6 @@ parsestrplus(struct compiling *c, const node *n) goto error; } } else { - assert(s != NULL && fstr == NULL); /* This is a regular string. Concatenate it. */ if (FstringParser_ConcatAndDel(&state, s) < 0) goto error; |