Ver Fonte

fix a bug

blueloveTH há 1 ano atrás
pai
commit
0109829ad4
3 ficheiros alterados com 13 adições e 1 exclusões
  1. 3 0
      src/interpreter/ceval.c
  2. 4 1
      src/public/py_exception.c
  3. 6 0
      tests/95_bugs.py

+ 3 - 0
src/interpreter/ceval.c

@@ -242,6 +242,7 @@ FrameResult VM__run_top_frame(VM* self) {
                 DISPATCH();
             }
             case OP_LOAD_CLASS_GLOBAL: {
+                assert(self->__curr_class);
                 py_Name name = byte.arg;
                 py_Ref tmp = py_getdict(self->__curr_class, name);
                 if(tmp) {
@@ -902,6 +903,7 @@ FrameResult VM__run_top_frame(VM* self) {
                 DISPATCH();
             }
             case OP_STORE_CLASS_ATTR: {
+                assert(self->__curr_class);
                 py_Name name = byte.arg;
                 if(py_istype(TOP(), tp_function)) {
                     Function* ud = py_touserdata(TOP());
@@ -912,6 +914,7 @@ FrameResult VM__run_top_frame(VM* self) {
                 DISPATCH();
             }
             case OP_ADD_CLASS_ANNOTATION: {
+                assert(self->__curr_class);
                 // [type_hint string]
                 py_Type type = py_totype(self->__curr_class);
                 py_TypeInfo* ti = TypeList__get(&self->types, type);

+ 4 - 1
src/public/py_exception.c

@@ -137,7 +137,10 @@ void py_clearexc(py_StackRef p0) {
     vm->last_retval = *py_NIL;
     vm->curr_exception = *py_NIL;
     vm->is_curr_exc_handled = false;
-    vm->__curr_class = NULL;
+
+    /* Don't clear this, because StopIteration() may corrupt the class defination */
+    // vm->__curr_class = NULL;
+
     vm->__curr_function = NULL;
     if(p0) vm->stack.sp = p0;
 }

+ 6 - 0
tests/95_bugs.py

@@ -137,3 +137,9 @@ assert (g == 1), g
 
 assert list.__new__(list) == []
 assert a.__new__ == list.__new__
+
+
+class A:
+    x: list[int] = [i for i in range(1, 4)]
+
+assert A.x == [1, 2, 3]