# Python code coverage for Lib/test/test_operator.py

# | count | content |
---|---|---|

1 | n/a | import unittest |

2 | n/a | import pickle |

3 | n/a | import sys |

4 | n/a | |

5 | n/a | from test import support |

6 | n/a | |

7 | n/a | py_operator = support.import_fresh_module('operator', blocked=['_operator']) |

8 | n/a | c_operator = support.import_fresh_module('operator', fresh=['_operator']) |

9 | n/a | |

10 | n/a | class Seq1: |

11 | n/a | def __init__(self, lst): |

12 | n/a | self.lst = lst |

13 | n/a | def __len__(self): |

14 | n/a | return len(self.lst) |

15 | n/a | def __getitem__(self, i): |

16 | n/a | return self.lst[i] |

17 | n/a | def __add__(self, other): |

18 | n/a | return self.lst + other.lst |

19 | n/a | def __mul__(self, other): |

20 | n/a | return self.lst * other |

21 | n/a | def __rmul__(self, other): |

22 | n/a | return other * self.lst |

23 | n/a | |

24 | n/a | class Seq2(object): |

25 | n/a | def __init__(self, lst): |

26 | n/a | self.lst = lst |

27 | n/a | def __len__(self): |

28 | n/a | return len(self.lst) |

29 | n/a | def __getitem__(self, i): |

30 | n/a | return self.lst[i] |

31 | n/a | def __add__(self, other): |

32 | n/a | return self.lst + other.lst |

33 | n/a | def __mul__(self, other): |

34 | n/a | return self.lst * other |

35 | n/a | def __rmul__(self, other): |

36 | n/a | return other * self.lst |

37 | n/a | |

38 | n/a | |

39 | n/a | class OperatorTestCase: |

40 | n/a | def test_lt(self): |

41 | n/a | operator = self.module |

42 | n/a | self.assertRaises(TypeError, operator.lt) |

43 | n/a | self.assertRaises(TypeError, operator.lt, 1j, 2j) |

44 | n/a | self.assertFalse(operator.lt(1, 0)) |

45 | n/a | self.assertFalse(operator.lt(1, 0.0)) |

46 | n/a | self.assertFalse(operator.lt(1, 1)) |

47 | n/a | self.assertFalse(operator.lt(1, 1.0)) |

48 | n/a | self.assertTrue(operator.lt(1, 2)) |

49 | n/a | self.assertTrue(operator.lt(1, 2.0)) |

50 | n/a | |

51 | n/a | def test_le(self): |

52 | n/a | operator = self.module |

53 | n/a | self.assertRaises(TypeError, operator.le) |

54 | n/a | self.assertRaises(TypeError, operator.le, 1j, 2j) |

55 | n/a | self.assertFalse(operator.le(1, 0)) |

56 | n/a | self.assertFalse(operator.le(1, 0.0)) |

57 | n/a | self.assertTrue(operator.le(1, 1)) |

58 | n/a | self.assertTrue(operator.le(1, 1.0)) |

59 | n/a | self.assertTrue(operator.le(1, 2)) |

60 | n/a | self.assertTrue(operator.le(1, 2.0)) |

61 | n/a | |

62 | n/a | def test_eq(self): |

63 | n/a | operator = self.module |

64 | n/a | class C(object): |

65 | n/a | def __eq__(self, other): |

66 | n/a | raise SyntaxError |

67 | n/a | self.assertRaises(TypeError, operator.eq) |

68 | n/a | self.assertRaises(SyntaxError, operator.eq, C(), C()) |

69 | n/a | self.assertFalse(operator.eq(1, 0)) |

70 | n/a | self.assertFalse(operator.eq(1, 0.0)) |

71 | n/a | self.assertTrue(operator.eq(1, 1)) |

72 | n/a | self.assertTrue(operator.eq(1, 1.0)) |

73 | n/a | self.assertFalse(operator.eq(1, 2)) |

74 | n/a | self.assertFalse(operator.eq(1, 2.0)) |

75 | n/a | |

76 | n/a | def test_ne(self): |

77 | n/a | operator = self.module |

78 | n/a | class C(object): |

79 | n/a | def __ne__(self, other): |

80 | n/a | raise SyntaxError |

81 | n/a | self.assertRaises(TypeError, operator.ne) |

82 | n/a | self.assertRaises(SyntaxError, operator.ne, C(), C()) |

83 | n/a | self.assertTrue(operator.ne(1, 0)) |

84 | n/a | self.assertTrue(operator.ne(1, 0.0)) |

85 | n/a | self.assertFalse(operator.ne(1, 1)) |

86 | n/a | self.assertFalse(operator.ne(1, 1.0)) |

87 | n/a | self.assertTrue(operator.ne(1, 2)) |

88 | n/a | self.assertTrue(operator.ne(1, 2.0)) |

89 | n/a | |

90 | n/a | def test_ge(self): |

91 | n/a | operator = self.module |

92 | n/a | self.assertRaises(TypeError, operator.ge) |

93 | n/a | self.assertRaises(TypeError, operator.ge, 1j, 2j) |

94 | n/a | self.assertTrue(operator.ge(1, 0)) |

95 | n/a | self.assertTrue(operator.ge(1, 0.0)) |

96 | n/a | self.assertTrue(operator.ge(1, 1)) |

97 | n/a | self.assertTrue(operator.ge(1, 1.0)) |

98 | n/a | self.assertFalse(operator.ge(1, 2)) |

99 | n/a | self.assertFalse(operator.ge(1, 2.0)) |

100 | n/a | |

101 | n/a | def test_gt(self): |

102 | n/a | operator = self.module |

103 | n/a | self.assertRaises(TypeError, operator.gt) |

104 | n/a | self.assertRaises(TypeError, operator.gt, 1j, 2j) |

105 | n/a | self.assertTrue(operator.gt(1, 0)) |

106 | n/a | self.assertTrue(operator.gt(1, 0.0)) |

107 | n/a | self.assertFalse(operator.gt(1, 1)) |

108 | n/a | self.assertFalse(operator.gt(1, 1.0)) |

109 | n/a | self.assertFalse(operator.gt(1, 2)) |

110 | n/a | self.assertFalse(operator.gt(1, 2.0)) |

111 | n/a | |

112 | n/a | def test_abs(self): |

113 | n/a | operator = self.module |

114 | n/a | self.assertRaises(TypeError, operator.abs) |

115 | n/a | self.assertRaises(TypeError, operator.abs, None) |

116 | n/a | self.assertEqual(operator.abs(-1), 1) |

117 | n/a | self.assertEqual(operator.abs(1), 1) |

118 | n/a | |

119 | n/a | def test_add(self): |

120 | n/a | operator = self.module |

121 | n/a | self.assertRaises(TypeError, operator.add) |

122 | n/a | self.assertRaises(TypeError, operator.add, None, None) |

123 | n/a | self.assertEqual(operator.add(3, 4), 7) |

124 | n/a | |

125 | n/a | def test_bitwise_and(self): |

126 | n/a | operator = self.module |

127 | n/a | self.assertRaises(TypeError, operator.and_) |

128 | n/a | self.assertRaises(TypeError, operator.and_, None, None) |

129 | n/a | self.assertEqual(operator.and_(0xf, 0xa), 0xa) |

130 | n/a | |

131 | n/a | def test_concat(self): |

132 | n/a | operator = self.module |

133 | n/a | self.assertRaises(TypeError, operator.concat) |

134 | n/a | self.assertRaises(TypeError, operator.concat, None, None) |

135 | n/a | self.assertEqual(operator.concat('py', 'thon'), 'python') |

136 | n/a | self.assertEqual(operator.concat([1, 2], [3, 4]), [1, 2, 3, 4]) |

137 | n/a | self.assertEqual(operator.concat(Seq1([5, 6]), Seq1([7])), [5, 6, 7]) |

138 | n/a | self.assertEqual(operator.concat(Seq2([5, 6]), Seq2([7])), [5, 6, 7]) |

139 | n/a | self.assertRaises(TypeError, operator.concat, 13, 29) |

140 | n/a | |

141 | n/a | def test_countOf(self): |

142 | n/a | operator = self.module |

143 | n/a | self.assertRaises(TypeError, operator.countOf) |

144 | n/a | self.assertRaises(TypeError, operator.countOf, None, None) |

145 | n/a | self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1) |

146 | n/a | self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0) |

147 | n/a | |

148 | n/a | def test_delitem(self): |

149 | n/a | operator = self.module |

150 | n/a | a = [4, 3, 2, 1] |

151 | n/a | self.assertRaises(TypeError, operator.delitem, a) |

152 | n/a | self.assertRaises(TypeError, operator.delitem, a, None) |

153 | n/a | self.assertIsNone(operator.delitem(a, 1)) |

154 | n/a | self.assertEqual(a, [4, 2, 1]) |

155 | n/a | |

156 | n/a | def test_floordiv(self): |

157 | n/a | operator = self.module |

158 | n/a | self.assertRaises(TypeError, operator.floordiv, 5) |

159 | n/a | self.assertRaises(TypeError, operator.floordiv, None, None) |

160 | n/a | self.assertEqual(operator.floordiv(5, 2), 2) |

161 | n/a | |

162 | n/a | def test_truediv(self): |

163 | n/a | operator = self.module |

164 | n/a | self.assertRaises(TypeError, operator.truediv, 5) |

165 | n/a | self.assertRaises(TypeError, operator.truediv, None, None) |

166 | n/a | self.assertEqual(operator.truediv(5, 2), 2.5) |

167 | n/a | |

168 | n/a | def test_getitem(self): |

169 | n/a | operator = self.module |

170 | n/a | a = range(10) |

171 | n/a | self.assertRaises(TypeError, operator.getitem) |

172 | n/a | self.assertRaises(TypeError, operator.getitem, a, None) |

173 | n/a | self.assertEqual(operator.getitem(a, 2), 2) |

174 | n/a | |

175 | n/a | def test_indexOf(self): |

176 | n/a | operator = self.module |

177 | n/a | self.assertRaises(TypeError, operator.indexOf) |

178 | n/a | self.assertRaises(TypeError, operator.indexOf, None, None) |

179 | n/a | self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1) |

180 | n/a | self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0) |

181 | n/a | |

182 | n/a | def test_invert(self): |

183 | n/a | operator = self.module |

184 | n/a | self.assertRaises(TypeError, operator.invert) |

185 | n/a | self.assertRaises(TypeError, operator.invert, None) |

186 | n/a | self.assertEqual(operator.inv(4), -5) |

187 | n/a | |

188 | n/a | def test_lshift(self): |

189 | n/a | operator = self.module |

190 | n/a | self.assertRaises(TypeError, operator.lshift) |

191 | n/a | self.assertRaises(TypeError, operator.lshift, None, 42) |

192 | n/a | self.assertEqual(operator.lshift(5, 1), 10) |

193 | n/a | self.assertEqual(operator.lshift(5, 0), 5) |

194 | n/a | self.assertRaises(ValueError, operator.lshift, 2, -1) |

195 | n/a | |

196 | n/a | def test_mod(self): |

197 | n/a | operator = self.module |

198 | n/a | self.assertRaises(TypeError, operator.mod) |

199 | n/a | self.assertRaises(TypeError, operator.mod, None, 42) |

200 | n/a | self.assertEqual(operator.mod(5, 2), 1) |

201 | n/a | |

202 | n/a | def test_mul(self): |

203 | n/a | operator = self.module |

204 | n/a | self.assertRaises(TypeError, operator.mul) |

205 | n/a | self.assertRaises(TypeError, operator.mul, None, None) |

206 | n/a | self.assertEqual(operator.mul(5, 2), 10) |

207 | n/a | |

208 | n/a | def test_matmul(self): |

209 | n/a | operator = self.module |

210 | n/a | self.assertRaises(TypeError, operator.matmul) |

211 | n/a | self.assertRaises(TypeError, operator.matmul, 42, 42) |

212 | n/a | class M: |

213 | n/a | def __matmul__(self, other): |

214 | n/a | return other - 1 |

215 | n/a | self.assertEqual(M() @ 42, 41) |

216 | n/a | |

217 | n/a | def test_neg(self): |

218 | n/a | operator = self.module |

219 | n/a | self.assertRaises(TypeError, operator.neg) |

220 | n/a | self.assertRaises(TypeError, operator.neg, None) |

221 | n/a | self.assertEqual(operator.neg(5), -5) |

222 | n/a | self.assertEqual(operator.neg(-5), 5) |

223 | n/a | self.assertEqual(operator.neg(0), 0) |

224 | n/a | self.assertEqual(operator.neg(-0), 0) |

225 | n/a | |

226 | n/a | def test_bitwise_or(self): |

227 | n/a | operator = self.module |

228 | n/a | self.assertRaises(TypeError, operator.or_) |

229 | n/a | self.assertRaises(TypeError, operator.or_, None, None) |

230 | n/a | self.assertEqual(operator.or_(0xa, 0x5), 0xf) |

231 | n/a | |

232 | n/a | def test_pos(self): |

233 | n/a | operator = self.module |

234 | n/a | self.assertRaises(TypeError, operator.pos) |

235 | n/a | self.assertRaises(TypeError, operator.pos, None) |

236 | n/a | self.assertEqual(operator.pos(5), 5) |

237 | n/a | self.assertEqual(operator.pos(-5), -5) |

238 | n/a | self.assertEqual(operator.pos(0), 0) |

239 | n/a | self.assertEqual(operator.pos(-0), 0) |

240 | n/a | |

241 | n/a | def test_pow(self): |

242 | n/a | operator = self.module |

243 | n/a | self.assertRaises(TypeError, operator.pow) |

244 | n/a | self.assertRaises(TypeError, operator.pow, None, None) |

245 | n/a | self.assertEqual(operator.pow(3,5), 3**5) |

246 | n/a | self.assertRaises(TypeError, operator.pow, 1) |

247 | n/a | self.assertRaises(TypeError, operator.pow, 1, 2, 3) |

248 | n/a | |

249 | n/a | def test_rshift(self): |

250 | n/a | operator = self.module |

251 | n/a | self.assertRaises(TypeError, operator.rshift) |

252 | n/a | self.assertRaises(TypeError, operator.rshift, None, 42) |

253 | n/a | self.assertEqual(operator.rshift(5, 1), 2) |

254 | n/a | self.assertEqual(operator.rshift(5, 0), 5) |

255 | n/a | self.assertRaises(ValueError, operator.rshift, 2, -1) |

256 | n/a | |

257 | n/a | def test_contains(self): |

258 | n/a | operator = self.module |

259 | n/a | self.assertRaises(TypeError, operator.contains) |

260 | n/a | self.assertRaises(TypeError, operator.contains, None, None) |

261 | n/a | self.assertTrue(operator.contains(range(4), 2)) |

262 | n/a | self.assertFalse(operator.contains(range(4), 5)) |

263 | n/a | |

264 | n/a | def test_setitem(self): |

265 | n/a | operator = self.module |

266 | n/a | a = list(range(3)) |

267 | n/a | self.assertRaises(TypeError, operator.setitem, a) |

268 | n/a | self.assertRaises(TypeError, operator.setitem, a, None, None) |

269 | n/a | self.assertIsNone(operator.setitem(a, 0, 2)) |

270 | n/a | self.assertEqual(a, [2, 1, 2]) |

271 | n/a | self.assertRaises(IndexError, operator.setitem, a, 4, 2) |

272 | n/a | |

273 | n/a | def test_sub(self): |

274 | n/a | operator = self.module |

275 | n/a | self.assertRaises(TypeError, operator.sub) |

276 | n/a | self.assertRaises(TypeError, operator.sub, None, None) |

277 | n/a | self.assertEqual(operator.sub(5, 2), 3) |

278 | n/a | |

279 | n/a | def test_truth(self): |

280 | n/a | operator = self.module |

281 | n/a | class C(object): |

282 | n/a | def __bool__(self): |

283 | n/a | raise SyntaxError |

284 | n/a | self.assertRaises(TypeError, operator.truth) |

285 | n/a | self.assertRaises(SyntaxError, operator.truth, C()) |

286 | n/a | self.assertTrue(operator.truth(5)) |

287 | n/a | self.assertTrue(operator.truth([0])) |

288 | n/a | self.assertFalse(operator.truth(0)) |

289 | n/a | self.assertFalse(operator.truth([])) |

290 | n/a | |

291 | n/a | def test_bitwise_xor(self): |

292 | n/a | operator = self.module |

293 | n/a | self.assertRaises(TypeError, operator.xor) |

294 | n/a | self.assertRaises(TypeError, operator.xor, None, None) |

295 | n/a | self.assertEqual(operator.xor(0xb, 0xc), 0x7) |

296 | n/a | |

297 | n/a | def test_is(self): |

298 | n/a | operator = self.module |

299 | n/a | a = b = 'xyzpdq' |

300 | n/a | c = a[:3] + b[3:] |

301 | n/a | self.assertRaises(TypeError, operator.is_) |

302 | n/a | self.assertTrue(operator.is_(a, b)) |

303 | n/a | self.assertFalse(operator.is_(a,c)) |

304 | n/a | |

305 | n/a | def test_is_not(self): |

306 | n/a | operator = self.module |

307 | n/a | a = b = 'xyzpdq' |

308 | n/a | c = a[:3] + b[3:] |

309 | n/a | self.assertRaises(TypeError, operator.is_not) |

310 | n/a | self.assertFalse(operator.is_not(a, b)) |

311 | n/a | self.assertTrue(operator.is_not(a,c)) |

312 | n/a | |

313 | n/a | def test_attrgetter(self): |

314 | n/a | operator = self.module |

315 | n/a | class A: |

316 | n/a | pass |

317 | n/a | a = A() |

318 | n/a | a.name = 'arthur' |

319 | n/a | f = operator.attrgetter('name') |

320 | n/a | self.assertEqual(f(a), 'arthur') |

321 | n/a | self.assertRaises(TypeError, f) |

322 | n/a | self.assertRaises(TypeError, f, a, 'dent') |

323 | n/a | self.assertRaises(TypeError, f, a, surname='dent') |

324 | n/a | f = operator.attrgetter('rank') |

325 | n/a | self.assertRaises(AttributeError, f, a) |

326 | n/a | self.assertRaises(TypeError, operator.attrgetter, 2) |

327 | n/a | self.assertRaises(TypeError, operator.attrgetter) |

328 | n/a | |

329 | n/a | # multiple gets |

330 | n/a | record = A() |

331 | n/a | record.x = 'X' |

332 | n/a | record.y = 'Y' |

333 | n/a | record.z = 'Z' |

334 | n/a | self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y')) |

335 | n/a | self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y')) |

336 | n/a | |

337 | n/a | class C(object): |

338 | n/a | def __getattr__(self, name): |

339 | n/a | raise SyntaxError |

340 | n/a | self.assertRaises(SyntaxError, operator.attrgetter('foo'), C()) |

341 | n/a | |

342 | n/a | # recursive gets |

343 | n/a | a = A() |

344 | n/a | a.name = 'arthur' |

345 | n/a | a.child = A() |

346 | n/a | a.child.name = 'thomas' |

347 | n/a | f = operator.attrgetter('child.name') |

348 | n/a | self.assertEqual(f(a), 'thomas') |

349 | n/a | self.assertRaises(AttributeError, f, a.child) |

350 | n/a | f = operator.attrgetter('name', 'child.name') |

351 | n/a | self.assertEqual(f(a), ('arthur', 'thomas')) |

352 | n/a | f = operator.attrgetter('name', 'child.name', 'child.child.name') |

353 | n/a | self.assertRaises(AttributeError, f, a) |

354 | n/a | f = operator.attrgetter('child.') |

355 | n/a | self.assertRaises(AttributeError, f, a) |

356 | n/a | f = operator.attrgetter('.child') |

357 | n/a | self.assertRaises(AttributeError, f, a) |

358 | n/a | |

359 | n/a | a.child.child = A() |

360 | n/a | a.child.child.name = 'johnson' |

361 | n/a | f = operator.attrgetter('child.child.name') |

362 | n/a | self.assertEqual(f(a), 'johnson') |

363 | n/a | f = operator.attrgetter('name', 'child.name', 'child.child.name') |

364 | n/a | self.assertEqual(f(a), ('arthur', 'thomas', 'johnson')) |

365 | n/a | |

366 | n/a | def test_itemgetter(self): |

367 | n/a | operator = self.module |

368 | n/a | a = 'ABCDE' |

369 | n/a | f = operator.itemgetter(2) |

370 | n/a | self.assertEqual(f(a), 'C') |

371 | n/a | self.assertRaises(TypeError, f) |

372 | n/a | self.assertRaises(TypeError, f, a, 3) |

373 | n/a | self.assertRaises(TypeError, f, a, size=3) |

374 | n/a | f = operator.itemgetter(10) |

375 | n/a | self.assertRaises(IndexError, f, a) |

376 | n/a | |

377 | n/a | class C(object): |

378 | n/a | def __getitem__(self, name): |

379 | n/a | raise SyntaxError |

380 | n/a | self.assertRaises(SyntaxError, operator.itemgetter(42), C()) |

381 | n/a | |

382 | n/a | f = operator.itemgetter('name') |

383 | n/a | self.assertRaises(TypeError, f, a) |

384 | n/a | self.assertRaises(TypeError, operator.itemgetter) |

385 | n/a | |

386 | n/a | d = dict(key='val') |

387 | n/a | f = operator.itemgetter('key') |

388 | n/a | self.assertEqual(f(d), 'val') |

389 | n/a | f = operator.itemgetter('nonkey') |

390 | n/a | self.assertRaises(KeyError, f, d) |

391 | n/a | |

392 | n/a | # example used in the docs |

393 | n/a | inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |

394 | n/a | getcount = operator.itemgetter(1) |

395 | n/a | self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1]) |

396 | n/a | self.assertEqual(sorted(inventory, key=getcount), |

397 | n/a | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]) |

398 | n/a | |

399 | n/a | # multiple gets |

400 | n/a | data = list(map(str, range(20))) |

401 | n/a | self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5')) |

402 | n/a | self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data) |

403 | n/a | |

404 | n/a | def test_methodcaller(self): |

405 | n/a | operator = self.module |

406 | n/a | self.assertRaises(TypeError, operator.methodcaller) |

407 | n/a | self.assertRaises(TypeError, operator.methodcaller, 12) |

408 | n/a | class A: |

409 | n/a | def foo(self, *args, **kwds): |

410 | n/a | return args[0] + args[1] |

411 | n/a | def bar(self, f=42): |

412 | n/a | return f |

413 | n/a | def baz(*args, **kwds): |

414 | n/a | return kwds['name'], kwds['self'] |

415 | n/a | a = A() |

416 | n/a | f = operator.methodcaller('foo') |

417 | n/a | self.assertRaises(IndexError, f, a) |

418 | n/a | f = operator.methodcaller('foo', 1, 2) |

419 | n/a | self.assertEqual(f(a), 3) |

420 | n/a | self.assertRaises(TypeError, f) |

421 | n/a | self.assertRaises(TypeError, f, a, 3) |

422 | n/a | self.assertRaises(TypeError, f, a, spam=3) |

423 | n/a | f = operator.methodcaller('bar') |

424 | n/a | self.assertEqual(f(a), 42) |

425 | n/a | self.assertRaises(TypeError, f, a, a) |

426 | n/a | f = operator.methodcaller('bar', f=5) |

427 | n/a | self.assertEqual(f(a), 5) |

428 | n/a | f = operator.methodcaller('baz', name='spam', self='eggs') |

429 | n/a | self.assertEqual(f(a), ('spam', 'eggs')) |

430 | n/a | |

431 | n/a | def test_inplace(self): |

432 | n/a | operator = self.module |

433 | n/a | class C(object): |

434 | n/a | def __iadd__ (self, other): return "iadd" |

435 | n/a | def __iand__ (self, other): return "iand" |

436 | n/a | def __ifloordiv__(self, other): return "ifloordiv" |

437 | n/a | def __ilshift__ (self, other): return "ilshift" |

438 | n/a | def __imod__ (self, other): return "imod" |

439 | n/a | def __imul__ (self, other): return "imul" |

440 | n/a | def __imatmul__ (self, other): return "imatmul" |

441 | n/a | def __ior__ (self, other): return "ior" |

442 | n/a | def __ipow__ (self, other): return "ipow" |

443 | n/a | def __irshift__ (self, other): return "irshift" |

444 | n/a | def __isub__ (self, other): return "isub" |

445 | n/a | def __itruediv__ (self, other): return "itruediv" |

446 | n/a | def __ixor__ (self, other): return "ixor" |

447 | n/a | def __getitem__(self, other): return 5 # so that C is a sequence |

448 | n/a | c = C() |

449 | n/a | self.assertEqual(operator.iadd (c, 5), "iadd") |

450 | n/a | self.assertEqual(operator.iand (c, 5), "iand") |

451 | n/a | self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") |

452 | n/a | self.assertEqual(operator.ilshift (c, 5), "ilshift") |

453 | n/a | self.assertEqual(operator.imod (c, 5), "imod") |

454 | n/a | self.assertEqual(operator.imul (c, 5), "imul") |

455 | n/a | self.assertEqual(operator.imatmul (c, 5), "imatmul") |

456 | n/a | self.assertEqual(operator.ior (c, 5), "ior") |

457 | n/a | self.assertEqual(operator.ipow (c, 5), "ipow") |

458 | n/a | self.assertEqual(operator.irshift (c, 5), "irshift") |

459 | n/a | self.assertEqual(operator.isub (c, 5), "isub") |

460 | n/a | self.assertEqual(operator.itruediv (c, 5), "itruediv") |

461 | n/a | self.assertEqual(operator.ixor (c, 5), "ixor") |

462 | n/a | self.assertEqual(operator.iconcat (c, c), "iadd") |

463 | n/a | |

464 | n/a | def test_length_hint(self): |

465 | n/a | operator = self.module |

466 | n/a | class X(object): |

467 | n/a | def __init__(self, value): |

468 | n/a | self.value = value |

469 | n/a | |

470 | n/a | def __length_hint__(self): |

471 | n/a | if type(self.value) is type: |

472 | n/a | raise self.value |

473 | n/a | else: |

474 | n/a | return self.value |

475 | n/a | |

476 | n/a | self.assertEqual(operator.length_hint([], 2), 0) |

477 | n/a | self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3) |

478 | n/a | |

479 | n/a | self.assertEqual(operator.length_hint(X(2)), 2) |

480 | n/a | self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4) |

481 | n/a | self.assertEqual(operator.length_hint(X(TypeError), 12), 12) |

482 | n/a | with self.assertRaises(TypeError): |

483 | n/a | operator.length_hint(X("abc")) |

484 | n/a | with self.assertRaises(ValueError): |

485 | n/a | operator.length_hint(X(-2)) |

486 | n/a | with self.assertRaises(LookupError): |

487 | n/a | operator.length_hint(X(LookupError)) |

488 | n/a | |

489 | n/a | def test_dunder_is_original(self): |

490 | n/a | operator = self.module |

491 | n/a | |

492 | n/a | names = [name for name in dir(operator) if not name.startswith('_')] |

493 | n/a | for name in names: |

494 | n/a | orig = getattr(operator, name) |

495 | n/a | dunder = getattr(operator, '__' + name.strip('_') + '__', None) |

496 | n/a | if dunder: |

497 | n/a | self.assertIs(dunder, orig) |

498 | n/a | |

499 | n/a | class PyOperatorTestCase(OperatorTestCase, unittest.TestCase): |

500 | n/a | module = py_operator |

501 | n/a | |

502 | n/a | @unittest.skipUnless(c_operator, 'requires _operator') |

503 | n/a | class COperatorTestCase(OperatorTestCase, unittest.TestCase): |

504 | n/a | module = c_operator |

505 | n/a | |

506 | n/a | |

507 | n/a | class OperatorPickleTestCase: |

508 | n/a | def copy(self, obj, proto): |

509 | n/a | with support.swap_item(sys.modules, 'operator', self.module): |

510 | n/a | pickled = pickle.dumps(obj, proto) |

511 | n/a | with support.swap_item(sys.modules, 'operator', self.module2): |

512 | n/a | return pickle.loads(pickled) |

513 | n/a | |

514 | n/a | def test_attrgetter(self): |

515 | n/a | attrgetter = self.module.attrgetter |

516 | n/a | class A: |

517 | n/a | pass |

518 | n/a | a = A() |

519 | n/a | a.x = 'X' |

520 | n/a | a.y = 'Y' |

521 | n/a | a.z = 'Z' |

522 | n/a | a.t = A() |

523 | n/a | a.t.u = A() |

524 | n/a | a.t.u.v = 'V' |

525 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |

526 | n/a | with self.subTest(proto=proto): |

527 | n/a | f = attrgetter('x') |

528 | n/a | f2 = self.copy(f, proto) |

529 | n/a | self.assertEqual(repr(f2), repr(f)) |

530 | n/a | self.assertEqual(f2(a), f(a)) |

531 | n/a | # multiple gets |

532 | n/a | f = attrgetter('x', 'y', 'z') |

533 | n/a | f2 = self.copy(f, proto) |

534 | n/a | self.assertEqual(repr(f2), repr(f)) |

535 | n/a | self.assertEqual(f2(a), f(a)) |

536 | n/a | # recursive gets |

537 | n/a | f = attrgetter('t.u.v') |

538 | n/a | f2 = self.copy(f, proto) |

539 | n/a | self.assertEqual(repr(f2), repr(f)) |

540 | n/a | self.assertEqual(f2(a), f(a)) |

541 | n/a | |

542 | n/a | def test_itemgetter(self): |

543 | n/a | itemgetter = self.module.itemgetter |

544 | n/a | a = 'ABCDE' |

545 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |

546 | n/a | with self.subTest(proto=proto): |

547 | n/a | f = itemgetter(2) |

548 | n/a | f2 = self.copy(f, proto) |

549 | n/a | self.assertEqual(repr(f2), repr(f)) |

550 | n/a | self.assertEqual(f2(a), f(a)) |

551 | n/a | # multiple gets |

552 | n/a | f = itemgetter(2, 0, 4) |

553 | n/a | f2 = self.copy(f, proto) |

554 | n/a | self.assertEqual(repr(f2), repr(f)) |

555 | n/a | self.assertEqual(f2(a), f(a)) |

556 | n/a | |

557 | n/a | def test_methodcaller(self): |

558 | n/a | methodcaller = self.module.methodcaller |

559 | n/a | class A: |

560 | n/a | def foo(self, *args, **kwds): |

561 | n/a | return args[0] + args[1] |

562 | n/a | def bar(self, f=42): |

563 | n/a | return f |

564 | n/a | def baz(*args, **kwds): |

565 | n/a | return kwds['name'], kwds['self'] |

566 | n/a | a = A() |

567 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |

568 | n/a | with self.subTest(proto=proto): |

569 | n/a | f = methodcaller('bar') |

570 | n/a | f2 = self.copy(f, proto) |

571 | n/a | self.assertEqual(repr(f2), repr(f)) |

572 | n/a | self.assertEqual(f2(a), f(a)) |

573 | n/a | # positional args |

574 | n/a | f = methodcaller('foo', 1, 2) |

575 | n/a | f2 = self.copy(f, proto) |

576 | n/a | self.assertEqual(repr(f2), repr(f)) |

577 | n/a | self.assertEqual(f2(a), f(a)) |

578 | n/a | # keyword args |

579 | n/a | f = methodcaller('bar', f=5) |

580 | n/a | f2 = self.copy(f, proto) |

581 | n/a | self.assertEqual(repr(f2), repr(f)) |

582 | n/a | self.assertEqual(f2(a), f(a)) |

583 | n/a | f = methodcaller('baz', self='eggs', name='spam') |

584 | n/a | f2 = self.copy(f, proto) |

585 | n/a | # Can't test repr consistently with multiple keyword args |

586 | n/a | self.assertEqual(f2(a), f(a)) |

587 | n/a | |

588 | n/a | class PyPyOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase): |

589 | n/a | module = py_operator |

590 | n/a | module2 = py_operator |

591 | n/a | |

592 | n/a | @unittest.skipUnless(c_operator, 'requires _operator') |

593 | n/a | class PyCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase): |

594 | n/a | module = py_operator |

595 | n/a | module2 = c_operator |

596 | n/a | |

597 | n/a | @unittest.skipUnless(c_operator, 'requires _operator') |

598 | n/a | class CPyOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase): |

599 | n/a | module = c_operator |

600 | n/a | module2 = py_operator |

601 | n/a | |

602 | n/a | @unittest.skipUnless(c_operator, 'requires _operator') |

603 | n/a | class CCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase): |

604 | n/a | module = c_operator |

605 | n/a | module2 = c_operator |

606 | n/a | |

607 | n/a | |

608 | n/a | if __name__ == "__main__": |

609 | n/a | unittest.main() |