mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 03:50:16 +00:00
Assert takes optional message
This commit is contained in:
parent
817f7c3583
commit
2563f64670
@ -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"))){
|
||||
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
4
src/vm.h
4
src/vm.h
@ -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:
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user