Skip to content

Commit 2eca5b4

Browse files
serhiy-storchakaericvsmith
authored andcommitted
[3.6] bpo-30682: Removed a too-strict assertion that failed for certain f-strings. (GH-2232) (#2242)
This caused a segfault on eval("f'\\\n'") and eval("f'\\\r'") in debug build.. (cherry picked from commit 11e97f2)
1 parent a0ccc54 commit 2eca5b4

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.6.2 release candidate 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-30604: Move co_extra_freefuncs to not be per-thread to avoid crashes
1417

1518
- bpo-29104: Fixed parsing backslashes in f-strings.

‎Python/ast.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4887,6 +4887,8 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
48874887
/* Do nothing. Just leave last_str alone (and possibly
48884888
NULL). */
48894889
} else if (!state->last_str) {
4890+
/* Note that the literal can be zero length, if the
4891+
input string is "\\\n" or "\\\r", among others. */
48904892
state->last_str = literal;
48914893
literal = NULL;
48924894
} else {
@@ -4896,8 +4898,6 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
48964898
return -1;
48974899
literal = NULL;
48984900
}
4899-
assert(!state->last_str ||
4900-
PyUnicode_GET_LENGTH(state->last_str) != 0);
49014901

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

0 commit comments

Comments
 (0)