Merge pull request #12 from apsz3/main

Assert with message
This commit is contained in:
BLUELOVETH 2023-02-11 20:03:49 +08:00 committed by GitHub
commit 794d0785a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View File

@ -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"))){

View File

@ -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();
}
};
}
}

View File

@ -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:
{

View File

@ -41,4 +41,11 @@ def f1():
try:
f1()
except KeyError:
print("PASS 04")
print("PASS 04")
assert True, "Msg"
try:
assert False, "Msg"
except AssertionError:
print("PASS 05")