Parses an `except` clause of a `try` statement. # Panics If the parser isn't positioned at an `except` token.
(&mut self)
| 1692 | /// |
| 1693 | /// If the parser isn't positioned at an `except` token. |
| 1694 | fn parse_except_clause(&mut self) -> (ExceptHandler, ExceptClauseKind) { |
| 1695 | let start = self.node_start(); |
| 1696 | self.bump(TokenKind::Except); |
| 1697 | |
| 1698 | let star_token_range = self.current_token_range(); |
| 1699 | let block_kind = if self.eat(TokenKind::Star) { |
| 1700 | ExceptClauseKind::Star(star_token_range) |
| 1701 | } else { |
| 1702 | ExceptClauseKind::Normal |
| 1703 | }; |
| 1704 | |
| 1705 | let type_ = if self.at_expr() { |
| 1706 | // test_err except_stmt_invalid_expression |
| 1707 | // try: |
| 1708 | // pass |
| 1709 | // except yield x: |
| 1710 | // pass |
| 1711 | // try: |
| 1712 | // pass |
| 1713 | // except* *x: |
| 1714 | // pass |
| 1715 | let parsed_expr = self.parse_expression_list(ExpressionContext::default()); |
| 1716 | if matches!( |
| 1717 | parsed_expr.expr, |
| 1718 | Expr::Tuple(ast::ExprTuple { |
| 1719 | parenthesized: false, |
| 1720 | .. |
| 1721 | }) |
| 1722 | ) { |
| 1723 | if self.at(TokenKind::As) { |
| 1724 | // test_err except_stmt_unparenthesized_tuple_as |
| 1725 | // try: |
| 1726 | // pass |
| 1727 | // except x, y as exc: |
| 1728 | // pass |
| 1729 | // try: |
| 1730 | // pass |
| 1731 | // except* x, y as eg: |
| 1732 | // pass |
| 1733 | self.add_error( |
| 1734 | ParseErrorType::OtherError( |
| 1735 | "Multiple exception types must be parenthesized when using `as`" |
| 1736 | .to_string(), |
| 1737 | ), |
| 1738 | &parsed_expr, |
| 1739 | ); |
| 1740 | } else { |
| 1741 | // test_err except_stmt_unparenthesized_tuple_no_as_py313 |
| 1742 | // # parse_options: {"target-version": "3.13"} |
| 1743 | // try: |
| 1744 | // pass |
| 1745 | // except x, y: |
| 1746 | // pass |
| 1747 | // try: |
| 1748 | // pass |
| 1749 | // except* x, y: |
| 1750 | // pass |
| 1751 |
no test coverage detected