@@ -867,26 +867,33 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *st
867867 {
868868 case UnaryOp_kind :
869869 {
870- if (node -> v .UnaryOp .op == USub &&
870+ if (( node -> v .UnaryOp .op == USub || node -> v . UnaryOp . op == UAdd ) &&
871871 node -> v .UnaryOp .operand -> kind == Constant_kind )
872872 {
873873 PyObject * operand = node -> v .UnaryOp .operand -> v .Constant .value ;
874- PyObject * folded = PyNumber_Negative (operand );
874+ PyObject * folded = node -> v .UnaryOp .op == USub ? PyNumber_Negative (operand ) : PyNumber_Positive (operand );
875+ if (folded == NULL ) {
876+ return 0 ;
877+ }
875878 return make_const (node , folded , ctx_ );
876879 }
877880 break ;
878881 }
879882 case BinOp_kind :
880883 {
881884 operator_ty op = node -> v .BinOp .op ;
882- if ((op == Add || op == Sub ) &&
883- node -> v .BinOp .right -> kind == Constant_kind )
885+ if (op == Add || op == Sub )
884886 {
885887 CALL (fold_const_match_patterns , expr_ty , node -> v .BinOp .left );
886- if (node -> v .BinOp .left -> kind == Constant_kind ) {
888+ CALL (fold_const_match_patterns , expr_ty , node -> v .BinOp .right );
889+ if (node -> v .BinOp .left -> kind == Constant_kind &&
890+ node -> v .BinOp .right -> kind == Constant_kind ) {
887891 PyObject * left = node -> v .BinOp .left -> v .Constant .value ;
888892 PyObject * right = node -> v .BinOp .right -> v .Constant .value ;
889893 PyObject * folded = op == Add ? PyNumber_Add (left , right ) : PyNumber_Subtract (left , right );
894+ if (folded == NULL ) {
895+ return 0 ;
896+ }
890897 return make_const (node , folded , ctx_ );
891898 }
892899 }
0 commit comments