bpo-30682: Removed a too-strict assertion that failed for certain f-s… · python/cpython@11e97f2 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -780,5 +780,11 @@ def test_dict(self):
780 780 self.assertEqual(f'{d["foo"]}', 'bar')
781 781 self.assertEqual(f"{d['foo']}", 'bar')
782 782
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 +
783 789 if __name__ == '__main__':
784 790 unittest.main()
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
10 10 Core and Builtins
11 11 -----------------
12 12
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 +
13 16 - bpo-30501: The compiler now produces more optimal code for complex condition
14 17 expressions in the "if", "while" and "assert" statement, the "if" expression,
15 18 and generator expressions and comprehensions.
Original file line number Diff line number Diff line change
@@ -4914,6 +4914,8 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
4914 4914 /* Do nothing. Just leave last_str alone (and possibly
4915 4915 NULL). */
4916 4916 } 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. */
4917 4919 state->last_str = literal;
4918 4920 literal = NULL;
4919 4921 } else {
@@ -4923,8 +4925,6 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
4923 4925 return -1;
4924 4926 literal = NULL;
4925 4927 }
4926 -assert(!state->last_str |
4927 -PyUnicode_GET_LENGTH(state->last_str) != 0);
4928 4928
4929 4929 /* We've dealt with the literal now. It can't be leaked on further
4930 4930 errors. */