Commit c5ee90e5 authored by Vinzenz Feenstra's avatar Vinzenz Feenstra

Extended test, usage of callattrInternal and getattr_internal

Signed-off-by: default avatarVinzenz Feenstra <evilissimo@gmail.com>
parent f9992af3
......@@ -38,22 +38,19 @@ extern "C" Box* dir1(Box* obj) {
// TODO: Recursive class traversal for lookup of types and eliminating
// duplicates afterwards
BoxedList* result = nullptr;
if (obj->cls->hasattrs) {
// If __dir__ is present just call it and return what it returns
HCBox* hcb = static_cast<HCBox*>(obj);
Box* obj_dir = hcb->cls->getattr("__dir__", nullptr, nullptr);
if (obj_dir) {
return runtimeCall(obj_dir, 1, obj, nullptr, nullptr, nullptr);
}
// If __dir__ is present just call it and return what it returns
static std::string attr_dir = "__dir__";
Box* dir_result = callattrInternal(obj, &attr_dir, CLASS_ONLY, nullptr, 0, nullptr, nullptr, nullptr, nullptr);
if (dir_result && dir_result->cls == list_cls) {
return dir_result;
}
// If __dict__ is present use its keys and add the reset below
Box* obj_dict = hcb->cls->getattr("__dict__", nullptr, nullptr);
if (obj_dict) {
result = new BoxedList();
assert(obj_dict->cls == dict_cls);
for (auto& kv : static_cast<BoxedDict*>(obj_dict)->d) {
listAppend(result, kv.first);
}
// If __dict__ is present use its keys and add the reset below
Box* obj_dict = getattr_internal(obj, "__dict__", false, true, nullptr, nullptr);
if (obj_dict && obj_dict->cls == dict_cls) {
result = new BoxedList();
for (auto& kv : static_cast<BoxedDict*>(obj_dict)->d) {
listAppend(result, kv.first);
}
}
if (!result) {
......
......@@ -21,8 +21,8 @@ class TestClass(object):
class TestClass2(object):
def __init__(self):
self.attribute1 = None
self.__dict__ = {'dictAttr': True}
self.__dict__ = {'dictAttr': False, 'attribute1': None}
self.other_attribute = False
def method1(self):
pass
......@@ -50,6 +50,22 @@ dir(int)
dir(str)
dir(set)
tc = TestClass()
for f in dir(tc):
print tc.__dict__[f]
def test_in_dir(names, obj):
r = dir(obj)
print [n in r for n in names]
test_in_dir(fake(), TestClass())
test_in_dir(['attribute1', 'method1'], TestClass2)
test_in_dir(['attribute1', 'method1', 'dictAttr'], TestClass2())
test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
'__module__'] + fake(), TestClass)
test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
'__module__', 'method1', 'dictAttr', 'attribute1'], TestClass2)
test_in_dir(['attribute1', 'dictAttr', '__init__', '__module__', 'method1',
'other_attribute', '__dict__'], TestClass2())
test_in_dir(fake(), TestClass())
print len(fake()) == len(dir(TestClass()))
for t in [str, int, list, set, dict]:
test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__module__'], t)
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