Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_tstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,14 @@ def test_triple_quoted(self):
)
self.assertEqual(fstring(t), "\n Hello,\n Python\n ")

def test_not_equal_with_format_spec(self):
# gh-146448: != in expression with format spec should not be
# confused with the ! conversion specifier
t = t"{0!=0:}"
self.assertTStringEqual(t, ("", ""), [(False, "0!=0")])

t = t"{0!=0:s}"
self.assertTStringEqual(t, ("", ""), [(False, "0!=0", None, "s")])

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix template string parser incorrectly truncating the ``Interpolation.str``
attribute when the expression contains ``!=`` followed by a format spec.
13 changes: 11 additions & 2 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,17 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
goto again; /* Read next line */
}

/* Punctuation character */
int is_punctuation = (c == ':' || c == '}' || c == '!' || c == '{');
/* Punctuation character.
* '!' is only treated as f-string/t-string punctuation (conversion
* specifier) when not followed by '=' (which would make it '!='). */
int is_punctuation = (c == ':' || c == '}' || c == '{');
if (c == '!') {
int ahead = tok_nextc(tok);
tok_backup(tok, ahead);
if (ahead != '=') {
is_punctuation = 1;
}
}
if (is_punctuation && INSIDE_FSTRING(tok) && INSIDE_FSTRING_EXPR(current_tok)) {
/* This code block gets executed before the curly_bracket_depth is incremented
* by the `{` case, so for ensuring that we are on the 0th level, we need
Expand Down
Loading