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
6 changes: 3 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -554,20 +554,20 @@ complex_number[expr_ty]:

signed_number[expr_ty]:
| NUMBER
| '+' number=NUMBER { number }
| '+' number=NUMBER { _PyAST_UnaryOp(UAdd, number, EXTRA) }
| '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }

signed_real_number[expr_ty]:
| real_number
| '+' real=real_number { real }
| '+' real=real_number { _PyAST_UnaryOp(UAdd, real, EXTRA) }
| '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }

real_number[expr_ty]:
| real=NUMBER { _PyPegen_ensure_real(p, real) }

imaginary_number[expr_ty]:
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
| '+' imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
| '+' imag=NUMBER { _PyAST_UnaryOp(UAdd, _PyPegen_ensure_imaginary(p, imag), EXTRA) }

capture_pattern[pattern_ty]:
| target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix AST generation asymmetry where unary positive ``+`` was implicitly dropped in pattern match expressions, restoring parsing parity with unary negative ``-``.
42 changes: 39 additions & 3 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
}

static int
ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
ensure_literal_signed(expr_ty exp, bool allow_real, bool allow_imaginary)
{
assert(exp->kind == UnaryOp_kind);
// Must be negation ...
if (exp->v.UnaryOp.op != USub) {
// Must be negation or positive ...
if (exp->v.UnaryOp.op != USub && exp->v.UnaryOp.op != UAdd) {
return 0;
}
// ... of a constant ...
Expand Down Expand Up @@ -461,7 +461,7 @@ ensure_literal_complex(expr_ty exp)
}
break;
case UnaryOp_kind:
if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
if (!ensure_literal_signed(left, /*real=*/true, /*imaginary=*/false)) {
return 0;
}
break;
Expand All @@ -476,6 +476,11 @@ ensure_literal_complex(expr_ty exp)
return 0;
}
break;
case UnaryOp_kind:
if (!ensure_literal_signed(right, /*real=*/false, /*imaginary=*/true)) {
return 0;
}
break;
default:
return 0;
}
Expand Down Expand Up @@ -512,9 +517,9 @@ validate_pattern_match_value(expr_ty exp)
// Constants and attribute lookups are always permitted
return 1;
case UnaryOp_kind:
// Negated numbers are permitted (whether real or imaginary)
// Signed numbers are permitted (whether real or imaginary)
// Compiler will complain if AST folding doesn't create a constant
if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
if (ensure_literal_signed(exp, /*real=*/true, /*imaginary=*/true)) {
return 1;
}
break;
Expand Down
17 changes: 12 additions & 5 deletions Python/ast_preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,26 +867,33 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *st
{
case UnaryOp_kind:
{
if (node->v.UnaryOp.op == USub &&
if ((node->v.UnaryOp.op == USub || node->v.UnaryOp.op == UAdd) &&
node->v.UnaryOp.operand->kind == Constant_kind)
{
PyObject *operand = node->v.UnaryOp.operand->v.Constant.value;
PyObject *folded = PyNumber_Negative(operand);
PyObject *folded = node->v.UnaryOp.op == USub ? PyNumber_Negative(operand) : PyNumber_Positive(operand);
if (folded == NULL) {
return 0;
}
return make_const(node, folded, ctx_);
}
break;
}
case BinOp_kind:
{
operator_ty op = node->v.BinOp.op;
if ((op == Add || op == Sub) &&
node->v.BinOp.right->kind == Constant_kind)
if (op == Add || op == Sub)
{
CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.left);
if (node->v.BinOp.left->kind == Constant_kind) {
CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.right);
if (node->v.BinOp.left->kind == Constant_kind &&
node->v.BinOp.right->kind == Constant_kind) {
PyObject *left = node->v.BinOp.left->v.Constant.value;
PyObject *right = node->v.BinOp.right->v.Constant.value;
PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right);
if (folded == NULL) {
return 0;
}
return make_const(node, folded, ctx_);
}
}
Expand Down
Loading