Kaynağa Gözat

Assert takes optional message

aps 3 yıl önce
ebeveyn
işleme
2563f64670
3 değiştirilmiş dosya ile 7 ekleme ve 2 silme
  1. 2 0
      src/compiler.h
  2. 2 1
      src/error.h
  3. 3 1
      src/vm.h

+ 2 - 0
src/compiler.h

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

+ 2 - 1
src/error.h

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

+ 3 - 1
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:
                 {