8000 bpo-30682: Removed a too-strict assertion that failed for certain f-s… · python/cpython@11e97f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11e97f2

Browse files
ericvsmithserhiy-storchaka
authored andcommitted
bpo-30682: Removed a too-strict assertion that failed for certain f-strings. (#2232)
This caused a segfault on eval("f'\\\n'") and eval("f'\\\r'") in debug build.
1 parent a49c935 commit 11e97f2

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

Lib/test/test_fstring.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,5 +780,11 @@ def test_dict(self):
780780
self.assertEqual(f'{d["foo"]}', 'bar')
781781
self.assertEqual(f"{d['foo']}", 'bar')
782782

783+
def test_backslash_char(self):
784+
# Check eval of a backslash followed by a control char.
785+
# See bpo-30682: this used to raise an assert in pydebug mode.
786+
self.assertEqual(eval('f"\\\n"'), '')
787+
self.assertEqual(eval('f"\\\r"'), '')
788+
783789
if __name__ == '__main__':
784790
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-30682: Removed a too-strict assertion that failed for certain f-strings,
14+
such as eval("f'\\\n'") and eval("f'\\\r'").
15+
1316
- bpo-30501: The compiler now produces more optimal code for complex condition
1417
expressions in the "if", "while" and "assert" statement, the "if" expression,
1518
and generator expressions and comprehensions.

Python/ast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4914,6 +4914,8 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
49144914
/* Do nothing. Just leave last_str alone (and possibly
49154915
NULL). */
49164916
} else if (!state->last_str) {
4917+
/* Note that the literal can be zero length, if the
4918+
input string is "\\\n" or "\\\r", among others. */
49174919
state->last_str = literal;
49184920
literal = NULL;
49194921
} else {
@@ -4923,8 +4925,6 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
49234925
return -1;
49244926
literal = NULL;
49254927
}
4926-
assert(!state->last_str ||
4927-
PyUnicode_GET_LENGTH(state->last_str) != 0);
49284928

49294929
/* We've dealt with the literal now. It can't be leaked on further
49304930
errors. */

0 commit comments

Comments
 (0)
0