-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-30529: Fix assert failure in with pydebug; add tests. #2232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -780,5 +780,19 @@ def test_dict(self): | |
self.assertEqual(f'{d["foo"]}', 'bar') | ||
self.assertEqual(f"{d['foo']}", 'bar') | ||
|
||
def test_backslash_char(self): | ||
# Check eval of a backslash followed by a control char. | ||
# As part of bpo-30529, this used to raise an assert in pydebug mode. | ||
for i in range(1, 32): | ||
with self.subTest(i=i): | ||
# This will result in a backslash followed by the control | ||
# char, except for \n and \r which give a zero length | ||
# string. The same thing happens with non-fstrings. | ||
expected_len = 0 if i in (10, 13) else 2 | ||
s = f'f"\\{chr(i)}"' | ||
self.assertEqual(len(eval(s)), expected_len) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can test not just length, but a value. This could produce more informative error message if the eval() returns unexpected result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll just test the zero length versions. |
||
s = f'"\\{chr(i)}"' | ||
self.assertEqual(len(eval(s)), expected_len) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4914,6 +4914,8 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, | |
/* Do nothing. Just leave last_str alone (and possibly | ||
NULL). */ | ||
} else if (!state->last_str) { | ||
/* Note that the literal can be zero length, if the | ||
Input string is "\\\n" or "\\\r". */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or The word "Input" shouldn't start from a capital letter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix it. |
||
state->last_str = literal; | ||
literal = NULL; | ||
} else { | ||
|
@@ -4923,8 +4925,6 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, | |
return -1; | ||
literal = NULL; | ||
} | ||
assert(!state->last_str || | ||
PyUnicode_GET_LENGTH(state->last_str) != 0); | ||
|
||
/* We've dealt with the literal now. It can't be leaked on further | ||
errors. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A backslash followed by the control character (except '\r' and '\n') produces a DeprecationWarning.
I would test just a case of
'\\\n'
. Maybe also'\\\r'
and'\\\r\n'
if you prefer.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I think I'll just test
'\\\r'
and'\\\n'
.