Commit 41f27d72 authored by Marius Wachtler's avatar Marius Wachtler

Implement delattr()

parent d044212d
......@@ -484,6 +484,14 @@ Box* bltinImport(Box* name, Box* globals, Box* locals, Box** args) {
return import(((BoxedInt*)level)->n, fromlist, &static_cast<BoxedString*>(name)->s);
}
Box* delattrFunc(Box* obj, Box* _str) {
if (_str->cls != str_cls)
raiseExcHelper(TypeError, "attribute name must be string, not '%s'", getTypeName(_str));
BoxedString* str = static_cast<BoxedString*>(_str);
delattr(obj, str->s.c_str());
return None;
}
Box* getattrFunc(Box* obj, Box* _str, Box* default_value) {
if (_str->cls != str_cls) {
raiseExcHelper(TypeError, "getattr(): attribute name must be string");
......@@ -956,6 +964,9 @@ void setupBuiltins() {
builtins_module->giveAttr(
"dumpAddr", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)pydumpAddr, UNKNOWN, 1), "dumpAddr"));
builtins_module->giveAttr("delattr",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)delattrFunc, NONE, 2), "delattr"));
builtins_module->giveAttr(
"getattr", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getattrFunc, UNKNOWN, 3, 1, false, false),
"getattr", { NULL }));
......
......@@ -164,3 +164,11 @@ try:
print "f(c) error"
except AttributeError, e:
print e
c.a = 2
c.b = 3
print hasattr(c, "a"), hasattr(c, "b")
delattr(c, "a");
print hasattr(c, "a"), hasattr(c, "b")
delattr(c, "b");
print hasattr(c, "a"), hasattr(c, "b")
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