Commit 0ccfdcb8 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Clean up our iternext handling

ie try to avoid api conversion.
parent 89cdcc7e
......@@ -827,7 +827,7 @@ static PyObject* slot_tp_iter(PyObject* self) noexcept {
return PySeqIter_New(self);
}
static PyObject* slot_tp_iternext(PyObject* self) noexcept {
/* Pyston change: static */ PyObject* slot_tp_iternext(PyObject* self) noexcept {
STAT_TIMER(t0, "us_timer_slot_tpiternext", SLOT_AVOIDABILITY(self));
static PyObject* next_str;
......
......@@ -35,6 +35,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_iternext(PyObject* self) noexcept;
}
#endif
......@@ -531,7 +531,6 @@ public:
BoxedString* reprICAsString();
bool nonzeroIC();
Box* hasnextOrNullIC();
Box* nextIC();
friend class AttrWrapper;
};
......
......@@ -633,25 +633,6 @@ extern "C" int PyObject_Print(PyObject* obj, FILE* fp, int flags) noexcept {
return internal_print(obj, fp, flags, 0);
};
extern "C" PyObject* PyIter_Next(PyObject* iter) noexcept {
try {
Box* hasnext = iter->hasnextOrNullIC();
if (hasnext) {
if (hasnext->nonzeroIC())
return iter->nextIC();
else
return NULL;
} else {
return iter->nextIC();
}
} catch (ExcInfo e) {
if (e.matches(StopIteration))
return NULL;
setCAPIException(e);
}
return NULL;
}
extern "C" int PyCallable_Check(PyObject* x) noexcept {
if (x == NULL)
return 0;
......
......@@ -41,24 +41,11 @@ public:
void next() override {
STAT_TIMER(t0, "us_timer_iteratorgeneric_next", 0);
assert(iterator);
Box* hasnext = iterator->hasnextOrNullIC();
if (hasnext) {
if (hasnext->nonzeroIC()) {
value = iterator->nextIC();
} else {
*this = *end();
}
} else {
try {
value = iterator->nextIC();
} catch (ExcInfo e) {
if (e.matches(StopIteration))
*this = *end();
else
throw e;
}
}
Box* next = PyIter_Next(iterator);
if (next)
value = next;
else
*this = *end();
}
Box* getValue() override { return value; }
......
......@@ -132,20 +132,14 @@ bool iterwrapperHasnextUnboxed(Box* s) {
RELEASE_ASSERT(s->cls == iterwrapper_cls, "");
BoxedIterWrapper* self = static_cast<BoxedIterWrapper*>(s);
static BoxedString* next_str = static_cast<BoxedString*>(PyString_InternFromString("next"));
Box* next;
try {
next = callattr(self->iter, next_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = false }),
ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} catch (ExcInfo e) {
if (e.matches(StopIteration)) {
self->next = NULL;
return false;
}
throw e;
}
Box* next = PyIter_Next(self->iter);
self->next = next;
return true;
if (!next) {
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_StopIteration))
throwCAPIException();
PyErr_Clear();
}
return next != NULL;
}
Box* iterwrapperHasnext(Box* s) {
......
......@@ -238,9 +238,38 @@ Box* BoxedClass::callHasnextIC(Box* obj, bool null_on_nonexistent) {
ArgPassSpec(0), nullptr, nullptr, nullptr, nullptr, nullptr);
}
extern "C" PyObject* PyIter_Next(PyObject* iter) noexcept {
if (iter->cls->tp_iternext != slot_tp_iternext) {
PyObject* result;
result = (*iter->cls->tp_iternext)(iter);
if (result == NULL && PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
return result;
}
try {
Box* hasnext = iter->hasnextOrNullIC();
if (hasnext) {
if (hasnext->nonzeroIC())
return iter->cls->callNextIC(iter);
else
return NULL;
} else {
return iter->cls->callNextIC(iter);
}
} catch (ExcInfo e) {
if (!e.matches(StopIteration))
setCAPIException(e);
return NULL;
}
}
Box* BoxedClass::callNextIC(Box* obj) {
assert(obj->cls == this);
// This would work, but it would have been better to just call tp_iternext
assert(this->tp_iternext == slot_tp_iternext);
auto ic = next_ic.get();
if (!ic) {
ic = new CallattrIC();
......@@ -303,11 +332,6 @@ Box* Box::hasnextOrNullIC() {
return this->cls->callHasnextIC(this, true);
}
Box* Box::nextIC() {
return this->cls->callNextIC(this);
}
std::string builtinStr("__builtin__");
extern "C" BoxedFunctionBase::BoxedFunctionBase(CLFunction* f)
......
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