Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1 | "Test the functionality of Python classes implementing operators." |
| 2 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 3 | import unittest |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 4 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 5 | |
| 6 | testmeths = [ |
| 7 | |
| 8 | # Binary operations |
| 9 | "add", |
| 10 | "radd", |
| 11 | "sub", |
| 12 | "rsub", |
| 13 | "mul", |
| 14 | "rmul", |
Serhiy Storchaka | c2ccce7 | 2015-03-12 22:01:30 +0200 | [diff] [blame] | 15 | "matmul", |
| 16 | "rmatmul", |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 17 | "truediv", |
| 18 | "rtruediv", |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 19 | "floordiv", |
| 20 | "rfloordiv", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 21 | "mod", |
| 22 | "rmod", |
| 23 | "divmod", |
| 24 | "rdivmod", |
| 25 | "pow", |
| 26 | "rpow", |
| 27 | "rshift", |
| 28 | "rrshift", |
| 29 | "lshift", |
| 30 | "rlshift", |
| 31 | "and", |
| 32 | "rand", |
| 33 | "or", |
| 34 | "ror", |
| 35 | "xor", |
| 36 | "rxor", |
| 37 | |
| 38 | # List/dict operations |
| 39 | "contains", |
| 40 | "getitem", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 41 | "setitem", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 42 | "delitem", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 43 | |
| 44 | # Unary operations |
| 45 | "neg", |
| 46 | "pos", |
| 47 | "abs", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 48 | |
| 49 | # generic operations |
| 50 | "init", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 51 | ] |
| 52 | |
| 53 | # These need to return something other than None |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 54 | # "hash", |
| 55 | # "str", |
| 56 | # "repr", |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 57 | # "int", |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 58 | # "float", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 59 | |
| 60 | # These are separate because they can influence the test of other methods. |
| 61 | # "getattr", |
| 62 | # "setattr", |
| 63 | # "delattr", |
| 64 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 65 | callLst = [] |
| 66 | def trackCall(f): |
| 67 | def track(*args, **kwargs): |
| 68 | callLst.append((f.__name__, args)) |
| 69 | return f(*args, **kwargs) |
| 70 | return track |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 71 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 72 | statictests = """ |
| 73 | @trackCall |
| 74 | def __hash__(self, *args): |
| 75 | return hash(id(self)) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 77 | @trackCall |
| 78 | def __str__(self, *args): |
| 79 | return "AllTests" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 80 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 81 | @trackCall |
| 82 | def __repr__(self, *args): |
| 83 | return "AllTests" |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 84 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 85 | @trackCall |
| 86 | def __int__(self, *args): |
| 87 | return 1 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 89 | @trackCall |
| 90 | def __index__(self, *args): |
| 91 | return 1 |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 92 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 93 | @trackCall |
| 94 | def __float__(self, *args): |
| 95 | return 1.0 |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 96 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 97 | @trackCall |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 98 | def __eq__(self, *args): |
| 99 | return True |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 101 | @trackCall |
| 102 | def __ne__(self, *args): |
| 103 | return False |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 104 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 105 | @trackCall |
| 106 | def __lt__(self, *args): |
| 107 | return False |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 108 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 109 | @trackCall |
| 110 | def __le__(self, *args): |
| 111 | return True |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 112 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 113 | @trackCall |
| 114 | def __gt__(self, *args): |
| 115 | return False |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 116 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 117 | @trackCall |
| 118 | def __ge__(self, *args): |
| 119 | return True |
| 120 | """ |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 121 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 122 | # Synthesize all the other AllTests methods from the names in testmeths. |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 123 | |
| 124 | method_template = """\ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 125 | @trackCall |
| 126 | def __%s__(self, *args): |
| 127 | pass |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 128 | """ |
| 129 | |
Thomas Wouters | 4cdada9 | 2006-04-15 09:19:16 +0000 | [diff] [blame] | 130 | d = {} |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 131 | exec(statictests, globals(), d) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 132 | for method in testmeths: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 133 | exec(method_template % method, globals(), d) |
| 134 | AllTests = type("AllTests", (object,), d) |
| 135 | del d, statictests, method, method_template |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 136 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 137 | class ClassTests(unittest.TestCase): |
| 138 | def setUp(self): |
| 139 | callLst[:] = [] |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 140 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 141 | def assertCallStack(self, expected_calls): |
| 142 | actualCallList = callLst[:] # need to copy because the comparison below will add |
| 143 | # additional calls to callLst |
| 144 | if expected_calls != actualCallList: |
| 145 | self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" % |
| 146 | (expected_calls, actualCallList)) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 147 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 148 | def testInit(self): |
| 149 | foo = AllTests() |
| 150 | self.assertCallStack([("__init__", (foo,))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 151 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 152 | def testBinaryOps(self): |
| 153 | testme = AllTests() |
| 154 | # Binary operations |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 155 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 156 | callLst[:] = [] |
| 157 | testme + 1 |
| 158 | self.assertCallStack([("__add__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 159 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 160 | callLst[:] = [] |
| 161 | 1 + testme |
| 162 | self.assertCallStack([("__radd__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 163 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 164 | callLst[:] = [] |
| 165 | testme - 1 |
| 166 | self.assertCallStack([("__sub__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 167 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 168 | callLst[:] = [] |
| 169 | 1 - testme |
| 170 | self.assertCallStack([("__rsub__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 171 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 172 | callLst[:] = [] |
| 173 | testme * 1 |
| 174 | self.assertCallStack([("__mul__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 175 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 176 | callLst[:] = [] |
| 177 | 1 * testme |
| 178 | self.assertCallStack([("__rmul__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 179 | |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 180 | callLst[:] = [] |
Serhiy Storchaka | c2ccce7 | 2015-03-12 22:01:30 +0200 | [diff] [blame] | 181 | testme @ 1 |
| 182 | self.assertCallStack([("__matmul__", (testme, 1))]) |
| 183 | |
| 184 | callLst[:] = [] |
| 185 | 1 @ testme |
| 186 | self.assertCallStack([("__rmatmul__", (testme, 1))]) |
| 187 | |
| 188 | callLst[:] = [] |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 189 | testme / 1 |
| 190 | self.assertCallStack([("__truediv__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 191 | |
| 192 | |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 193 | callLst[:] = [] |
| 194 | 1 / testme |
| 195 | self.assertCallStack([("__rtruediv__", (testme, 1))]) |
| 196 | |
| 197 | callLst[:] = [] |
| 198 | testme // 1 |
| 199 | self.assertCallStack([("__floordiv__", (testme, 1))]) |
| 200 | |
| 201 | |
| 202 | callLst[:] = [] |
| 203 | 1 // testme |
| 204 | self.assertCallStack([("__rfloordiv__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 205 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 206 | callLst[:] = [] |
| 207 | testme % 1 |
| 208 | self.assertCallStack([("__mod__", (testme, 1))]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 209 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 210 | callLst[:] = [] |
| 211 | 1 % testme |
| 212 | self.assertCallStack([("__rmod__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 213 | |
| 214 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 215 | callLst[:] = [] |
| 216 | divmod(testme,1) |
| 217 | self.assertCallStack([("__divmod__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 218 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 219 | callLst[:] = [] |
| 220 | divmod(1, testme) |
| 221 | self.assertCallStack([("__rdivmod__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 222 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 223 | callLst[:] = [] |
| 224 | testme ** 1 |
| 225 | self.assertCallStack([("__pow__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 226 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 227 | callLst[:] = [] |
| 228 | 1 ** testme |
| 229 | self.assertCallStack([("__rpow__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 230 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 231 | callLst[:] = [] |
| 232 | testme >> 1 |
| 233 | self.assertCallStack([("__rshift__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 234 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 235 | callLst[:] = [] |
| 236 | 1 >> testme |
| 237 | self.assertCallStack([("__rrshift__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 238 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 239 | callLst[:] = [] |
| 240 | testme << 1 |
| 241 | self.assertCallStack([("__lshift__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 242 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 243 | callLst[:] = [] |
| 244 | 1 << testme |
| 245 | self.assertCallStack([("__rlshift__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 246 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 247 | callLst[:] = [] |
| 248 | testme & 1 |
| 249 | self.assertCallStack([("__and__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 250 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 251 | callLst[:] = [] |
| 252 | 1 & testme |
| 253 | self.assertCallStack([("__rand__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 254 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 255 | callLst[:] = [] |
| 256 | testme | 1 |
| 257 | self.assertCallStack([("__or__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 258 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 259 | callLst[:] = [] |
| 260 | 1 | testme |
| 261 | self.assertCallStack([("__ror__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 262 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 263 | callLst[:] = [] |
| 264 | testme ^ 1 |
| 265 | self.assertCallStack([("__xor__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 266 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 267 | callLst[:] = [] |
| 268 | 1 ^ testme |
| 269 | self.assertCallStack([("__rxor__", (testme, 1))]) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 270 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 271 | def testListAndDictOps(self): |
| 272 | testme = AllTests() |
| 273 | |
| 274 | # List/dict operations |
| 275 | |
| 276 | class Empty: pass |
| 277 | |
| 278 | try: |
| 279 | 1 in Empty() |
| 280 | self.fail('failed, should have raised TypeError') |
| 281 | except TypeError: |
| 282 | pass |
| 283 | |
| 284 | callLst[:] = [] |
| 285 | 1 in testme |
| 286 | self.assertCallStack([('__contains__', (testme, 1))]) |
| 287 | |
| 288 | callLst[:] = [] |
| 289 | testme[1] |
| 290 | self.assertCallStack([('__getitem__', (testme, 1))]) |
| 291 | |
| 292 | callLst[:] = [] |
| 293 | testme[1] = 1 |
| 294 | self.assertCallStack([('__setitem__', (testme, 1, 1))]) |
| 295 | |
| 296 | callLst[:] = [] |
| 297 | del testme[1] |
| 298 | self.assertCallStack([('__delitem__', (testme, 1))]) |
| 299 | |
| 300 | callLst[:] = [] |
| 301 | testme[:42] |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 302 | self.assertCallStack([('__getitem__', (testme, slice(None, 42)))]) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 303 | |
| 304 | callLst[:] = [] |
| 305 | testme[:42] = "The Answer" |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 306 | self.assertCallStack([('__setitem__', (testme, slice(None, 42), |
| 307 | "The Answer"))]) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 308 | |
| 309 | callLst[:] = [] |
| 310 | del testme[:42] |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 311 | self.assertCallStack([('__delitem__', (testme, slice(None, 42)))]) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 312 | |
| 313 | callLst[:] = [] |
| 314 | testme[2:1024:10] |
| 315 | self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))]) |
| 316 | |
| 317 | callLst[:] = [] |
| 318 | testme[2:1024:10] = "A lot" |
| 319 | self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10), |
| 320 | "A lot"))]) |
| 321 | callLst[:] = [] |
| 322 | del testme[2:1024:10] |
| 323 | self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))]) |
| 324 | |
| 325 | callLst[:] = [] |
| 326 | testme[:42, ..., :24:, 24, 100] |
| 327 | self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None), |
| 328 | Ellipsis, |
| 329 | slice(None, 24, None), |
| 330 | 24, 100)))]) |
| 331 | callLst[:] = [] |
| 332 | testme[:42, ..., :24:, 24, 100] = "Strange" |
| 333 | self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None), |
| 334 | Ellipsis, |
| 335 | slice(None, 24, None), |
| 336 | 24, 100), "Strange"))]) |
| 337 | callLst[:] = [] |
| 338 | del testme[:42, ..., :24:, 24, 100] |
| 339 | self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None), |
| 340 | Ellipsis, |
| 341 | slice(None, 24, None), |
| 342 | 24, 100)))]) |
| 343 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 344 | def testUnaryOps(self): |
| 345 | testme = AllTests() |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 346 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 347 | callLst[:] = [] |
| 348 | -testme |
| 349 | self.assertCallStack([('__neg__', (testme,))]) |
| 350 | callLst[:] = [] |
| 351 | +testme |
| 352 | self.assertCallStack([('__pos__', (testme,))]) |
| 353 | callLst[:] = [] |
| 354 | abs(testme) |
| 355 | self.assertCallStack([('__abs__', (testme,))]) |
| 356 | callLst[:] = [] |
| 357 | int(testme) |
| 358 | self.assertCallStack([('__int__', (testme,))]) |
| 359 | callLst[:] = [] |
| 360 | float(testme) |
| 361 | self.assertCallStack([('__float__', (testme,))]) |
| 362 | callLst[:] = [] |
| 363 | oct(testme) |
| 364 | self.assertCallStack([('__index__', (testme,))]) |
| 365 | callLst[:] = [] |
| 366 | hex(testme) |
| 367 | self.assertCallStack([('__index__', (testme,))]) |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 368 | |
| 369 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 370 | def testMisc(self): |
| 371 | testme = AllTests() |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 372 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 373 | callLst[:] = [] |
| 374 | hash(testme) |
| 375 | self.assertCallStack([('__hash__', (testme,))]) |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 376 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 377 | callLst[:] = [] |
| 378 | repr(testme) |
| 379 | self.assertCallStack([('__repr__', (testme,))]) |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 380 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 381 | callLst[:] = [] |
| 382 | str(testme) |
| 383 | self.assertCallStack([('__str__', (testme,))]) |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 384 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 385 | callLst[:] = [] |
| 386 | testme == 1 |
| 387 | self.assertCallStack([('__eq__', (testme, 1))]) |
| 388 | |
| 389 | callLst[:] = [] |
| 390 | testme < 1 |
| 391 | self.assertCallStack([('__lt__', (testme, 1))]) |
| 392 | |
| 393 | callLst[:] = [] |
| 394 | testme > 1 |
| 395 | self.assertCallStack([('__gt__', (testme, 1))]) |
| 396 | |
| 397 | callLst[:] = [] |
| 398 | testme != 1 |
| 399 | self.assertCallStack([('__ne__', (testme, 1))]) |
| 400 | |
| 401 | callLst[:] = [] |
| 402 | 1 == testme |
| 403 | self.assertCallStack([('__eq__', (1, testme))]) |
| 404 | |
| 405 | callLst[:] = [] |
| 406 | 1 < testme |
| 407 | self.assertCallStack([('__gt__', (1, testme))]) |
| 408 | |
| 409 | callLst[:] = [] |
| 410 | 1 > testme |
| 411 | self.assertCallStack([('__lt__', (1, testme))]) |
| 412 | |
| 413 | callLst[:] = [] |
| 414 | 1 != testme |
| 415 | self.assertCallStack([('__ne__', (1, testme))]) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 416 | |
| 417 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 418 | def testGetSetAndDel(self): |
| 419 | # Interfering tests |
| 420 | class ExtraTests(AllTests): |
| 421 | @trackCall |
| 422 | def __getattr__(self, *args): |
| 423 | return "SomeVal" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 424 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 425 | @trackCall |
| 426 | def __setattr__(self, *args): |
| 427 | pass |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 428 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 429 | @trackCall |
| 430 | def __delattr__(self, *args): |
| 431 | pass |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 432 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 433 | testme = ExtraTests() |
| 434 | |
| 435 | callLst[:] = [] |
| 436 | testme.spam |
| 437 | self.assertCallStack([('__getattr__', (testme, "spam"))]) |
| 438 | |
| 439 | callLst[:] = [] |
| 440 | testme.eggs = "spam, spam, spam and ham" |
| 441 | self.assertCallStack([('__setattr__', (testme, "eggs", |
| 442 | "spam, spam, spam and ham"))]) |
| 443 | |
| 444 | callLst[:] = [] |
| 445 | del testme.cardinal |
| 446 | self.assertCallStack([('__delattr__', (testme, "cardinal"))]) |
| 447 | |
| 448 | def testDel(self): |
| 449 | x = [] |
| 450 | |
| 451 | class DelTest: |
| 452 | def __del__(self): |
| 453 | x.append("crab people, crab people") |
| 454 | testme = DelTest() |
| 455 | del testme |
| 456 | import gc |
| 457 | gc.collect() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 458 | self.assertEqual(["crab people, crab people"], x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 459 | |
| 460 | def testBadTypeReturned(self): |
| 461 | # return values of some method are type-checked |
| 462 | class BadTypeClass: |
| 463 | def __int__(self): |
| 464 | return None |
| 465 | __float__ = __int__ |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 466 | __complex__ = __int__ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 467 | __str__ = __int__ |
| 468 | __repr__ = __int__ |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 469 | __bytes__ = __int__ |
| 470 | __bool__ = __int__ |
| 471 | __index__ = __int__ |
| 472 | def index(x): |
| 473 | return [][x] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 474 | |
Serhiy Storchaka | a60c2fe | 2015-03-12 21:56:08 +0200 | [diff] [blame] | 475 | for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 476 | self.assertRaises(TypeError, f, BadTypeClass()) |
| 477 | |
| 478 | def testHashStuff(self): |
| 479 | # Test correct errors from hash() on objects with comparisons but |
| 480 | # no __hash__ |
| 481 | |
| 482 | class C0: |
| 483 | pass |
| 484 | |
| 485 | hash(C0()) # This should work; the next two should raise TypeError |
| 486 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 487 | class C2: |
| 488 | def __eq__(self, other): return 1 |
| 489 | |
| 490 | self.assertRaises(TypeError, hash, C2()) |
| 491 | |
| 492 | |
| 493 | def testSFBug532646(self): |
| 494 | # Test for SF bug 532646 |
| 495 | |
| 496 | class A: |
| 497 | pass |
| 498 | A.__call__ = A() |
| 499 | a = A() |
| 500 | |
| 501 | try: |
| 502 | a() # This should not segfault |
| 503 | except RuntimeError: |
| 504 | pass |
| 505 | else: |
| 506 | self.fail("Failed to raise RuntimeError") |
| 507 | |
| 508 | def testForExceptionsRaisedInInstanceGetattr2(self): |
| 509 | # Tests for exceptions raised in instance_getattr2(). |
| 510 | |
| 511 | def booh(self): |
| 512 | raise AttributeError("booh") |
| 513 | |
| 514 | class A: |
| 515 | a = property(booh) |
| 516 | try: |
| 517 | A().a # Raised AttributeError: A instance has no attribute 'a' |
| 518 | except AttributeError as x: |
| 519 | if str(x) != "booh": |
| 520 | self.fail("attribute error for A().a got masked: %s" % x) |
| 521 | |
| 522 | class E: |
| 523 | __eq__ = property(booh) |
| 524 | E() == E() # In debug mode, caused a C-level assert() to fail |
| 525 | |
| 526 | class I: |
| 527 | __init__ = property(booh) |
| 528 | try: |
| 529 | # In debug mode, printed XXX undetected error and |
| 530 | # raises AttributeError |
| 531 | I() |
| 532 | except AttributeError as x: |
| 533 | pass |
| 534 | else: |
| 535 | self.fail("attribute error for I.__init__ got masked") |
| 536 | |
| 537 | def testHashComparisonOfMethods(self): |
| 538 | # Test comparison and hash of methods |
| 539 | class A: |
| 540 | def __init__(self, x): |
| 541 | self.x = x |
| 542 | def f(self): |
| 543 | pass |
| 544 | def g(self): |
| 545 | pass |
| 546 | def __eq__(self, other): |
| 547 | return self.x == other.x |
| 548 | def __hash__(self): |
| 549 | return self.x |
| 550 | class B(A): |
| 551 | pass |
| 552 | |
| 553 | a1 = A(1) |
| 554 | a2 = A(2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 555 | self.assertEqual(a1.f, a1.f) |
| 556 | self.assertNotEqual(a1.f, a2.f) |
| 557 | self.assertNotEqual(a1.f, a1.g) |
| 558 | self.assertEqual(a1.f, A(1).f) |
| 559 | self.assertEqual(hash(a1.f), hash(a1.f)) |
| 560 | self.assertEqual(hash(a1.f), hash(A(1).f)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 561 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 562 | self.assertNotEqual(A.f, a1.f) |
| 563 | self.assertNotEqual(A.f, A.g) |
| 564 | self.assertEqual(B.f, A.f) |
| 565 | self.assertEqual(hash(B.f), hash(A.f)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 566 | |
| 567 | # the following triggers a SystemError in 2.4 |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 568 | a = A(hash(A.f)^(-1)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 569 | hash(a.f) |
| 570 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 571 | if __name__ == '__main__': |
| 572 | unittest.main() |