Commit abdf99d3 authored by Marius Wachtler's avatar Marius Wachtler

Allow subclassing of properties

parent b0ba8e15
...@@ -91,9 +91,7 @@ static Box* propertyDel(Box* self, Box* obj) { ...@@ -91,9 +91,7 @@ static Box* propertyDel(Box* self, Box* obj) {
} }
static Box* property_copy(BoxedProperty* old, Box* get, Box* set, Box* del) { static Box* property_copy(BoxedProperty* old, Box* get, Box* set, Box* del) {
// In CPython, I think this can take a subclass of property, and will call the subclass's RELEASE_ASSERT(isSubclass(old->cls, property_cls), "");
// constructor... for now just enforce that it's a property object and inline the constructor:
RELEASE_ASSERT(old->cls == property_cls, "");
if (!get) if (!get)
get = old->prop_get; get = old->prop_get;
...@@ -102,7 +100,10 @@ static Box* property_copy(BoxedProperty* old, Box* get, Box* set, Box* del) { ...@@ -102,7 +100,10 @@ static Box* property_copy(BoxedProperty* old, Box* get, Box* set, Box* del) {
if (!del) if (!del)
del = old->prop_del; del = old->prop_del;
// Optimization for the case when the old propery is not subclassed
if (old->cls == property_cls)
return new BoxedProperty(get, set, del, old->prop_doc); return new BoxedProperty(get, set, del, old->prop_doc);
return runtimeCall(old->cls, ArgPassSpec(4), get, set, del, &old->prop_doc, NULL);
} }
static Box* propertyGetter(Box* self, Box* obj) { static Box* propertyGetter(Box* self, Box* obj) {
......
...@@ -63,3 +63,22 @@ try: ...@@ -63,3 +63,22 @@ try:
except AttributeError, e: except AttributeError, e:
print e print e
c.x = 1 c.x = 1
class MyProperty(property):
pass
class C(object):
v = "empty"
@MyProperty
def p(self):
print "get"
return self.v
@p.setter
def p(self, value):
print "set"
self.v = "it " + value
c = C()
c.p = "worked"
print c.p
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