Commit 8c5b2a78 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Basic support for issubclass() on old style classes

parent 54746476
...@@ -340,13 +340,21 @@ Box* isinstance_func(Box* obj, Box* cls) { ...@@ -340,13 +340,21 @@ Box* isinstance_func(Box* obj, Box* cls) {
} }
Box* issubclass_func(Box* child, Box* parent) { Box* issubclass_func(Box* child, Box* parent) {
if (parent->cls == classobj_cls) { if (child->cls != type_cls && child->cls != classobj_cls)
Py_FatalError("don't handle issubclass for old style classes yet"); raiseExcHelper(TypeError, "issubclass() arg 1 must be a class");
RELEASE_ASSERT(parent->cls != tuple_cls, "unsupported");
if (child->cls == classobj_cls) {
if (parent->cls != classobj_cls)
return False;
return boxBool(classobjIssubclass(static_cast<BoxedClassobj*>(child), static_cast<BoxedClassobj*>(parent)));
} }
RELEASE_ASSERT(child->cls == type_cls, ""); assert(child->cls == type_cls);
// TODO parent can also be a tuple of classes if (parent->cls != type_cls)
RELEASE_ASSERT(parent->cls == type_cls, ""); return False;
return boxBool(isSubclass(static_cast<BoxedClass*>(child), static_cast<BoxedClass*>(parent))); return boxBool(isSubclass(static_cast<BoxedClass*>(child), static_cast<BoxedClass*>(parent)));
} }
......
...@@ -25,19 +25,19 @@ namespace pyston { ...@@ -25,19 +25,19 @@ namespace pyston {
BoxedClass* classobj_cls, *instance_cls; BoxedClass* classobj_cls, *instance_cls;
bool classIssubclass(BoxedClassobj* child, BoxedClassobj* parent) { bool classobjIssubclass(BoxedClassobj* child, BoxedClassobj* parent) {
if (child == parent) if (child == parent)
return true; return true;
for (auto e : child->bases->elts) { for (auto e : child->bases->elts) {
if (e->cls == classobj_cls && classIssubclass(static_cast<BoxedClassobj*>(e), parent)) if (e->cls == classobj_cls && classobjIssubclass(static_cast<BoxedClassobj*>(e), parent))
return true; return true;
} }
return false; return false;
} }
bool instanceIsinstance(BoxedInstance* obj, BoxedClassobj* cls) { bool instanceIsinstance(BoxedInstance* obj, BoxedClassobj* cls) {
return classIssubclass(obj->inst_cls, cls); return classobjIssubclass(obj->inst_cls, cls);
} }
static Box* classLookup(BoxedClassobj* cls, const std::string& attr) { static Box* classLookup(BoxedClassobj* cls, const std::string& attr) {
......
...@@ -27,6 +27,7 @@ class BoxedInstance; ...@@ -27,6 +27,7 @@ class BoxedInstance;
extern BoxedClass* classobj_cls, *instance_cls; extern BoxedClass* classobj_cls, *instance_cls;
bool instanceIsinstance(BoxedInstance* obj, BoxedClassobj* cls); bool instanceIsinstance(BoxedInstance* obj, BoxedClassobj* cls);
bool classobjIssubclass(BoxedClassobj* child, BoxedClassobj* parent);
class BoxedClassobj : public Box { class BoxedClassobj : public Box {
public: public:
......
...@@ -102,3 +102,12 @@ print isinstance(D(), C) ...@@ -102,3 +102,12 @@ print isinstance(D(), C)
print str(f)[:26] print str(f)[:26]
print repr(f)[:26] print repr(f)[:26]
class OldStyleClass:
pass
print issubclass(OldStyleClass, object)
print isinstance(OldStyleClass(), OldStyleClass)
print issubclass(OldStyleClass, OldStyleClass)
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