Commit 5e724cf1 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add a seqiter_cls gc handler, and a test for these

parent bd0b336a
......@@ -58,6 +58,16 @@ Box* seqiterNext(Box* s) {
return r;
}
static void seqiterGCVisit(GCVisitor* v, Box* b) {
assert(b->cls == seqiter_cls);
boxGCHandler(v, b);
BoxedSeqIter* si = static_cast<BoxedSeqIter*>(b);
v->visit(si->b);
if (si->next)
v->visit(si->next);
}
static void iterwrapperGCVisit(GCVisitor* v, Box* b) {
assert(b->cls == iterwrapper_cls);
boxGCHandler(v, b);
......@@ -108,7 +118,7 @@ extern "C" PyObject* PySeqIter_New(PyObject* seq) noexcept {
}
void setupIter() {
seqiter_cls = new BoxedHeapClass(object_cls, NULL, 0, sizeof(BoxedSeqIter), false, "iterator");
seqiter_cls = new BoxedHeapClass(object_cls, seqiterGCVisit, 0, sizeof(BoxedSeqIter), false, "iterator");
seqiter_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)seqiterNext, UNKNOWN, 1)));
seqiter_cls->giveAttr("__hasnext__", new BoxedFunction(boxRTFunction((void*)seqiterHasnext, BOXED_BOOL, 1)));
......
# Regression test: make sure we GC special iterator objects correctly
import gc
class C(object):
def f(self, i):
return i * i
def __getitem__(self, i):
if i < 1000:
return self.f(i)
raise IndexError(i)
for i in C():
gc.collect()
print i
class C2(object):
def __init__(self):
self.n = 0
def __iter__(self):
return self
def f(self):
self.n += 1
return self.n * 2
def next(self):
if self.n < 1000:
return self.f()
raise StopIteration()
for i in C2():
gc.collect()
print i
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment