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