Commit b905e1e0 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Make more use of the new slots

parent 23fe2a16
......@@ -249,9 +249,6 @@ private:
InternedString iter_name = nodeName(node, "lc_iter", i);
pushAssign(iter_name, iter_call);
// TODO bad to save these like this?
AST_expr* hasnext_attr = makeLoadAttribute(makeName(iter_name, AST_TYPE::Load, node->lineno),
internString("__hasnext__"), true);
AST_expr* next_attr
= makeLoadAttribute(makeName(iter_name, AST_TYPE::Load, node->lineno), internString("next"), true);
......@@ -267,7 +264,9 @@ private:
push_back(j);
curblock = test_block;
AST_expr* test_call = callNonzero(remapExpr(makeCall(hasnext_attr)));
AST_LangPrimitive* test_call = new AST_LangPrimitive(AST_LangPrimitive::HASNEXT);
test_call->args.push_back(makeName(iter_name, AST_TYPE::Load, node->lineno));
AST_expr* test = remapExpr(test_call);
CFGBlock* body_block = cfg->addBlock();
body_block->info = "comprehension_body";
......@@ -279,7 +278,7 @@ private:
AST_Branch* br = new AST_Branch();
br->col_offset = node->col_offset;
br->lineno = node->lineno;
br->test = test_call;
br->test = test;
br->iftrue = body_block;
br->iffalse = exit_block;
curblock->connectTo(body_block);
......
......@@ -166,6 +166,15 @@ static Box* classobjGetattribute(Box* _cls, Box* _attr) {
return r;
}
static Box* classobj_getattro(Box* cls, Box* attr) noexcept {
try {
return classobjGetattribute(cls, attr);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
Box* classobjStr(Box* _obj) {
if (!isSubclass(_obj->cls, classobj_cls)) {
raiseExcHelper(TypeError, "descriptor '__str__' requires a 'classobj' object but received an '%s'",
......@@ -225,6 +234,15 @@ Box* instanceGetattribute(Box* _inst, Box* _attr) {
return _instanceGetattribute(_inst, _attr, true);
}
static Box* instance_getattro(Box* cls, Box* attr) noexcept {
try {
return instanceGetattribute(cls, attr);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
Box* instanceSetattr(Box* _inst, Box* _attr, Box* value) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
......@@ -418,6 +436,7 @@ void setupClassobj() {
classobj_cls->giveAttr("__dict__", dict_descr);
classobj_cls->freeze();
classobj_cls->tp_getattro = classobj_getattro;
instance_cls->giveAttr("__getattribute__",
......@@ -434,5 +453,6 @@ void setupClassobj() {
instance_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)instanceHash, UNKNOWN, 1)));
instance_cls->freeze();
instance_cls->tp_getattro = instance_getattro;
}
}
......@@ -39,6 +39,21 @@ Box* seqiterIter(Box* s) {
return s;
}
bool seqiterHasnextUnboxed(Box* s) {
RELEASE_ASSERT(s->cls == seqiter_cls || s->cls == seqreviter_cls, "");
BoxedSeqIter* self = static_cast<BoxedSeqIter*>(s);
Box* next;
try {
next = getitem(self->b, boxInt(self->idx));
} catch (ExcInfo e) {
return false;
}
self->idx++;
self->next = next;
return true;
}
Box* seqiterHasnext(Box* s) {
RELEASE_ASSERT(s->cls == seqiter_cls || s->cls == seqreviter_cls, "");
BoxedSeqIter* self = static_cast<BoxedSeqIter*>(s);
......@@ -107,7 +122,7 @@ static void iterwrapperGCVisit(GCVisitor* v, Box* b) {
v->visit(iw->next);
}
Box* iterwrapperHasnext(Box* s) {
bool iterwrapperHasnextUnboxed(Box* s) {
RELEASE_ASSERT(s->cls == iterwrapper_cls, "");
BoxedIterWrapper* self = static_cast<BoxedIterWrapper*>(s);
......@@ -119,12 +134,16 @@ Box* iterwrapperHasnext(Box* s) {
} catch (ExcInfo e) {
if (e.matches(StopIteration)) {
self->next = NULL;
return False;
return false;
}
throw e;
}
self->next = next;
return True;
return true;
}
Box* iterwrapperHasnext(Box* s) {
return boxBool(iterwrapperHasnextUnboxed(s));
}
Box* iterwrapperNext(Box* s) {
......@@ -155,6 +174,7 @@ void setupIter() {
seqiter_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)seqiterIter, UNKNOWN, 1)));
seqiter_cls->freeze();
seqiter_cls->tpp_hasnext = seqiterHasnextUnboxed;
seqreviter_cls = BoxedHeapClass::create(type_cls, object_cls, NULL, 0, 0, sizeof(BoxedSeqIter), false, "reversed");
......@@ -172,5 +192,6 @@ void setupIter() {
new BoxedFunction(boxRTFunction((void*)iterwrapperHasnext, BOXED_BOOL, 1)));
iterwrapper_cls->freeze();
iterwrapper_cls->tpp_hasnext = iterwrapperHasnextUnboxed;
}
}
......@@ -3676,14 +3676,16 @@ extern "C" Box* createBoxedIterWrapperIfNeeded(Box* o) {
}
}
if (typeLookup(o->cls, hasnext_str, NULL) == NULL)
// assert((typeLookup(o->cls, hasnext_str, NULL) == NULL) == (o->cls->tpp_hasnext == object_cls->tpp_hasnext));
if (o->cls->tpp_hasnext == object_cls->tpp_hasnext)
return new BoxedIterWrapper(o);
return o;
}
extern "C" Box* getPystonIter(Box* o) {
Box* r = getiter(o);
if (typeLookup(r->cls, hasnext_str, NULL) == NULL)
// assert((typeLookup(r->cls, hasnext_str, NULL) == NULL) == (r->cls->tpp_hasnext == object_cls->tpp_hasnext));
if (r->cls->tpp_hasnext == object_cls->tpp_hasnext)
return new BoxedIterWrapper(r);
return r;
}
......
......@@ -2419,6 +2419,7 @@ void setupStr() {
new BoxedFunction(boxRTFunction((void*)BoxedStringIterator::hasnext, BOXED_BOOL, 1)));
str_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)BoxedStringIterator::next, STR, 1)));
str_iterator_cls->freeze();
str_iterator_cls->tpp_hasnext = (BoxedClass::pyston_inquiry)BoxedStringIterator::hasnextUnboxed;
str_cls->tp_as_buffer = &string_as_buffer;
......
......@@ -428,6 +428,7 @@ void setupTuple() {
tuple_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)tupleiterNext, UNKNOWN, 1)));
tuple_iterator_cls->freeze();
tuple_iterator_cls->tpp_hasnext = tupleiterHasnextUnboxed;
}
void teardownTuple() {
......
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