Assert takes optional message

This commit is contained in:
aps 2023-02-10 22:34:44 -05:00
parent 817f7c3583
commit 2563f64670
3 changed files with 7 additions and 2 deletions

View File

@ -887,6 +887,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"))){

View File

@ -86,7 +86,8 @@ 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.compare("") != 0) ss << type << ": " << msg;
else ss << type;
return ss.str();
}
};

View File

@ -186,8 +186,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:
{