<s> import unittest <EOL> from test import test_support <EOL> import sys <EOL> class TestIsInstanceExceptions ( unittest . TestCase ) : <EOL> def test_class_has_no_bases ( self ) : <EOL> class I ( object ) : <EOL> def getclass ( self ) : <EOL> return None <EOL> __class__ = property ( getclass ) <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> return ( ) <EOL> __bases__ = property ( getbases ) <EOL> self . assertEqual ( False , isinstance ( I ( ) , C ( ) ) ) <EOL> def test_bases_raises_other_than_attribute_error ( self ) : <EOL> class E ( object ) : <EOL> def getbases ( self ) : <EOL> raise RuntimeError <EOL> __bases__ = property ( getbases ) <EOL> class I ( object ) : <EOL> def getclass ( self ) : <EOL> return E ( ) <EOL> __class__ = property ( getclass ) <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> return ( ) <EOL> __bases__ = property ( getbases ) <EOL> self . assertRaises ( RuntimeError , isinstance , I ( ) , C ( ) ) <EOL> def test_dont_mask_non_attribute_error ( self ) : <EOL> class I : pass <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise RuntimeError <EOL> __bases__ = property ( getbases ) <EOL> self . assertRaises ( RuntimeError , isinstance , I ( ) , C ( ) ) <EOL> def test_mask_attribute_error ( self ) : <EOL> class I : pass <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise AttributeError <EOL> __bases__ = property ( getbases ) <EOL> self . assertRaises ( TypeError , isinstance , I ( ) , C ( ) ) <EOL> class TestIsSubclassExceptions ( unittest . TestCase ) : <EOL> def test_dont_mask_non_attribute_error ( self ) : <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise RuntimeError <EOL> __bases__ = property ( getbases ) <EOL> class S ( C ) : pass <EOL> self . assertRaises ( RuntimeError , issubclass , C ( ) , S ( ) ) <EOL> def test_mask_attribute_error ( self ) : <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise AttributeError <EOL> __bases__ = property ( getbases ) <EOL> class S ( C ) : pass <EOL> self . assertRaises ( TypeError , issubclass , C ( ) , S ( ) ) <EOL> def test_dont_mask_non_attribute_error_in_cls_arg ( self ) : <EOL> class B : pass <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise RuntimeError <EOL> __bases__ = property ( getbases ) <EOL> self . assertRaises ( RuntimeError , issubclass , B , C ( ) ) <EOL> def test_mask_attribute_error_in_cls_arg ( self ) : <EOL> class B : pass <EOL> class C ( object ) : <EOL> def getbases ( self ) : <EOL> raise AttributeError <EOL> __bases__ = property ( getbases ) <EOL> self . assertRaises ( TypeError , issubclass , B , C ( ) ) <EOL> class AbstractClass ( object ) : <EOL> def __init__ ( self , bases ) : <EOL> self . bases = bases <EOL> def getbases ( self ) : <EOL> return self . bases <EOL> __bases__ = property ( getbases ) <EOL> def __call__ ( self ) : <EOL> return AbstractInstance ( self ) <EOL> class AbstractInstance ( object ) : <EOL> def __init__ ( self , klass ) : <EOL> self . klass = klass <EOL> def getclass ( self ) : <EOL> return self . klass <EOL> __class__ = property ( getclass ) <EOL> AbstractSuper = AbstractClass ( bases = ( ) ) <EOL> AbstractChild = AbstractClass ( bases = ( AbstractSuper , ) ) <EOL> class Super : <EOL> pass <EOL> class Child ( Super ) : <EOL> pass <EOL> class NewSuper ( object ) : <EOL> pass <EOL> class NewChild ( NewSuper ) : <EOL> pass <EOL> class TestIsInstanceIsSubclass ( unittest . TestCase ) : <EOL> def test_isinstance_normal ( self ) : <EOL> self . assertEqual ( True , isinstance ( Super ( ) , Super ) ) <EOL> self . assertEqual ( False , isinstance ( Super ( ) , Child ) ) <EOL> self . assertEqual ( False , isinstance ( Super ( ) , AbstractSuper ) ) <EOL> self . assertEqual ( False , isinstance ( Super ( ) , AbstractChild ) ) <EOL> self . assertEqual ( True , isinstance ( Child ( ) , Super ) ) <EOL> self . assertEqual ( False , isinstance ( Child ( ) , AbstractSuper ) ) <EOL> def test_isinstance_abstract ( self ) : <EOL> self . assertEqual ( True , isinstance ( AbstractSuper ( ) , AbstractSuper ) ) <EOL> self . assertEqual ( False , isinstance ( AbstractSuper ( ) , AbstractChild ) ) <EOL> self . assertEqual ( False , isinstance ( AbstractSuper ( ) , Super ) ) <EOL> self . assertEqual ( False , isinstance ( AbstractSuper ( ) , Child ) ) <EOL> self . assertEqual ( True , isinstance ( AbstractChild ( ) , AbstractChild ) ) <EOL> self . assertEqual ( True , isinstance ( AbstractChild ( ) , AbstractSuper ) ) <EOL> self . assertEqual ( False , isinstance ( AbstractChild ( ) , Super ) ) <EOL> self . assertEqual ( False , isinstance ( AbstractChild ( ) , Child ) ) <EOL> def test_subclass_normal ( self ) : <EOL> self . assertEqual ( True , issubclass ( Super , Super ) ) <EOL> self . assertEqual ( False , issubclass ( Super , AbstractSuper ) ) <EOL> self . assertEqual ( False , issubclass ( Super , Child ) ) <EOL> self . assertEqual ( True , issubclass ( Child , Child ) ) <EOL> self . assertEqual ( True , issubclass ( Child , Super ) ) <EOL> self . assertEqual ( False , issubclass ( Child , AbstractSuper ) ) <EOL> def test_subclass_abstract ( self ) : <EOL> self . assertEqual ( True , issubclass ( AbstractSuper , AbstractSuper ) ) <EOL> self . assertEqual ( False , issubclass ( AbstractSuper , AbstractChild ) ) <EOL> self . assertEqual ( False , issubclass ( AbstractSuper , Child ) ) <EOL> self . assertEqual ( True , issubclass ( AbstractChild , AbstractChild ) ) <EOL> self . assertEqual ( True , issubclass ( AbstractChild , AbstractSuper ) ) <EOL> self . assertEqual ( False , issubclass ( AbstractChild , Super ) ) <EOL> self . assertEqual ( False , issubclass ( AbstractChild , Child ) ) <EOL> def test_subclass_tuple ( self ) : <EOL> self . assertEqual ( True , issubclass ( Child , ( Child , ) ) ) <EOL> self . assertEqual ( True , issubclass ( Child , ( Super , ) ) ) <EOL> self . assertEqual ( False , issubclass ( Super , ( Child , ) ) ) <EOL> self . assertEqual ( True , issubclass ( Super , ( Child , Super ) ) ) <EOL> self . assertEqual ( False , issubclass ( Child , ( ) ) ) <EOL> self . assertEqual ( True , issubclass ( Super , ( Child , ( Super , ) ) ) ) <EOL> self . assertEqual ( True , issubclass ( NewChild , ( NewChild , ) ) ) <EOL> self . assertEqual ( True , issubclass ( NewChild , ( NewSuper , ) ) ) <EOL> self . assertEqual ( False , issubclass ( NewSuper , ( NewChild , ) ) ) <EOL> self . assertEqual ( True , issubclass ( NewSuper , ( NewChild , NewSuper ) ) ) <EOL> self . assertEqual ( False , issubclass ( NewChild , ( ) ) ) <EOL> self . assertEqual ( True , issubclass ( NewSuper , ( NewChild , ( NewSuper , ) ) ) ) <EOL> self . assertEqual ( True , issubclass ( int , ( long , ( float , int ) ) ) ) <EOL> if test_support . have_unicode : <EOL> self . assertEqual ( True , issubclass ( str , ( unicode , ( Child , NewChild , basestring ) ) ) ) <EOL> def test_subclass_recursion_limit ( self ) : <EOL> self . assertRaises ( RuntimeError , blowstack , issubclass , str , str ) <EOL> def test_isinstance_recursion_limit ( self ) : <EOL> self . assertRaises ( RuntimeError , blowstack , isinstance , '<STR_LIT>' , str ) <EOL> def blowstack ( fxn , arg , compare_to ) : <EOL> tuple_arg = ( compare_to , ) <EOL> for cnt in xrange ( sys . getrecursionlimit ( ) + <NUM_LIT:5> ) : <EOL> tuple_arg = ( tuple_arg , ) <EOL> fxn ( arg , tuple_arg ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( <EOL> TestIsInstanceExceptions , <EOL> TestIsSubclassExceptions , <EOL> TestIsInstanceIsSubclass <EOL> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
