Commit 2775f89f authored by Travis Hance's avatar Travis Hance Committed by Travis Hance

made property with get

parent cd73ad20
......@@ -826,5 +826,6 @@ void setupBuiltins() {
builtins_module->giveAttr("instancemethod", instancemethod_cls);
builtins_module->giveAttr("complex", complex_cls);
builtins_module->giveAttr("super", super_cls);
builtins_module->giveAttr("property", property_cls);
}
}
......@@ -23,10 +23,23 @@ static Box* memberGet(BoxedMemberDescriptor* self, Box* inst, Box* owner) {
Py_FatalError("unimplemented");
}
static Box* propertyNew(Box* cls, Box* fget, Box* fset, Box** args) {
RELEASE_ASSERT(cls == property_cls, "");
Box* fdel = args[0];
Box* doc = args[1];
return new BoxedProperty(fget, fset, fdel, doc);
}
void setupDescr() {
member_cls->giveAttr("__name__", boxStrConstant("member"));
member_cls->giveAttr("__get__", new BoxedFunction(boxRTFunction((void*)memberGet, UNKNOWN, 3)));
member_cls->freeze();
property_cls->giveAttr("__name__", boxStrConstant("property"));
property_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)propertyNew, UNKNOWN, 5, 4, false, false),
{ None, None, None, None }));
property_cls->freeze();
}
void teardownDescr() {
......
......@@ -907,6 +907,16 @@ Box* dataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, const
}
}
else if (descr->cls == property_cls) {
rewrite_args = NULL; // TODO
BoxedProperty* prop = static_cast<BoxedProperty*>(descr);
if (prop->prop_get == NULL || prop->prop_get == None) {
raiseExcHelper(AttributeError, "unreadable attribute");
}
return runtimeCallInternal1(prop->prop_get, NULL, ArgPassSpec(1), obj);
}
return NULL;
}
......
......@@ -291,7 +291,7 @@ extern "C" void closureGCHandler(GCVisitor* v, Box* b) {
extern "C" {
BoxedClass* object_cls, *type_cls, *none_cls, *bool_cls, *int_cls, *float_cls, *str_cls, *function_cls,
*instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *member_cls,
*closure_cls, *generator_cls, *complex_cls, *basestring_cls, *unicode_cls;
*closure_cls, *generator_cls, *complex_cls, *basestring_cls, *unicode_cls, *property_cls;
BoxedTuple* EmptyTuple;
......@@ -700,6 +700,7 @@ void setupRuntime() {
member_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedMemberDescriptor), false);
closure_cls = new BoxedClass(type_cls, object_cls, &closureGCHandler, offsetof(BoxedClosure, attrs),
sizeof(BoxedClosure), false);
property_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedProperty), false);
attrwrapper_cls = new BoxedClass(type_cls, object_cls, &AttrWrapper::gcHandler, 0, sizeof(AttrWrapper), false);
STR = typeFromClass(str_cls);
......
......@@ -80,7 +80,7 @@ Box* getSysStdout();
extern "C" {
extern BoxedClass* object_cls, *type_cls, *bool_cls, *int_cls, *long_cls, *float_cls, *str_cls, *function_cls,
*none_cls, *instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *xrange_cls,
*member_cls, *method_cls, *closure_cls, *generator_cls, *complex_cls, *basestring_cls, *unicode_cls;
*member_cls, *method_cls, *closure_cls, *generator_cls, *complex_cls, *basestring_cls, *unicode_cls, *property_cls;
}
extern "C" { extern Box* None, *NotImplemented, *True, *False; }
extern "C" {
......@@ -390,6 +390,17 @@ public:
: Box(member_cls), type((MemberType)member->type), offset(member->offset) {}
};
class BoxedProperty : public Box {
public:
Box* prop_get;
Box* prop_set;
Box* prop_del;
Box* prop_doc;
BoxedProperty(Box* get, Box* set, Box* del, Box* doc)
: Box(property_cls), prop_get(get), prop_set(set), prop_del(del), prop_doc(doc) {}
};
// TODO is there any particular reason to make this a Box, ie a python-level object?
class BoxedClosure : public Box {
public:
......
class C(object):
def fget(self):
return 5
x = property(fget)
c = C()
print c.x
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