Commit 1d452dde authored by Kevin Modzelewski's avatar Kevin Modzelewski

(oldstyle) instance.nonzero

parent 06c7d568
......@@ -154,7 +154,7 @@ Box* classobjStr(Box* _obj) {
return boxString(static_cast<BoxedString*>(_mod)->s + "." + cls->name->s);
}
Box* instanceGetattribute(Box* _inst, Box* _attr) {
static Box* _instanceGetattribute(Box* _inst, Box* _attr, bool raise_on_missing) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
......@@ -177,15 +177,22 @@ Box* instanceGetattribute(Box* _inst, Box* _attr) {
Box* getattr = classLookup(inst->inst_cls, getattr_str);
RELEASE_ASSERT(getattr == NULL, "unimplemented");
if (!raise_on_missing)
return NULL;
raiseExcHelper(AttributeError, "%s instance has no attribute '%s'", inst->inst_cls->name->s.c_str(),
attr->s.c_str());
}
Box* instanceGetattribute(Box* _inst, Box* _attr) {
return _instanceGetattribute(_inst, _attr, true);
}
Box* instanceStr(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* str_func = instanceGetattribute(inst, boxStrConstant("__str__"));
Box* str_func = _instanceGetattribute(inst, boxStrConstant("__str__"), false);
if (str_func) {
return runtimeCall(str_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
......@@ -194,6 +201,19 @@ Box* instanceStr(Box* _inst) {
}
}
Box* instanceNonzero(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* nonzero_func = _instanceGetattribute(inst, boxStrConstant("__nonzero__"), false);
if (nonzero_func) {
return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} else {
return True;
}
}
void setupClassobj() {
classobj_cls = new BoxedClass(type_cls, object_cls, &BoxedClassobj::gcHandler, offsetof(BoxedClassobj, attrs),
sizeof(BoxedClassobj), false);
......@@ -218,6 +238,7 @@ void setupClassobj() {
instance_cls->giveAttr("__getattribute__",
new BoxedFunction(boxRTFunction((void*)instanceGetattribute, UNKNOWN, 2)));
instance_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)instanceStr, UNKNOWN, 1)));
instance_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)instanceNonzero, UNKNOWN, 1)));
instance_cls->freeze();
}
......
......@@ -38,6 +38,7 @@
#include "gc/collector.h"
#include "gc/heap.h"
#include "runtime/capi.h"
#include "runtime/classobj.h"
#include "runtime/float.h"
#include "runtime/generator.h"
#include "runtime/iterobject.h"
......@@ -1547,7 +1548,8 @@ extern "C" bool nonzero(Box* obj) {
Box* func = getclsattr_internal(obj, "__nonzero__", NULL);
if (func == NULL) {
RELEASE_ASSERT(isUserDefined(obj->cls), "%s.__nonzero__", getTypeName(obj)->c_str()); // TODO
ASSERT(isUserDefined(obj->cls) || obj->cls == classobj_cls, "%s.__nonzero__",
getTypeName(obj)->c_str()); // TODO
return true;
}
......
......@@ -53,3 +53,16 @@ def str2():
return "str2"
e.__str__ = str2
print e
print bool(e)
print bool(E)
class F:
def __init__(self, n):
self.n = n
def __nonzero__(self):
return self.n
print bool(F(0))
print bool(F(1))
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