Commit 3fa9064d authored by Krzysztof Klinikowski's avatar Krzysztof Klinikowski

Added hasattr implementation buildin

parent b2f7eddc
......@@ -299,6 +299,18 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) {
return rtn;
}
Box* hasattr(Box* obj, Box* _str) {
if (_str->cls != str_cls) {
raiseExcHelper(TypeError, "hasattr(): attribute name must be string");
}
BoxedString* str = static_cast<BoxedString*>(_str);
Box* attr = getattr_internal(obj, str->s, true, true, NULL, NULL);
Box* rtn = attr ? True : False;
return rtn;
}
Box* map2(Box* f, Box* container) {
Box* rtn = new BoxedList();
for (Box* e : container->pyElements()) {
......@@ -452,6 +464,10 @@ void setupBuiltins() {
addRTFunction(getattr_func, (void*)getattr3, NULL, 3, false);
builtins_module->giveAttr("getattr", new BoxedFunction(getattr_func));
Box* hasattr_obj = new BoxedFunction(boxRTFunction((void*)hasattr, NULL, 2, false));
builtins_module->giveAttr("hasattr", hasattr_obj);
Box* isinstance_obj = new BoxedFunction(boxRTFunction((void*)isinstance_func, NULL, 2, false));
builtins_module->giveAttr("isinstance", isinstance_obj);
......
a = 'test'
assert hasattr(a, "__str__")
assert hasattr(a, "dupa") == False
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