ยปCore Development>Code coverage>Lib/test/test_operator.py

Python code coverage for Lib/test/test_operator.py

#countcontent
1n/aimport unittest
2n/aimport pickle
3n/aimport sys
4n/a
5n/afrom test import support
6n/a
7n/apy_operator = support.import_fresh_module('operator', blocked=['_operator'])
8n/ac_operator = support.import_fresh_module('operator', fresh=['_operator'])
9n/a
10n/aclass Seq1:
11n/a def __init__(self, lst):
12n/a self.lst = lst
13n/a def __len__(self):
14n/a return len(self.lst)
15n/a def __getitem__(self, i):
16n/a return self.lst[i]
17n/a def __add__(self, other):
18n/a return self.lst + other.lst
19n/a def __mul__(self, other):
20n/a return self.lst * other
21n/a def __rmul__(self, other):
22n/a return other * self.lst
23n/a
24n/aclass Seq2(object):
25n/a def __init__(self, lst):
26n/a self.lst = lst
27n/a def __len__(self):
28n/a return len(self.lst)
29n/a def __getitem__(self, i):
30n/a return self.lst[i]
31n/a def __add__(self, other):
32n/a return self.lst + other.lst
33n/a def __mul__(self, other):
34n/a return self.lst * other
35n/a def __rmul__(self, other):
36n/a return other * self.lst
37n/a
38n/a
39n/aclass OperatorTestCase:
40n/a def test_lt(self):
41n/a operator = self.module
42n/a self.assertRaises(TypeError, operator.lt)
43n/a self.assertRaises(TypeError, operator.lt, 1j, 2j)
44n/a self.assertFalse(operator.lt(1, 0))
45n/a self.assertFalse(operator.lt(1, 0.0))
46n/a self.assertFalse(operator.lt(1, 1))
47n/a self.assertFalse(operator.lt(1, 1.0))
48n/a self.assertTrue(operator.lt(1, 2))
49n/a self.assertTrue(operator.lt(1, 2.0))
50n/a
51n/a def test_le(self):
52n/a operator = self.module
53n/a self.assertRaises(TypeError, operator.le)
54n/a self.assertRaises(TypeError, operator.le, 1j, 2j)
55n/a self.assertFalse(operator.le(1, 0))
56n/a self.assertFalse(operator.le(1, 0.0))
57n/a self.assertTrue(operator.le(1, 1))
58n/a self.assertTrue(operator.le(1, 1.0))
59n/a self.assertTrue(operator.le(1, 2))
60n/a self.assertTrue(operator.le(1, 2.0))
61n/a
62n/a def test_eq(self):
63n/a operator = self.module
64n/a class C(object):
65n/a def __eq__(self, other):
66n/a raise SyntaxError
67n/a self.assertRaises(TypeError, operator.eq)
68n/a self.assertRaises(SyntaxError, operator.eq, C(), C())
69n/a self.assertFalse(operator.eq(1, 0))
70n/a self.assertFalse(operator.eq(1, 0.0))
71n/a self.assertTrue(operator.eq(1, 1))
72n/a self.assertTrue(operator.eq(1, 1.0))
73n/a self.assertFalse(operator.eq(1, 2))
74n/a self.assertFalse(operator.eq(1, 2.0))
75n/a
76n/a def test_ne(self):
77n/a operator = self.module
78n/a class C(object):
79n/a def __ne__(self, other):
80n/a raise SyntaxError
81n/a self.assertRaises(TypeError, operator.ne)
82n/a self.assertRaises(SyntaxError, operator.ne, C(), C())
83n/a self.assertTrue(operator.ne(1, 0))
84n/a self.assertTrue(operator.ne(1, 0.0))
85n/a self.assertFalse(operator.ne(1, 1))
86n/a self.assertFalse(operator.ne(1, 1.0))
87n/a self.assertTrue(operator.ne(1, 2))
88n/a self.assertTrue(operator.ne(1, 2.0))
89n/a
90n/a def test_ge(self):
91n/a operator = self.module
92n/a self.assertRaises(TypeError, operator.ge)
93n/a self.assertRaises(TypeError, operator.ge, 1j, 2j)
94n/a self.assertTrue(operator.ge(1, 0))
95n/a self.assertTrue(operator.ge(1, 0.0))
96n/a self.assertTrue(operator.ge(1, 1))
97n/a self.assertTrue(operator.ge(1, 1.0))
98n/a self.assertFalse(operator.ge(1, 2))
99n/a self.assertFalse(operator.ge(1, 2.0))
100n/a
101n/a def test_gt(self):
102n/a operator = self.module
103n/a self.assertRaises(TypeError, operator.gt)
104n/a self.assertRaises(TypeError, operator.gt, 1j, 2j)
105n/a self.assertTrue(operator.gt(1, 0))
106n/a self.assertTrue(operator.gt(1, 0.0))
107n/a self.assertFalse(operator.gt(1, 1))
108n/a self.assertFalse(operator.gt(1, 1.0))
109n/a self.assertFalse(operator.gt(1, 2))
110n/a self.assertFalse(operator.gt(1, 2.0))
111n/a
112n/a def test_abs(self):
113n/a operator = self.module
114n/a self.assertRaises(TypeError, operator.abs)
115n/a self.assertRaises(TypeError, operator.abs, None)
116n/a self.assertEqual(operator.abs(-1), 1)
117n/a self.assertEqual(operator.abs(1), 1)
118n/a
119n/a def test_add(self):
120n/a operator = self.module
121n/a self.assertRaises(TypeError, operator.add)
122n/a self.assertRaises(TypeError, operator.add, None, None)
123n/a self.assertEqual(operator.add(3, 4), 7)
124n/a
125n/a def test_bitwise_and(self):
126n/a operator = self.module
127n/a self.assertRaises(TypeError, operator.and_)
128n/a self.assertRaises(TypeError, operator.and_, None, None)
129n/a self.assertEqual(operator.and_(0xf, 0xa), 0xa)
130n/a
131n/a def test_concat(self):
132n/a operator = self.module
133n/a self.assertRaises(TypeError, operator.concat)
134n/a self.assertRaises(TypeError, operator.concat, None, None)
135n/a self.assertEqual(operator.concat('py', 'thon'), 'python')
136n/a self.assertEqual(operator.concat([1, 2], [3, 4]), [1, 2, 3, 4])
137n/a self.assertEqual(operator.concat(Seq1([5, 6]), Seq1([7])), [5, 6, 7])
138n/a self.assertEqual(operator.concat(Seq2([5, 6]), Seq2([7])), [5, 6, 7])
139n/a self.assertRaises(TypeError, operator.concat, 13, 29)
140n/a
141n/a def test_countOf(self):
142n/a operator = self.module
143n/a self.assertRaises(TypeError, operator.countOf)
144n/a self.assertRaises(TypeError, operator.countOf, None, None)
145n/a self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
146n/a self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)
147n/a
148n/a def test_delitem(self):
149n/a operator = self.module
150n/a a = [4, 3, 2, 1]
151n/a self.assertRaises(TypeError, operator.delitem, a)
152n/a self.assertRaises(TypeError, operator.delitem, a, None)
153n/a self.assertIsNone(operator.delitem(a, 1))
154n/a self.assertEqual(a, [4, 2, 1])
155n/a
156n/a def test_floordiv(self):
157n/a operator = self.module
158n/a self.assertRaises(TypeError, operator.floordiv, 5)
159n/a self.assertRaises(TypeError, operator.floordiv, None, None)
160n/a self.assertEqual(operator.floordiv(5, 2), 2)
161n/a
162n/a def test_truediv(self):
163n/a operator = self.module
164n/a self.assertRaises(TypeError, operator.truediv, 5)
165n/a self.assertRaises(TypeError, operator.truediv, None, None)
166n/a self.assertEqual(operator.truediv(5, 2), 2.5)
167n/a
168n/a def test_getitem(self):
169n/a operator = self.module
170n/a a = range(10)
171n/a self.assertRaises(TypeError, operator.getitem)
172n/a self.assertRaises(TypeError, operator.getitem, a, None)
173n/a self.assertEqual(operator.getitem(a, 2), 2)
174n/a
175n/a def test_indexOf(self):
176n/a operator = self.module
177n/a self.assertRaises(TypeError, operator.indexOf)
178n/a self.assertRaises(TypeError, operator.indexOf, None, None)
179n/a self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
180n/a self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
181n/a
182n/a def test_invert(self):
183n/a operator = self.module
184n/a self.assertRaises(TypeError, operator.invert)
185n/a self.assertRaises(TypeError, operator.invert, None)
186n/a self.assertEqual(operator.inv(4), -5)
187n/a
188n/a def test_lshift(self):
189n/a operator = self.module
190n/a self.assertRaises(TypeError, operator.lshift)
191n/a self.assertRaises(TypeError, operator.lshift, None, 42)
192n/a self.assertEqual(operator.lshift(5, 1), 10)
193n/a self.assertEqual(operator.lshift(5, 0), 5)
194n/a self.assertRaises(ValueError, operator.lshift, 2, -1)
195n/a
196n/a def test_mod(self):
197n/a operator = self.module
198n/a self.assertRaises(TypeError, operator.mod)
199n/a self.assertRaises(TypeError, operator.mod, None, 42)
200n/a self.assertEqual(operator.mod(5, 2), 1)
201n/a
202n/a def test_mul(self):
203n/a operator = self.module
204n/a self.assertRaises(TypeError, operator.mul)
205n/a self.assertRaises(TypeError, operator.mul, None, None)
206n/a self.assertEqual(operator.mul(5, 2), 10)
207n/a
208n/a def test_matmul(self):
209n/a operator = self.module
210n/a self.assertRaises(TypeError, operator.matmul)
211n/a self.assertRaises(TypeError, operator.matmul, 42, 42)
212n/a class M:
213n/a def __matmul__(self, other):
214n/a return other - 1
215n/a self.assertEqual(M() @ 42, 41)
216n/a
217n/a def test_neg(self):
218n/a operator = self.module
219n/a self.assertRaises(TypeError, operator.neg)
220n/a self.assertRaises(TypeError, operator.neg, None)
221n/a self.assertEqual(operator.neg(5), -5)
222n/a self.assertEqual(operator.neg(-5), 5)
223n/a self.assertEqual(operator.neg(0), 0)
224n/a self.assertEqual(operator.neg(-0), 0)
225n/a
226n/a def test_bitwise_or(self):
227n/a operator = self.module
228n/a self.assertRaises(TypeError, operator.or_)
229n/a self.assertRaises(TypeError, operator.or_, None, None)
230n/a self.assertEqual(operator.or_(0xa, 0x5), 0xf)
231n/a
232n/a def test_pos(self):
233n/a operator = self.module
234n/a self.assertRaises(TypeError, operator.pos)
235n/a self.assertRaises(TypeError, operator.pos, None)
236n/a self.assertEqual(operator.pos(5), 5)
237n/a self.assertEqual(operator.pos(-5), -5)
238n/a self.assertEqual(operator.pos(0), 0)
239n/a self.assertEqual(operator.pos(-0), 0)
240n/a
241n/a def test_pow(self):
242n/a operator = self.module
243n/a self.assertRaises(TypeError, operator.pow)
244n/a self.assertRaises(TypeError, operator.pow, None, None)
245n/a self.assertEqual(operator.pow(3,5), 3**5)
246n/a self.assertRaises(TypeError, operator.pow, 1)
247n/a self.assertRaises(TypeError, operator.pow, 1, 2, 3)
248n/a
249n/a def test_rshift(self):
250n/a operator = self.module
251n/a self.assertRaises(TypeError, operator.rshift)
252n/a self.assertRaises(TypeError, operator.rshift, None, 42)
253n/a self.assertEqual(operator.rshift(5, 1), 2)
254n/a self.assertEqual(operator.rshift(5, 0), 5)
255n/a self.assertRaises(ValueError, operator.rshift, 2, -1)
256n/a
257n/a def test_contains(self):
258n/a operator = self.module
259n/a self.assertRaises(TypeError, operator.contains)
260n/a self.assertRaises(TypeError, operator.contains, None, None)
261n/a self.assertTrue(operator.contains(range(4), 2))
262n/a self.assertFalse(operator.contains(range(4), 5))
263n/a
264n/a def test_setitem(self):
265n/a operator = self.module
266n/a a = list(range(3))
267n/a self.assertRaises(TypeError, operator.setitem, a)
268n/a self.assertRaises(TypeError, operator.setitem, a, None, None)
269n/a self.assertIsNone(operator.setitem(a, 0, 2))
270n/a self.assertEqual(a, [2, 1, 2])
271n/a self.assertRaises(IndexError, operator.setitem, a, 4, 2)
272n/a
273n/a def test_sub(self):
274n/a operator = self.module
275n/a self.assertRaises(TypeError, operator.sub)
276n/a self.assertRaises(TypeError, operator.sub, None, None)
277n/a self.assertEqual(operator.sub(5, 2), 3)
278n/a
279n/a def test_truth(self):
280n/a operator = self.module
281n/a class C(object):
282n/a def __bool__(self):
283n/a raise SyntaxError
284n/a self.assertRaises(TypeError, operator.truth)
285n/a self.assertRaises(SyntaxError, operator.truth, C())
286n/a self.assertTrue(operator.truth(5))
287n/a self.assertTrue(operator.truth([0]))
288n/a self.assertFalse(operator.truth(0))
289n/a self.assertFalse(operator.truth([]))
290n/a
291n/a def test_bitwise_xor(self):
292n/a operator = self.module
293n/a self.assertRaises(TypeError, operator.xor)
294n/a self.assertRaises(TypeError, operator.xor, None, None)
295n/a self.assertEqual(operator.xor(0xb, 0xc), 0x7)
296n/a
297n/a def test_is(self):
298n/a operator = self.module
299n/a a = b = 'xyzpdq'
300n/a c = a[:3] + b[3:]
301n/a self.assertRaises(TypeError, operator.is_)
302n/a self.assertTrue(operator.is_(a, b))
303n/a self.assertFalse(operator.is_(a,c))
304n/a
305n/a def test_is_not(self):
306n/a operator = self.module
307n/a a = b = 'xyzpdq'
308n/a c = a[:3] + b[3:]
309n/a self.assertRaises(TypeError, operator.is_not)
310n/a self.assertFalse(operator.is_not(a, b))
311n/a self.assertTrue(operator.is_not(a,c))
312n/a
313n/a def test_attrgetter(self):
314n/a operator = self.module
315n/a class A:
316n/a pass
317n/a a = A()
318n/a a.name = 'arthur'
319n/a f = operator.attrgetter('name')
320n/a self.assertEqual(f(a), 'arthur')
321n/a self.assertRaises(TypeError, f)
322n/a self.assertRaises(TypeError, f, a, 'dent')
323n/a self.assertRaises(TypeError, f, a, surname='dent')
324n/a f = operator.attrgetter('rank')
325n/a self.assertRaises(AttributeError, f, a)
326n/a self.assertRaises(TypeError, operator.attrgetter, 2)
327n/a self.assertRaises(TypeError, operator.attrgetter)
328n/a
329n/a # multiple gets
330n/a record = A()
331n/a record.x = 'X'
332n/a record.y = 'Y'
333n/a record.z = 'Z'
334n/a self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))
335n/a self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y'))
336n/a
337n/a class C(object):
338n/a def __getattr__(self, name):
339n/a raise SyntaxError
340n/a self.assertRaises(SyntaxError, operator.attrgetter('foo'), C())
341n/a
342n/a # recursive gets
343n/a a = A()
344n/a a.name = 'arthur'
345n/a a.child = A()
346n/a a.child.name = 'thomas'
347n/a f = operator.attrgetter('child.name')
348n/a self.assertEqual(f(a), 'thomas')
349n/a self.assertRaises(AttributeError, f, a.child)
350n/a f = operator.attrgetter('name', 'child.name')
351n/a self.assertEqual(f(a), ('arthur', 'thomas'))
352n/a f = operator.attrgetter('name', 'child.name', 'child.child.name')
353n/a self.assertRaises(AttributeError, f, a)
354n/a f = operator.attrgetter('child.')
355n/a self.assertRaises(AttributeError, f, a)
356n/a f = operator.attrgetter('.child')
357n/a self.assertRaises(AttributeError, f, a)
358n/a
359n/a a.child.child = A()
360n/a a.child.child.name = 'johnson'
361n/a f = operator.attrgetter('child.child.name')
362n/a self.assertEqual(f(a), 'johnson')
363n/a f = operator.attrgetter('name', 'child.name', 'child.child.name')
364n/a self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))
365n/a
366n/a def test_itemgetter(self):
367n/a operator = self.module
368n/a a = 'ABCDE'
369n/a f = operator.itemgetter(2)
370n/a self.assertEqual(f(a), 'C')
371n/a self.assertRaises(TypeError, f)
372n/a self.assertRaises(TypeError, f, a, 3)
373n/a self.assertRaises(TypeError, f, a, size=3)
374n/a f = operator.itemgetter(10)
375n/a self.assertRaises(IndexError, f, a)
376n/a
377n/a class C(object):
378n/a def __getitem__(self, name):
379n/a raise SyntaxError
380n/a self.assertRaises(SyntaxError, operator.itemgetter(42), C())
381n/a
382n/a f = operator.itemgetter('name')
383n/a self.assertRaises(TypeError, f, a)
384n/a self.assertRaises(TypeError, operator.itemgetter)
385n/a
386n/a d = dict(key='val')
387n/a f = operator.itemgetter('key')
388n/a self.assertEqual(f(d), 'val')
389n/a f = operator.itemgetter('nonkey')
390n/a self.assertRaises(KeyError, f, d)
391n/a
392n/a # example used in the docs
393n/a inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
394n/a getcount = operator.itemgetter(1)
395n/a self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1])
396n/a self.assertEqual(sorted(inventory, key=getcount),
397n/a [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
398n/a
399n/a # multiple gets
400n/a data = list(map(str, range(20)))
401n/a self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
402n/a self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)
403n/a
404n/a def test_methodcaller(self):
405n/a operator = self.module
406n/a self.assertRaises(TypeError, operator.methodcaller)
407n/a self.assertRaises(TypeError, operator.methodcaller, 12)
408n/a class A:
409n/a def foo(self, *args, **kwds):
410n/a return args[0] + args[1]
411n/a def bar(self, f=42):
412n/a return f
413n/a def baz(*args, **kwds):
414n/a return kwds['name'], kwds['self']
415n/a a = A()
416n/a f = operator.methodcaller('foo')
417n/a self.assertRaises(IndexError, f, a)
418n/a f = operator.methodcaller('foo', 1, 2)
419n/a self.assertEqual(f(a), 3)
420n/a self.assertRaises(TypeError, f)
421n/a self.assertRaises(TypeError, f, a, 3)
422n/a self.assertRaises(TypeError, f, a, spam=3)
423n/a f = operator.methodcaller('bar')
424n/a self.assertEqual(f(a), 42)
425n/a self.assertRaises(TypeError, f, a, a)
426n/a f = operator.methodcaller('bar', f=5)
427n/a self.assertEqual(f(a), 5)
428n/a f = operator.methodcaller('baz', name='spam', self='eggs')
429n/a self.assertEqual(f(a), ('spam', 'eggs'))
430n/a
431n/a def test_inplace(self):
432n/a operator = self.module
433n/a class C(object):
434n/a def __iadd__ (self, other): return "iadd"
435n/a def __iand__ (self, other): return "iand"
436n/a def __ifloordiv__(self, other): return "ifloordiv"
437n/a def __ilshift__ (self, other): return "ilshift"
438n/a def __imod__ (self, other): return "imod"
439n/a def __imul__ (self, other): return "imul"
440n/a def __imatmul__ (self, other): return "imatmul"
441n/a def __ior__ (self, other): return "ior"
442n/a def __ipow__ (self, other): return "ipow"
443n/a def __irshift__ (self, other): return "irshift"
444n/a def __isub__ (self, other): return "isub"
445n/a def __itruediv__ (self, other): return "itruediv"
446n/a def __ixor__ (self, other): return "ixor"
447n/a def __getitem__(self, other): return 5 # so that C is a sequence
448n/a c = C()
449n/a self.assertEqual(operator.iadd (c, 5), "iadd")
450n/a self.assertEqual(operator.iand (c, 5), "iand")
451n/a self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
452n/a self.assertEqual(operator.ilshift (c, 5), "ilshift")
453n/a self.assertEqual(operator.imod (c, 5), "imod")
454n/a self.assertEqual(operator.imul (c, 5), "imul")
455n/a self.assertEqual(operator.imatmul (c, 5), "imatmul")
456n/a self.assertEqual(operator.ior (c, 5), "ior")
457n/a self.assertEqual(operator.ipow (c, 5), "ipow")
458n/a self.assertEqual(operator.irshift (c, 5), "irshift")
459n/a self.assertEqual(operator.isub (c, 5), "isub")
460n/a self.assertEqual(operator.itruediv (c, 5), "itruediv")
461n/a self.assertEqual(operator.ixor (c, 5), "ixor")
462n/a self.assertEqual(operator.iconcat (c, c), "iadd")
463n/a
464n/a def test_length_hint(self):
465n/a operator = self.module
466n/a class X(object):
467n/a def __init__(self, value):
468n/a self.value = value
469n/a
470n/a def __length_hint__(self):
471n/a if type(self.value) is type:
472n/a raise self.value
473n/a else:
474n/a return self.value
475n/a
476n/a self.assertEqual(operator.length_hint([], 2), 0)
477n/a self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3)
478n/a
479n/a self.assertEqual(operator.length_hint(X(2)), 2)
480n/a self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4)
481n/a self.assertEqual(operator.length_hint(X(TypeError), 12), 12)
482n/a with self.assertRaises(TypeError):
483n/a operator.length_hint(X("abc"))
484n/a with self.assertRaises(ValueError):
485n/a operator.length_hint(X(-2))
486n/a with self.assertRaises(LookupError):
487n/a operator.length_hint(X(LookupError))
488n/a
489n/a def test_dunder_is_original(self):
490n/a operator = self.module
491n/a
492n/a names = [name for name in dir(operator) if not name.startswith('_')]
493n/a for name in names:
494n/a orig = getattr(operator, name)
495n/a dunder = getattr(operator, '__' + name.strip('_') + '__', None)
496n/a if dunder:
497n/a self.assertIs(dunder, orig)
498n/a
499n/aclass PyOperatorTestCase(OperatorTestCase, unittest.TestCase):
500n/a module = py_operator
501n/a
502n/a@unittest.skipUnless(c_operator, 'requires _operator')
503n/aclass COperatorTestCase(OperatorTestCase, unittest.TestCase):
504n/a module = c_operator
505n/a
506n/a
507n/aclass OperatorPickleTestCase:
508n/a def copy(self, obj, proto):
509n/a with support.swap_item(sys.modules, 'operator', self.module):
510n/a pickled = pickle.dumps(obj, proto)
511n/a with support.swap_item(sys.modules, 'operator', self.module2):
512n/a return pickle.loads(pickled)
513n/a
514n/a def test_attrgetter(self):
515n/a attrgetter = self.module.attrgetter
516n/a class A:
517n/a pass
518n/a a = A()
519n/a a.x = 'X'
520n/a a.y = 'Y'
521n/a a.z = 'Z'
522n/a a.t = A()
523n/a a.t.u = A()
524n/a a.t.u.v = 'V'
525n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
526n/a with self.subTest(proto=proto):
527n/a f = attrgetter('x')
528n/a f2 = self.copy(f, proto)
529n/a self.assertEqual(repr(f2), repr(f))
530n/a self.assertEqual(f2(a), f(a))
531n/a # multiple gets
532n/a f = attrgetter('x', 'y', 'z')
533n/a f2 = self.copy(f, proto)
534n/a self.assertEqual(repr(f2), repr(f))
535n/a self.assertEqual(f2(a), f(a))
536n/a # recursive gets
537n/a f = attrgetter('t.u.v')
538n/a f2 = self.copy(f, proto)
539n/a self.assertEqual(repr(f2), repr(f))
540n/a self.assertEqual(f2(a), f(a))
541n/a
542n/a def test_itemgetter(self):
543n/a itemgetter = self.module.itemgetter
544n/a a = 'ABCDE'
545n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
546n/a with self.subTest(proto=proto):
547n/a f = itemgetter(2)
548n/a f2 = self.copy(f, proto)
549n/a self.assertEqual(repr(f2), repr(f))
550n/a self.assertEqual(f2(a), f(a))
551n/a # multiple gets
552n/a f = itemgetter(2, 0, 4)
553n/a f2 = self.copy(f, proto)
554n/a self.assertEqual(repr(f2), repr(f))
555n/a self.assertEqual(f2(a), f(a))
556n/a
557n/a def test_methodcaller(self):
558n/a methodcaller = self.module.methodcaller
559n/a class A:
560n/a def foo(self, *args, **kwds):
561n/a return args[0] + args[1]
562n/a def bar(self, f=42):
563n/a return f
564n/a def baz(*args, **kwds):
565n/a return kwds['name'], kwds['self']
566n/a a = A()
567n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
568n/a with self.subTest(proto=proto):
569n/a f = methodcaller('bar')
570n/a f2 = self.copy(f, proto)
571n/a self.assertEqual(repr(f2), repr(f))
572n/a self.assertEqual(f2(a), f(a))
573n/a # positional args
574n/a f = methodcaller('foo', 1, 2)
575n/a f2 = self.copy(f, proto)
576n/a self.assertEqual(repr(f2), repr(f))
577n/a self.assertEqual(f2(a), f(a))
578n/a # keyword args
579n/a f = methodcaller('bar', f=5)
580n/a f2 = self.copy(f, proto)
581n/a self.assertEqual(repr(f2), repr(f))
582n/a self.assertEqual(f2(a), f(a))
583n/a f = methodcaller('baz', self='eggs', name='spam')
584n/a f2 = self.copy(f, proto)
585n/a # Can't test repr consistently with multiple keyword args
586n/a self.assertEqual(f2(a), f(a))
587n/a
588n/aclass PyPyOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
589n/a module = py_operator
590n/a module2 = py_operator
591n/a
592n/a@unittest.skipUnless(c_operator, 'requires _operator')
593n/aclass PyCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
594n/a module = py_operator
595n/a module2 = c_operator
596n/a
597n/a@unittest.skipUnless(c_operator, 'requires _operator')
598n/aclass CPyOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
599n/a module = c_operator
600n/a module2 = py_operator
601n/a
602n/a@unittest.skipUnless(c_operator, 'requires _operator')
603n/aclass CCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
604n/a module = c_operator
605n/a module2 = c_operator
606n/a
607n/a
608n/aif __name__ == "__main__":
609n/a unittest.main()