diff --git a/src/compiler.h b/src/compiler.h index d786e5b9..6b7904d5 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -871,6 +871,8 @@ __LISTCOMP: compile_try_except(); }else if(match(TK("assert"))){ EXPR(); + if (match(TK(","))) EXPR(); + else emit(OP_LOAD_CONST, co()->add_const(vm->PyStr(""))); emit(OP_ASSERT); consume_end_stmt(); } else if(match(TK("with"))){ diff --git a/src/error.h b/src/error.h index 2e55d385..9fc76949 100644 --- a/src/error.h +++ b/src/error.h @@ -86,8 +86,9 @@ public: StrStream ss; if(is_re) ss << "Traceback (most recent call last):\n"; while(!st.empty()) { ss << st.top() << '\n'; st.pop(); } - ss << type << ": " << msg; + if (!msg.empty()) ss << type << ": " << msg; + else ss << type; return ss.str(); } }; -} \ No newline at end of file +} diff --git a/src/vm.h b/src/vm.h index 4ca0e839..4bca3d73 100644 --- a/src/vm.h +++ b/src/vm.h @@ -182,8 +182,10 @@ class VM { case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break; case OP_ASSERT: { + PyVar _msg = frame->pop_value(this); + Str msg = PyStr_AS_C(asStr(_msg)); PyVar expr = frame->pop_value(this); - if(asBool(expr) != True) _error("AssertionError", ""); + if(asBool(expr) != True) _error("AssertionError", msg); } break; case OP_EXCEPTION_MATCH: { diff --git a/tests/_exception.py b/tests/_exception.py index 84423b10..4e77ac35 100644 --- a/tests/_exception.py +++ b/tests/_exception.py @@ -41,4 +41,11 @@ def f1(): try: f1() except KeyError: - print("PASS 04") \ No newline at end of file + print("PASS 04") + + +assert True, "Msg" +try: + assert False, "Msg" +except AssertionError: + print("PASS 05")