Commit 28a115d2 authored by Travis Hance's avatar Travis Hance

get cpython/test_property.py to pass

parent d5de0aad
# expected: fail
# Test case for property
# more tests are in test_descr
......
......@@ -34,20 +34,9 @@ static Box* memberGet(BoxedMemberDescriptor* self, Box* inst, Box* owner) {
Py_FatalError("unimplemented");
}
static Box* propertyInit(Box* _self, Box* fget, Box* fset, Box** args) {
RELEASE_ASSERT(isSubclass(_self->cls, property_cls), "");
Box* fdel = args[0];
Box* doc = args[1];
BoxedProperty* self = static_cast<BoxedProperty*>(_self);
self->prop_get = fget;
self->prop_set = fset;
self->prop_del = fdel;
self->prop_doc = doc;
self->getter_doc = false;
/* if no docstring given and the getter has one, use that one */
if ((doc == NULL || doc == None) && fget != NULL) {
static void propertyDocCopy(BoxedProperty* prop, Box* fget) {
assert(prop);
assert(fget);
Box* get_doc;
try {
get_doc = getattrInternal(fget, "__doc__", NULL);
......@@ -59,17 +48,34 @@ static Box* propertyInit(Box* _self, Box* fget, Box* fset, Box** args) {
}
if (get_doc) {
if (self->cls == property_cls) {
self->prop_doc = get_doc;
if (prop->cls == property_cls) {
prop->prop_doc = get_doc;
} else {
/* If this is a property subclass, put __doc__
in dict of the subclass instance instead,
otherwise it gets shadowed by __doc__ in the
class's dict. */
setattr(self, "__doc__", get_doc);
setattr(prop, "__doc__", get_doc);
}
self->getter_doc = true;
prop->getter_doc = true;
}
}
static Box* propertyInit(Box* _self, Box* fget, Box* fset, Box** args) {
RELEASE_ASSERT(isSubclass(_self->cls, property_cls), "");
Box* fdel = args[0];
Box* doc = args[1];
BoxedProperty* self = static_cast<BoxedProperty*>(_self);
self->prop_get = fget;
self->prop_set = fset;
self->prop_del = fdel;
self->prop_doc = doc;
self->getter_doc = false;
/* if no docstring given and the getter has one, use that one */
if ((doc == NULL || doc == None) && fget != NULL) {
propertyDocCopy(self, fget);
}
return None;
......@@ -120,17 +126,31 @@ static Box* propertyDel(Box* self, Box* obj) {
static Box* property_copy(BoxedProperty* old, Box* get, Box* set, Box* del) {
RELEASE_ASSERT(isSubclass(old->cls, property_cls), "");
if (!get)
if (!get || get == None)
get = old->prop_get;
if (!set)
if (!set || set == None)
set = old->prop_set;
if (!del)
if (!del || del == None)
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 runtimeCall(old->cls, ArgPassSpec(4), get, set, del, &old->prop_doc, NULL);
if (old->cls == property_cls) {
BoxedProperty* prop = new BoxedProperty(get, set, del, old->prop_doc);
prop->getter_doc = false;
if ((old->getter_doc && get != None) || !old->prop_doc)
propertyDocCopy(prop, get);
return prop;
} else {
Box* doc;
if ((old->getter_doc && get != None) || !old->prop_doc)
doc = None;
else
doc = old->prop_doc;
return runtimeCall(old->cls, ArgPassSpec(4), get, set, del, &doc, NULL);
}
}
static Box* propertyGetter(Box* self, Box* obj) {
......
......@@ -122,3 +122,47 @@ try:
f = property(ObjWithDocDesc)
except BaseException as e:
print e.message
print 'test the setting of a __doc__ when you copy it'
class Desc(object):
def __get__(self, obj, typ):
print 'desc called'
return "blah"
class ObjWithDocDesc(object):
__doc__ = Desc()
prop = property(ObjWithDocDesc)
print 'made prop'
print prop.__doc__
def g():
"""doc of g"""
return 5
prop2 = prop.getter(g)
print 'made prop2'
print prop2.__doc__
prop3 = prop.setter(lambda self, val : None)
print prop3.__doc__
prop4 = prop.deleter(lambda self, val : None)
print prop4.__doc__
print 'test the setting of a __doc__ when you copy it when using a subclass of property'
class PropertySubclass(property):
pass
class Desc(object):
def __get__(self, obj, typ):
print 'desc called'
return "blah"
class ObjWithDocDesc(object):
__doc__ = Desc()
prop = PropertySubclass(ObjWithDocDesc)
print 'made prop'
print prop.__doc__
def g():
"""doc of g"""
return 5
prop2 = prop.getter(g)
print 'made prop2'
print prop2.__doc__
prop3 = prop.setter(lambda self, val : None)
print prop3.__doc__
prop4 = prop.deleter(lambda self, val : None)
print prop4.__doc__
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