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
20 changes: 20 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,26 @@ def test_ifexp_body_stmt_else_stmt(self):
]:
self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg)

def test_double_ampersand(self):
self._check_error(
"a && b",
"Maybe you meant 'and' or '&' instead of '&&'",
lineno=1,
end_lineno=1,
offset=3,
end_offset=5,
)

def test_double_pipe(self):
self._check_error(
"a || b",
"Maybe you meant 'or' or '|' instead of '||'",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also assert the error location, it should be the second | or & char.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review, i've updated with error location, please let me know if we need any further improvements

lineno=1,
end_lineno=1,
offset=3,
end_offset=5,
)


class LazyImportRestrictionTestCase(SyntaxErrorTestCase):
"""Test syntax restrictions for lazy imports."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve SyntaxError message for ``&&`` and ``||`` operators, suggesting ``and``/``&`` and ``or``/``|`` respectively.
10 changes: 10 additions & 0 deletions Parser/pegen_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@
RAISE_INDENTATION_ERROR(last_token->type == INDENT ? "unexpected indent" : "unexpected unindent");
return;
}
if (last_token->type == AMPER && p->tokens[p->fill - 2]->type == AMPER) {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token,
"invalid syntax. Maybe you meant 'and' or '&' instead of '&&'");
return;
}
if (last_token->type == VBAR && p->tokens[p->fill - 2]->type == VBAR) {

Check failure

Code scanning / ClusterFuzzLite/CIFuzz

A read or write past the end of a heap buffer. Error

Heap-buffer-overflow
READ 8

Check failure

Code scanning / ClusterFuzzLite/CIFuzz

Don't crash Error

Null-dereference READ
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token,
"invalid syntax. Maybe you meant 'or' or '|' instead of '||'");
return;
}
// Unknown error (generic case)

// Use the last token we found on the first pass to avoid reporting
Expand Down
Loading