1# ticket: 608 2 3cdef class MyInt(int): 4 """ 5 >>> MyInt(2) == 2 6 True 7 >>> MyInt(2).attr is None 8 True 9 """ 10 cdef readonly object attr 11 12cdef class MyInt2(int): 13 """ 14 >>> MyInt2(2) == 2 15 True 16 >>> MyInt2(2).attr is None 17 True 18 >>> MyInt2(2).test(3) 19 5 20 """ 21 cdef readonly object attr 22 23 def test(self, arg): 24 return self._test(arg) 25 26 cdef _test(self, arg): 27 return self + arg 28 29cdef class MyInt3(MyInt2): 30 """ 31 >>> MyInt3(2) == 2 32 True 33 >>> MyInt3(2).attr is None 34 True 35 >>> MyInt3(2).test(3) 36 6 37 """ 38 cdef _test(self, arg): 39 return self + arg + 1 40 41cdef class MyFloat(float): 42 """ 43 >>> MyFloat(1.0)== 1.0 44 True 45 >>> MyFloat(1.0).attr is None 46 True 47 """ 48 cdef readonly object attr 49 50ustring = u'abc' 51 52cdef class MyUnicode(unicode): 53 """ 54 >>> MyUnicode(ustring) == ustring 55 True 56 >>> MyUnicode(ustring + ustring) == ustring 57 False 58 >>> MyUnicode(ustring).attr is None 59 True 60 """ 61 cdef readonly object attr 62 63cdef class MyList(list): 64 """ 65 >>> MyList([1,2,3]) == [1,2,3] 66 True 67 >>> MyList([1,2,3]).attr is None 68 True 69 """ 70 cdef readonly object attr 71 72cdef class MyListOverride(list): 73 """ 74 >>> MyListOverride([1,2,3]) == [1,2,3] 75 True 76 >>> l = MyListOverride([1,2,3]) 77 >>> l.reverse() 78 >>> l 79 [1, 2, 3, 5] 80 >>> l._reverse() 81 >>> l 82 [1, 2, 3, 5, 5] 83 """ 84 # not doctested: 85 """ 86 >>> l = MyListOverride([1,2,3]) 87 >>> l.append(8) 88 >>> l 89 [1, 2, 3, 0, 8] 90 >>> l._append(9) 91 >>> l 92 [1, 2, 3, 0, 8, 0, 9] 93 """ 94 def reverse(self): 95 self[:] = self + [5] 96 97 def _reverse(self): 98 self.reverse() 99 100 ## FIXME: this doesn't currently work: 101 102 ## cdef int append(self, value) except -1: 103 ## self[:] = self + [0] + [value] 104 ## return 0 105 106 ## def _append(self, value): 107 ## self.append(value) 108 109cdef class MyDict(dict): 110 """ 111 >>> MyDict({1:2, 3:4}) == {1:2, 3:4} 112 True 113 >>> MyDict({1:2, 3:4}).attr is None 114 True 115 """ 116 cdef readonly object attr 117 118cdef class MyException(Exception): 119 """ 120 >>> raise MyException(3) # doctest: +IGNORE_EXCEPTION_DETAIL 121 Traceback (most recent call last): 122 ... 123 MyException: 3 124 """ 125 cdef readonly int value 126 def __cinit__(self, value): 127 self.value = value 128 129def test_exception_isinstance(maybe_exn): 130 """ 131 >>> test_exception_isinstance(Exception()) 132 True 133 >>> test_exception_isinstance(MyException(3)) 134 True 135 >>> test_exception_isinstance(3) 136 False 137 """ 138 return isinstance(maybe_exn, Exception) 139 140def test_exception_type_cast(Exception maybe_exn): 141 """ 142 >>> test_exception_type_cast(Exception()) 143 >>> test_exception_type_cast(MyException(3)) 144 >>> test_exception_type_cast(3) # doctest: +ELLIPSIS 145 Traceback (most recent call last): 146 ... 147 TypeError: Argument 'maybe_exn' has incorrect type (expected ...Exception, got int) 148 """ 149 cdef object o = maybe_exn 150 cdef Exception e = o 151