Skip to content

Commit 4b30c64

Browse files
committed
add __iter__ runtime IC
1 parent 4540202 commit 4b30c64

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

from_cpython/Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ struct _typeobject {
456456

457457
void* _hcls;
458458
void* _hcattrs;
459-
char _ics[32];
459+
char _ics[40];
460460
void* _gcvisit_func;
461461
int _attrs_offset;
462462
bool _flags[7];

src/runtime/list.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,6 @@ void setupList() {
12731273
static PyMappingMethods list_as_mapping;
12741274
list_cls->tp_as_mapping = &list_as_mapping;
12751275

1276-
list_cls->tp_iter = listIter;
12771276
list_iterator_cls = BoxedClass::create(type_cls, object_cls, &BoxedListIterator::gcHandler, 0, 0,
12781277
sizeof(BoxedListIterator), false, "listiterator");
12791278
list_reverse_iterator_cls = BoxedClass::create(type_cls, object_cls, &BoxedListIterator::gcHandler, 0, 0,
@@ -1356,6 +1355,7 @@ void setupList() {
13561355

13571356
list_cls->giveAttr("__hash__", None);
13581357
list_cls->freeze();
1358+
list_cls->tp_iter = listIter;
13591359

13601360
list_cls->tp_as_sequence->sq_length = list_length;
13611361
list_cls->tp_as_sequence->sq_concat = (binaryfunc)list_concat;

src/runtime/objmodel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5711,8 +5711,7 @@ Box* getiter(Box* o) {
57115711
if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_ITER) && type->tp_iter != slot_tp_iter && type->tp_iter) {
57125712
r = type->tp_iter(o);
57135713
} else {
5714-
static BoxedString* iter_str = internStringImmortal("__iter__");
5715-
r = callattrInternal0<CXX, NOT_REWRITABLE>(o, iter_str, LookupScope::CLASS_ONLY, NULL, ArgPassSpec(0));
5714+
r = type->callIterIC(o);
57165715
}
57175716
if (r) {
57185717
if (!PyIter_Check(r)) {

src/runtime/types.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ Box* BoxedClass::callReprIC(Box* obj) {
290290
return ic->call(obj, repr_str, callattr_flags, nullptr, nullptr, nullptr, nullptr, nullptr);
291291
}
292292

293+
Box* BoxedClass::callIterIC(Box* obj) {
294+
assert(obj->cls == this);
295+
296+
auto ic = iter_ic.get();
297+
if (!ic) {
298+
ic = new CallattrIC();
299+
iter_ic.reset(ic);
300+
}
301+
302+
static BoxedString* iter_str = internStringImmortal("__iter__");
303+
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
304+
return ic->call(obj, iter_str, callattr_flags, nullptr, nullptr, nullptr, nullptr, nullptr);
305+
}
306+
293307
bool BoxedClass::callNonzeroIC(Box* obj) {
294308
assert(obj->cls == this);
295309

src/runtime/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ class BoxedClass : public BoxVar {
194194

195195
// TODO: these don't actually get deallocated right now
196196
std::unique_ptr<CallattrCapiIC> next_ic;
197-
std::unique_ptr<CallattrIC> hasnext_ic, repr_ic;
197+
std::unique_ptr<CallattrIC> hasnext_ic, repr_ic, iter_ic;
198198
std::unique_ptr<NonzeroIC> nonzero_ic;
199199
Box* callHasnextIC(Box* obj, bool null_on_nonexistent);
200200
Box* call_nextIC(Box* obj) noexcept;
201201
Box* callReprIC(Box* obj);
202+
Box* callIterIC(Box* obj);
202203
bool callNonzeroIC(Box* obj);
203204

204205
gcvisit_func gc_visit;

0 commit comments

Comments
 (0)