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
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ symtable
(Contributed by Serhiy Storchaka in :gh:`153844`.)


tokenize
--------

* Invalid token ``<>`` excluded from :mod:`tokenize` output.
(Contributed by Sergey B Kirpichev in :gh:`151464`.)


tkinter
-------

Expand Down
8 changes: 8 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ noteq_bitwise_or[CmpopExprPair*]:
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
| invalid_noteq
gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
Expand Down Expand Up @@ -1659,3 +1660,10 @@ invalid_bitwise_or:
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?")
: NULL
}

invalid_noteq:
| a='<' b='>' {
_PyPegen_tokens_are_adjacent(a, b)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?")
: NULL
}
2 changes: 1 addition & 1 deletion Include/internal/pycore_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ extern "C" {
// Export these 4 symbols for 'test_peg_generator'
PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */
PyAPI_FUNC(int) _PyToken_OneChar(int);
PyAPI_FUNC(int) _PyToken_TwoChars(int, int);
PyAPI_FUNC(int) _PyToken_TwoChars(int, int, int);
PyAPI_FUNC(int) _PyToken_ThreeChars(int, int, int);

#ifdef __cplusplus
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_flufl.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,5 @@ def test_barry_as_bdfl_relative_import(self):
self.assertEqual(cm.exception.offset, len(code) - 4)




if __name__ == '__main__':
unittest.main()
25 changes: 25 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,31 @@ def test_ifexp_body_stmt_else_stmt(self):
]:
self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg)

def test_diamond_operator(self):
self._check_error(
"1<>2",
r"Maybe you meant '!=' instead of '<>'\?",
lineno=1,
end_lineno=1,
offset=2,
end_offset=4,
)

def test_diamond_operator_barry_as_flufl(self):
# Under barry_as_FLUFL, '<>' is the valid "not equal" operator
compile(
"from __future__ import barry_as_FLUFL\n1<>2",
"<test>", "exec",
)
self._check_error(
"from __future__ import barry_as_FLUFL\na != b",
"with Barry as BDFL, use '<>' instead of '!='",
lineno=2,
end_lineno=2,
offset=3,
end_offset=5,
)

def test_double_ampersand(self):
self._check_error(
"a && b",
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,20 @@ def test_multiline_non_ascii_fstring_with_expr(self):
FSTRING_END \'"\' (2, 2) (2, 3)
""")

def test_ineq_tokens(self):
self.check_tokenize("1 != 2", """\
NUMBER '1' (1, 0) (1, 1)
OP '!=' (1, 2) (1, 4)
NUMBER '2' (1, 5) (1, 6)
""")
self.check_tokenize("1 <> 2", """\
NUMBER '1' (1, 0) (1, 1)
OP '<' (1, 2) (1, 3)
OP '>' (1, 3) (1, 4)
NUMBER '2' (1, 5) (1, 6)
""")


class GenerateTokensTest(TokenizeTest):
def check_tokenize(self, s, expected):
# Format the tokens in s in a table format.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Exclude invalid token ``<>`` from :mod:`tokenize` output. :exc:`SyntaxError`
for ``<>`` now suggests ``!=``.
1 change: 1 addition & 0 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,7 @@ _PyPegen_checked_from_import(Parser *p, asdl_seq *dots, expr_ty module_name,
alias_ty alias = asdl_seq_GET(names, i);
if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) {
p->flags |= PyPARSE_BARRY_AS_BDFL;
p->tok->barry_as_bdfl = 1;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
/* Check for two-character token */
{
int c2 = tok_nextc(tok);
int current_token = _PyToken_TwoChars(c, c2);
int current_token = _PyToken_TwoChars(c, c2, tok->barry_as_bdfl);
if (current_token != OP) {
int c3 = tok_nextc(tok);
int current_token3 = _PyToken_ThreeChars(c, c2, c3);
Expand Down
1 change: 1 addition & 0 deletions Parser/lexer/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ _PyTokenizer_tok_new(void)
#ifdef Py_DEBUG
tok->debug = _Py_GetConfig()->parser_debug;
#endif
tok->barry_as_bdfl = 0;
return tok;
}

Expand Down
1 change: 1 addition & 0 deletions Parser/lexer/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct tok_state {
#ifdef Py_DEBUG
int debug;
#endif
int barry_as_bdfl;
};

int _PyLexer_type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset,
Expand Down
Loading
Loading