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(); compile_try_except();
}else if(match(TK("assert"))){ }else if(match(TK("assert"))){
EXPR(); EXPR();
if (match(TK(","))) EXPR();
else emit(OP_LOAD_CONST, co()->add_const(vm->PyStr("")));
emit(OP_ASSERT); emit(OP_ASSERT);
consume_end_stmt(); consume_end_stmt();
} else if(match(TK("with"))){ } else if(match(TK("with"))){

View File

@ -86,7 +86,8 @@ public:
StrStream ss; StrStream ss;
if(is_re) ss << "Traceback (most recent call last):\n"; if(is_re) ss << "Traceback (most recent call last):\n";
while(!st.empty()) { ss << st.top() << '\n'; st.pop(); } 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(); return ss.str();
} }
}; };

View File

@ -186,8 +186,10 @@ class VM {
case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break; case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
case OP_ASSERT: case OP_ASSERT:
{ {
PyVar _msg = frame->pop_value(this);
Str msg = PyStr_AS_C(asStr(_msg));
PyVar expr = frame->pop_value(this); PyVar expr = frame->pop_value(this);
if(asBool(expr) != True) _error("AssertionError", ""); if(asBool(expr) != True) _error("AssertionError", msg);
} break; } break;
case OP_EXCEPTION_MATCH: case OP_EXCEPTION_MATCH:
{ {