blueloveTH 6 months ago
parent
commit
0ce88fa70c
3 changed files with 15 additions and 2 deletions
  1. 3 1
      src/interpreter/ceval.c
  2. 3 1
      src/interpreter/vm.c
  3. 9 0
      tests/71_gc_bug.py

+ 3 - 1
src/interpreter/ceval.c

@@ -4,6 +4,7 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/common/sstream.h"
 #include "pocketpy/objects/codeobject.h"
+#include "pocketpy/objects/exception.h"
 #include "pocketpy/pocketpy.h"
 #include "pocketpy/objects/error.h"
 #include <stdbool.h>
@@ -820,7 +821,8 @@ __NEXT_STEP:
                 return RES_YIELD;
             } else {
                 assert(self->last_retval.type == tp_StopIteration);
-                py_ObjectRef value = py_getslot(&self->last_retval, 0);
+                BaseException* ud = py_touserdata(py_retval());
+                py_ObjectRef value = &ud->args;
                 if(py_isnil(value)) value = py_None();
                 *TOP() = *value;  // [iter] -> [retval]
                 DISPATCH_JUMP((int16_t)byte.arg);

+ 3 - 1
src/interpreter/vm.c

@@ -635,7 +635,9 @@ void ManagedHeap__mark(ManagedHeap* self) {
     assert(p_stack->length == 0);
 
     // mark value stack
-    for(py_TValue* p = vm->stack.begin; p != vm->stack.end; p++) {
+    for(py_TValue* p = vm->stack.begin; p < vm->stack.sp; p++) {
+        // assert(p->type != tp_nil);
+        if(py_isnil(p)) continue;
         pk__mark_value(p);
     }
     // mark modules

+ 9 - 0
tests/71_gc_bug.py

@@ -0,0 +1,9 @@
+a=[]
+import gc
+gc.collect()
+
+# a.append(a)
+print(list(globals().items()))
+del a
+print(list(globals().items()))
+gc.collect()