Commit 035b313f authored by Marius Wachtler's avatar Marius Wachtler

Add attrwrapper.clear()

parent 0d9eedb9
...@@ -1542,6 +1542,22 @@ public: ...@@ -1542,6 +1542,22 @@ public:
return rtn; return rtn;
} }
static Box* clear(Box* _self) {
RELEASE_ASSERT(_self->cls == attrwrapper_cls, "");
AttrWrapper* self = static_cast<AttrWrapper*>(_self);
HCAttrs* attrs = self->b->getHCAttrsPtr();
RELEASE_ASSERT(attrs->hcls->type == HiddenClass::NORMAL || attrs->hcls->type == HiddenClass::SINGLETON, "");
while (true) {
const auto& attrMap = attrs->hcls->getStrAttrOffsets();
if (attrMap.size() == 0)
break;
self->b->delattr(attrMap.begin()->first(), NULL);
}
return None;
}
static Box* len(Box* _self) { static Box* len(Box* _self) {
RELEASE_ASSERT(_self->cls == attrwrapper_cls, ""); RELEASE_ASSERT(_self->cls == attrwrapper_cls, "");
AttrWrapper* self = static_cast<AttrWrapper*>(_self); AttrWrapper* self = static_cast<AttrWrapper*>(_self);
...@@ -2539,6 +2555,7 @@ void setupRuntime() { ...@@ -2539,6 +2555,7 @@ void setupRuntime() {
new BoxedFunction(boxRTFunction((void*)AttrWrapper::itervalues, UNKNOWN, 1))); new BoxedFunction(boxRTFunction((void*)AttrWrapper::itervalues, UNKNOWN, 1)));
attrwrapper_cls->giveAttr("iteritems", new BoxedFunction(boxRTFunction((void*)AttrWrapper::iteritems, UNKNOWN, 1))); attrwrapper_cls->giveAttr("iteritems", new BoxedFunction(boxRTFunction((void*)AttrWrapper::iteritems, UNKNOWN, 1)));
attrwrapper_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)AttrWrapper::copy, UNKNOWN, 1))); attrwrapper_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)AttrWrapper::copy, UNKNOWN, 1)));
attrwrapper_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)AttrWrapper::clear, NONE, 1)));
attrwrapper_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::len, BOXED_INT, 1))); attrwrapper_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::len, BOXED_INT, 1)));
attrwrapper_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::iter, UNKNOWN, 1))); attrwrapper_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::iter, UNKNOWN, 1)));
attrwrapper_cls->giveAttr("update", attrwrapper_cls->giveAttr("update",
......
...@@ -38,3 +38,12 @@ p() ...@@ -38,3 +38,12 @@ p()
c1.__dict__ = d = {} c1.__dict__ = d = {}
d['i'] = 5 d['i'] = 5
p() p()
class C(object):
def foo(self):
return 0
c = C()
c.attr = "test"
c.__dict__.clear()
print hasattr(c, "attr")
print hasattr(c, "foo")
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