Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/capi/typeobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ static PyObject* half_richcompare(PyObject* self, PyObject* other, int op) noexc
return Py_NotImplemented;
}

static PyObject* slot_tp_iter(PyObject* self) noexcept {
PyObject* slot_tp_iter(PyObject* self) noexcept {
STAT_TIMER(t0, "us_timer_slot_tpiter", SLOT_AVOIDABILITY(self));

PyObject* func, *res;
Expand Down
1 change: 1 addition & 0 deletions src/capi/typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PyObject* mro_external(PyObject* self) noexcept;
int type_set_bases(PyTypeObject* type, PyObject* value, void* context) noexcept;

PyObject* slot_tp_richcompare(PyObject* self, PyObject* other, int op) noexcept;
PyObject* slot_tp_iter(PyObject* self) noexcept;
PyObject* slot_tp_iternext(PyObject* self) noexcept;
PyObject* slot_tp_new(PyTypeObject* self, PyObject* args, PyObject* kwds) noexcept;
PyObject* slot_mp_subscript(PyObject* self, PyObject* arg1) noexcept;
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/builtin_modules/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ class BoxedEnumerate : public Box {
return new BoxedEnumerate(range.begin(), range.end(), idx);
}

static Box* iter(Box* _self) {
static Box* iter(Box* _self) noexcept {
assert(_self->cls == enumerate_cls);
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
return self;
Expand Down Expand Up @@ -1478,6 +1478,7 @@ void setupBuiltins() {
enumerate_cls->giveAttr("__hasnext__",
new BoxedFunction(boxRTFunction((void*)BoxedEnumerate::hasnext, BOXED_BOOL, 1)));
enumerate_cls->freeze();
enumerate_cls->tp_iter = BoxedEnumerate::iter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sharing the code between the capi (tp_iter) and c++ (__iter__ attribute) versions is fine since none of these functions should throw any exceptions, but maybe we could add noexcept to these functions?

builtins_module->giveAttr("enumerate", enumerate_cls);


Expand Down
6 changes: 6 additions & 0 deletions src/runtime/dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ extern "C" int PyDict_Contains(PyObject* op, PyObject* key) noexcept {

try {
if (op->cls == attrwrapper_cls) {
if (key->cls == str_cls) {
BoxedString* key_str = (BoxedString*)key;
internStringMortalInplace(key_str);
return unwrapAttrWrapper(op)->hasattr(key_str);
}

Box* rtn = PyObject_CallMethod(op, "__contains__", "O", key);
if (!rtn)
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/runtime/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,5 +455,6 @@ void setupGenerator() {
generator_cls->giveAttr("__name__", new (pyston_getset_cls) BoxedGetsetDescriptor(generatorName, NULL, NULL));

generator_cls->freeze();
generator_cls->tp_iter = PyObject_SelfIter;
}
}
2 changes: 1 addition & 1 deletion src/runtime/inline/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Box* listIterIter(Box* s) {
return s;
}

Box* listIter(Box* s) {
Box* listIter(Box* s) noexcept {
assert(isSubclass(s->cls, list_cls));
BoxedList* self = static_cast<BoxedList*>(s);
return new BoxedListIterator(self, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/inline/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Box* tupleIterIter(Box* s) {
return s;
}

Box* tupleIter(Box* s) {
Box* tupleIter(Box* s) noexcept {
assert(isSubclass(s->cls, tuple_cls));
BoxedTuple* self = static_cast<BoxedTuple*>(s);
return new BoxedTupleIterator(self);
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/inline/xrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Box* xrangeIterIter(Box* self) {
return self;
}

Box* xrangeIter(Box* self) {
Box* xrangeIter(Box* self) noexcept {
assert(self->cls == xrange_cls);

Box* rtn = new BoxedXrangeIterator(static_cast<BoxedXrange*>(self), false);
Expand Down Expand Up @@ -224,6 +224,8 @@ void setupXrange() {
xrange_cls->giveAttr("__iterator_cls__", xrange_iterator_cls);

xrange_cls->freeze();
xrange_cls->tp_iter = xrangeIter;

xrange_iterator_cls->freeze();
xrange_iterator_cls->tpp_hasnext = BoxedXrangeIterator::xrangeIteratorHasnextUnboxed;
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,7 @@ void setupList() {

list_cls->tp_as_sequence->sq_slice = list_slice;
list_cls->tp_as_sequence->sq_length = list_length;
list_cls->tp_iter = listIter;

CLFunction* hasnext = boxRTFunction((void*)listiterHasnextUnboxed, BOOL, 1);
addRTFunction(hasnext, (void*)listiterHasnext, BOXED_BOOL);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BoxedListIterator : public Box {
DEFAULT_CLASS(list_iterator_cls);
};

Box* listIter(Box* self);
Box* listIter(Box* self) noexcept;
Box* listIterIter(Box* self);
Box* listiterHasnext(Box* self);
i1 listiterHasnextUnboxed(Box* self);
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/objmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4911,8 +4911,14 @@ extern "C" Box* getiterHelper(Box* o) {

Box* getiter(Box* o) {
// TODO add rewriting to this? probably want to try to avoid this path though
static BoxedString* iter_str = internStringImmortal("__iter__");
Box* r = callattrInternal0(o, iter_str, LookupScope::CLASS_ONLY, NULL, ArgPassSpec(0));
BoxedClass* type = o->cls;
Box* r = NULL;
if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_ITER) && type->tp_iter != slot_tp_iter && type->tp_iter) {
r = type->tp_iter(o);
} else {
static BoxedString* iter_str = internStringImmortal("__iter__");
r = callattrInternal0(o, iter_str, LookupScope::CLASS_ONLY, NULL, ArgPassSpec(0));
}
if (r) {
if (!PyIter_Check(r)) {
raiseExcHelper(TypeError, "iter() returned non-iterator of type '%s'", r->cls->tp_name);
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Box* setXorSet(BoxedSet* lhs, BoxedSet* rhs) {
return rtn;
}

Box* setIter(BoxedSet* self) {
Box* setIter(BoxedSet* self) noexcept {
RELEASE_ASSERT(PyAnySet_Check(self), "");
return new BoxedSetIterator(self);
}
Expand Down Expand Up @@ -583,6 +583,9 @@ void setupSet() {

set_cls->freeze();
frozenset_cls->freeze();

set_cls->tp_iter = (decltype(set_cls->tp_iter))setIter;
frozenset_cls->tp_iter = (decltype(frozenset_cls->tp_iter))setIter;
}

void teardownSet() {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,7 +2311,7 @@ extern "C" void strIteratorGCHandler(GCVisitor* v, Box* b) {
v->visit(it->s);
}

Box* strIter(BoxedString* self) {
Box* strIter(BoxedString* self) noexcept {
assert(PyString_Check(self));
return new BoxedStringIterator(self);
}
Expand Down Expand Up @@ -2791,6 +2791,7 @@ void setupStr() {

str_cls->tp_as_sequence->sq_slice = str_slice;
str_cls->tp_as_sequence->sq_length = str_length;
str_cls->tp_iter = (decltype(str_cls->tp_iter))strIter;

basestring_cls->giveAttr("__doc__",
boxString("Type basestring cannot be instantiated; it is the base for str and unicode."));
Expand Down
1 change: 1 addition & 0 deletions src/runtime/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ void setupTuple() {

tuple_cls->tp_as_sequence->sq_item = (ssizeargfunc)tupleitem;
tuple_cls->tp_as_sequence->sq_length = (lenfunc)tuplelength;
tuple_cls->tp_iter = tupleIter;

CLFunction* hasnext = boxRTFunction((void*)tupleiterHasnextUnboxed, BOOL, 1);
addRTFunction(hasnext, (void*)tupleiterHasnext, BOXED_BOOL);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BoxedTupleIterator : public Box {
DEFAULT_CLASS(tuple_iterator_cls);
};

Box* tupleIter(Box* self);
Box* tupleIter(Box* self) noexcept;
Box* tupleIterIter(Box* self);
Box* tupleiterHasnext(Box* self);
i1 tupleiterHasnextUnboxed(Box* self);
Expand Down