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

Python code coverage for Lib/test/mapping_tests.py

#countcontent
1n/a# tests common to dict and UserDict
2n/aimport unittest
3n/aimport collections
4n/a
5n/a
6n/aclass BasicTestMappingProtocol(unittest.TestCase):
7n/a # This base class can be used to check that an object conforms to the
8n/a # mapping protocol
9n/a
10n/a # Functions that can be useful to override to adapt to dictionary
11n/a # semantics
12n/a type2test = None # which class is being tested (overwrite in subclasses)
13n/a
14n/a def _reference(self):
15n/a """Return a dictionary of values which are invariant by storage
16n/a in the object under test."""
17n/a return {"1": "2", "key1":"value1", "key2":(1,2,3)}
18n/a def _empty_mapping(self):
19n/a """Return an empty mapping object"""
20n/a return self.type2test()
21n/a def _full_mapping(self, data):
22n/a """Return a mapping object with the value contained in data
23n/a dictionary"""
24n/a x = self._empty_mapping()
25n/a for key, value in data.items():
26n/a x[key] = value
27n/a return x
28n/a
29n/a def __init__(self, *args, **kw):
30n/a unittest.TestCase.__init__(self, *args, **kw)
31n/a self.reference = self._reference().copy()
32n/a
33n/a # A (key, value) pair not in the mapping
34n/a key, value = self.reference.popitem()
35n/a self.other = {key:value}
36n/a
37n/a # A (key, value) pair in the mapping
38n/a key, value = self.reference.popitem()
39n/a self.inmapping = {key:value}
40n/a self.reference[key] = value
41n/a
42n/a def test_read(self):
43n/a # Test for read only operations on mapping
44n/a p = self._empty_mapping()
45n/a p1 = dict(p) #workaround for singleton objects
46n/a d = self._full_mapping(self.reference)
47n/a if d is p:
48n/a p = p1
49n/a #Indexing
50n/a for key, value in self.reference.items():
51n/a self.assertEqual(d[key], value)
52n/a knownkey = list(self.other.keys())[0]
53n/a self.assertRaises(KeyError, lambda:d[knownkey])
54n/a #len
55n/a self.assertEqual(len(p), 0)
56n/a self.assertEqual(len(d), len(self.reference))
57n/a #__contains__
58n/a for k in self.reference:
59n/a self.assertIn(k, d)
60n/a for k in self.other:
61n/a self.assertNotIn(k, d)
62n/a #cmp
63n/a self.assertEqual(p, p)
64n/a self.assertEqual(d, d)
65n/a self.assertNotEqual(p, d)
66n/a self.assertNotEqual(d, p)
67n/a #bool
68n/a if p: self.fail("Empty mapping must compare to False")
69n/a if not d: self.fail("Full mapping must compare to True")
70n/a # keys(), items(), iterkeys() ...
71n/a def check_iterandlist(iter, lst, ref):
72n/a self.assertTrue(hasattr(iter, '__next__'))
73n/a self.assertTrue(hasattr(iter, '__iter__'))
74n/a x = list(iter)
75n/a self.assertTrue(set(x)==set(lst)==set(ref))
76n/a check_iterandlist(iter(d.keys()), list(d.keys()),
77n/a self.reference.keys())
78n/a check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
79n/a check_iterandlist(iter(d.values()), list(d.values()),
80n/a self.reference.values())
81n/a check_iterandlist(iter(d.items()), list(d.items()),
82n/a self.reference.items())
83n/a #get
84n/a key, value = next(iter(d.items()))
85n/a knownkey, knownvalue = next(iter(self.other.items()))
86n/a self.assertEqual(d.get(key, knownvalue), value)
87n/a self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
88n/a self.assertNotIn(knownkey, d)
89n/a
90n/a def test_write(self):
91n/a # Test for write operations on mapping
92n/a p = self._empty_mapping()
93n/a #Indexing
94n/a for key, value in self.reference.items():
95n/a p[key] = value
96n/a self.assertEqual(p[key], value)
97n/a for key in self.reference.keys():
98n/a del p[key]
99n/a self.assertRaises(KeyError, lambda:p[key])
100n/a p = self._empty_mapping()
101n/a #update
102n/a p.update(self.reference)
103n/a self.assertEqual(dict(p), self.reference)
104n/a items = list(p.items())
105n/a p = self._empty_mapping()
106n/a p.update(items)
107n/a self.assertEqual(dict(p), self.reference)
108n/a d = self._full_mapping(self.reference)
109n/a #setdefault
110n/a key, value = next(iter(d.items()))
111n/a knownkey, knownvalue = next(iter(self.other.items()))
112n/a self.assertEqual(d.setdefault(key, knownvalue), value)
113n/a self.assertEqual(d[key], value)
114n/a self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
115n/a self.assertEqual(d[knownkey], knownvalue)
116n/a #pop
117n/a self.assertEqual(d.pop(knownkey), knownvalue)
118n/a self.assertNotIn(knownkey, d)
119n/a self.assertRaises(KeyError, d.pop, knownkey)
120n/a default = 909
121n/a d[knownkey] = knownvalue
122n/a self.assertEqual(d.pop(knownkey, default), knownvalue)
123n/a self.assertNotIn(knownkey, d)
124n/a self.assertEqual(d.pop(knownkey, default), default)
125n/a #popitem
126n/a key, value = d.popitem()
127n/a self.assertNotIn(key, d)
128n/a self.assertEqual(value, self.reference[key])
129n/a p=self._empty_mapping()
130n/a self.assertRaises(KeyError, p.popitem)
131n/a
132n/a def test_constructor(self):
133n/a self.assertEqual(self._empty_mapping(), self._empty_mapping())
134n/a
135n/a def test_bool(self):
136n/a self.assertTrue(not self._empty_mapping())
137n/a self.assertTrue(self.reference)
138n/a self.assertTrue(bool(self._empty_mapping()) is False)
139n/a self.assertTrue(bool(self.reference) is True)
140n/a
141n/a def test_keys(self):
142n/a d = self._empty_mapping()
143n/a self.assertEqual(list(d.keys()), [])
144n/a d = self.reference
145n/a self.assertIn(list(self.inmapping.keys())[0], d.keys())
146n/a self.assertNotIn(list(self.other.keys())[0], d.keys())
147n/a self.assertRaises(TypeError, d.keys, None)
148n/a
149n/a def test_values(self):
150n/a d = self._empty_mapping()
151n/a self.assertEqual(list(d.values()), [])
152n/a
153n/a self.assertRaises(TypeError, d.values, None)
154n/a
155n/a def test_items(self):
156n/a d = self._empty_mapping()
157n/a self.assertEqual(list(d.items()), [])
158n/a
159n/a self.assertRaises(TypeError, d.items, None)
160n/a
161n/a def test_len(self):
162n/a d = self._empty_mapping()
163n/a self.assertEqual(len(d), 0)
164n/a
165n/a def test_getitem(self):
166n/a d = self.reference
167n/a self.assertEqual(d[list(self.inmapping.keys())[0]],
168n/a list(self.inmapping.values())[0])
169n/a
170n/a self.assertRaises(TypeError, d.__getitem__)
171n/a
172n/a def test_update(self):
173n/a # mapping argument
174n/a d = self._empty_mapping()
175n/a d.update(self.other)
176n/a self.assertEqual(list(d.items()), list(self.other.items()))
177n/a
178n/a # No argument
179n/a d = self._empty_mapping()
180n/a d.update()
181n/a self.assertEqual(d, self._empty_mapping())
182n/a
183n/a # item sequence
184n/a d = self._empty_mapping()
185n/a d.update(self.other.items())
186n/a self.assertEqual(list(d.items()), list(self.other.items()))
187n/a
188n/a # Iterator
189n/a d = self._empty_mapping()
190n/a d.update(self.other.items())
191n/a self.assertEqual(list(d.items()), list(self.other.items()))
192n/a
193n/a # FIXME: Doesn't work with UserDict
194n/a # self.assertRaises((TypeError, AttributeError), d.update, None)
195n/a self.assertRaises((TypeError, AttributeError), d.update, 42)
196n/a
197n/a outerself = self
198n/a class SimpleUserDict:
199n/a def __init__(self):
200n/a self.d = outerself.reference
201n/a def keys(self):
202n/a return self.d.keys()
203n/a def __getitem__(self, i):
204n/a return self.d[i]
205n/a d.clear()
206n/a d.update(SimpleUserDict())
207n/a i1 = sorted(d.items())
208n/a i2 = sorted(self.reference.items())
209n/a self.assertEqual(i1, i2)
210n/a
211n/a class Exc(Exception): pass
212n/a
213n/a d = self._empty_mapping()
214n/a class FailingUserDict:
215n/a def keys(self):
216n/a raise Exc
217n/a self.assertRaises(Exc, d.update, FailingUserDict())
218n/a
219n/a d.clear()
220n/a
221n/a class FailingUserDict:
222n/a def keys(self):
223n/a class BogonIter:
224n/a def __init__(self):
225n/a self.i = 1
226n/a def __iter__(self):
227n/a return self
228n/a def __next__(self):
229n/a if self.i:
230n/a self.i = 0
231n/a return 'a'
232n/a raise Exc
233n/a return BogonIter()
234n/a def __getitem__(self, key):
235n/a return key
236n/a self.assertRaises(Exc, d.update, FailingUserDict())
237n/a
238n/a class FailingUserDict:
239n/a def keys(self):
240n/a class BogonIter:
241n/a def __init__(self):
242n/a self.i = ord('a')
243n/a def __iter__(self):
244n/a return self
245n/a def __next__(self):
246n/a if self.i <= ord('z'):
247n/a rtn = chr(self.i)
248n/a self.i += 1
249n/a return rtn
250n/a raise StopIteration
251n/a return BogonIter()
252n/a def __getitem__(self, key):
253n/a raise Exc
254n/a self.assertRaises(Exc, d.update, FailingUserDict())
255n/a
256n/a d = self._empty_mapping()
257n/a class badseq(object):
258n/a def __iter__(self):
259n/a return self
260n/a def __next__(self):
261n/a raise Exc()
262n/a
263n/a self.assertRaises(Exc, d.update, badseq())
264n/a
265n/a self.assertRaises(ValueError, d.update, [(1, 2, 3)])
266n/a
267n/a # no test_fromkeys or test_copy as both os.environ and selves don't support it
268n/a
269n/a def test_get(self):
270n/a d = self._empty_mapping()
271n/a self.assertTrue(d.get(list(self.other.keys())[0]) is None)
272n/a self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
273n/a d = self.reference
274n/a self.assertTrue(d.get(list(self.other.keys())[0]) is None)
275n/a self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
276n/a self.assertEqual(d.get(list(self.inmapping.keys())[0]),
277n/a list(self.inmapping.values())[0])
278n/a self.assertEqual(d.get(list(self.inmapping.keys())[0], 3),
279n/a list(self.inmapping.values())[0])
280n/a self.assertRaises(TypeError, d.get)
281n/a self.assertRaises(TypeError, d.get, None, None, None)
282n/a
283n/a def test_setdefault(self):
284n/a d = self._empty_mapping()
285n/a self.assertRaises(TypeError, d.setdefault)
286n/a
287n/a def test_popitem(self):
288n/a d = self._empty_mapping()
289n/a self.assertRaises(KeyError, d.popitem)
290n/a self.assertRaises(TypeError, d.popitem, 42)
291n/a
292n/a def test_pop(self):
293n/a d = self._empty_mapping()
294n/a k, v = list(self.inmapping.items())[0]
295n/a d[k] = v
296n/a self.assertRaises(KeyError, d.pop, list(self.other.keys())[0])
297n/a
298n/a self.assertEqual(d.pop(k), v)
299n/a self.assertEqual(len(d), 0)
300n/a
301n/a self.assertRaises(KeyError, d.pop, k)
302n/a
303n/a
304n/aclass TestMappingProtocol(BasicTestMappingProtocol):
305n/a def test_constructor(self):
306n/a BasicTestMappingProtocol.test_constructor(self)
307n/a self.assertTrue(self._empty_mapping() is not self._empty_mapping())
308n/a self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2})
309n/a
310n/a def test_bool(self):
311n/a BasicTestMappingProtocol.test_bool(self)
312n/a self.assertTrue(not self._empty_mapping())
313n/a self.assertTrue(self._full_mapping({"x": "y"}))
314n/a self.assertTrue(bool(self._empty_mapping()) is False)
315n/a self.assertTrue(bool(self._full_mapping({"x": "y"})) is True)
316n/a
317n/a def test_keys(self):
318n/a BasicTestMappingProtocol.test_keys(self)
319n/a d = self._empty_mapping()
320n/a self.assertEqual(list(d.keys()), [])
321n/a d = self._full_mapping({'a': 1, 'b': 2})
322n/a k = d.keys()
323n/a self.assertIn('a', k)
324n/a self.assertIn('b', k)
325n/a self.assertNotIn('c', k)
326n/a
327n/a def test_values(self):
328n/a BasicTestMappingProtocol.test_values(self)
329n/a d = self._full_mapping({1:2})
330n/a self.assertEqual(list(d.values()), [2])
331n/a
332n/a def test_items(self):
333n/a BasicTestMappingProtocol.test_items(self)
334n/a
335n/a d = self._full_mapping({1:2})
336n/a self.assertEqual(list(d.items()), [(1, 2)])
337n/a
338n/a def test_contains(self):
339n/a d = self._empty_mapping()
340n/a self.assertNotIn('a', d)
341n/a self.assertTrue(not ('a' in d))
342n/a self.assertTrue('a' not in d)
343n/a d = self._full_mapping({'a': 1, 'b': 2})
344n/a self.assertIn('a', d)
345n/a self.assertIn('b', d)
346n/a self.assertNotIn('c', d)
347n/a
348n/a self.assertRaises(TypeError, d.__contains__)
349n/a
350n/a def test_len(self):
351n/a BasicTestMappingProtocol.test_len(self)
352n/a d = self._full_mapping({'a': 1, 'b': 2})
353n/a self.assertEqual(len(d), 2)
354n/a
355n/a def test_getitem(self):
356n/a BasicTestMappingProtocol.test_getitem(self)
357n/a d = self._full_mapping({'a': 1, 'b': 2})
358n/a self.assertEqual(d['a'], 1)
359n/a self.assertEqual(d['b'], 2)
360n/a d['c'] = 3
361n/a d['a'] = 4
362n/a self.assertEqual(d['c'], 3)
363n/a self.assertEqual(d['a'], 4)
364n/a del d['b']
365n/a self.assertEqual(d, {'a': 4, 'c': 3})
366n/a
367n/a self.assertRaises(TypeError, d.__getitem__)
368n/a
369n/a def test_clear(self):
370n/a d = self._full_mapping({1:1, 2:2, 3:3})
371n/a d.clear()
372n/a self.assertEqual(d, {})
373n/a
374n/a self.assertRaises(TypeError, d.clear, None)
375n/a
376n/a def test_update(self):
377n/a BasicTestMappingProtocol.test_update(self)
378n/a # mapping argument
379n/a d = self._empty_mapping()
380n/a d.update({1:100})
381n/a d.update({2:20})
382n/a d.update({1:1, 2:2, 3:3})
383n/a self.assertEqual(d, {1:1, 2:2, 3:3})
384n/a
385n/a # no argument
386n/a d.update()
387n/a self.assertEqual(d, {1:1, 2:2, 3:3})
388n/a
389n/a # keyword arguments
390n/a d = self._empty_mapping()
391n/a d.update(x=100)
392n/a d.update(y=20)
393n/a d.update(x=1, y=2, z=3)
394n/a self.assertEqual(d, {"x":1, "y":2, "z":3})
395n/a
396n/a # item sequence
397n/a d = self._empty_mapping()
398n/a d.update([("x", 100), ("y", 20)])
399n/a self.assertEqual(d, {"x":100, "y":20})
400n/a
401n/a # Both item sequence and keyword arguments
402n/a d = self._empty_mapping()
403n/a d.update([("x", 100), ("y", 20)], x=1, y=2)
404n/a self.assertEqual(d, {"x":1, "y":2})
405n/a
406n/a # iterator
407n/a d = self._full_mapping({1:3, 2:4})
408n/a d.update(self._full_mapping({1:2, 3:4, 5:6}).items())
409n/a self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
410n/a
411n/a class SimpleUserDict:
412n/a def __init__(self):
413n/a self.d = {1:1, 2:2, 3:3}
414n/a def keys(self):
415n/a return self.d.keys()
416n/a def __getitem__(self, i):
417n/a return self.d[i]
418n/a d.clear()
419n/a d.update(SimpleUserDict())
420n/a self.assertEqual(d, {1:1, 2:2, 3:3})
421n/a
422n/a def test_fromkeys(self):
423n/a self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
424n/a d = self._empty_mapping()
425n/a self.assertTrue(not(d.fromkeys('abc') is d))
426n/a self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
427n/a self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
428n/a self.assertEqual(d.fromkeys([]), {})
429n/a def g():
430n/a yield 1
431n/a self.assertEqual(d.fromkeys(g()), {1:None})
432n/a self.assertRaises(TypeError, {}.fromkeys, 3)
433n/a class dictlike(self.type2test): pass
434n/a self.assertEqual(dictlike.fromkeys('a'), {'a':None})
435n/a self.assertEqual(dictlike().fromkeys('a'), {'a':None})
436n/a self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike)
437n/a self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike)
438n/a self.assertTrue(type(dictlike.fromkeys('a')) is dictlike)
439n/a class mydict(self.type2test):
440n/a def __new__(cls):
441n/a return collections.UserDict()
442n/a ud = mydict.fromkeys('ab')
443n/a self.assertEqual(ud, {'a':None, 'b':None})
444n/a self.assertIsInstance(ud, collections.UserDict)
445n/a self.assertRaises(TypeError, dict.fromkeys)
446n/a
447n/a class Exc(Exception): pass
448n/a
449n/a class baddict1(self.type2test):
450n/a def __init__(self):
451n/a raise Exc()
452n/a
453n/a self.assertRaises(Exc, baddict1.fromkeys, [1])
454n/a
455n/a class BadSeq(object):
456n/a def __iter__(self):
457n/a return self
458n/a def __next__(self):
459n/a raise Exc()
460n/a
461n/a self.assertRaises(Exc, self.type2test.fromkeys, BadSeq())
462n/a
463n/a class baddict2(self.type2test):
464n/a def __setitem__(self, key, value):
465n/a raise Exc()
466n/a
467n/a self.assertRaises(Exc, baddict2.fromkeys, [1])
468n/a
469n/a def test_copy(self):
470n/a d = self._full_mapping({1:1, 2:2, 3:3})
471n/a self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
472n/a d = self._empty_mapping()
473n/a self.assertEqual(d.copy(), d)
474n/a self.assertIsInstance(d.copy(), d.__class__)
475n/a self.assertRaises(TypeError, d.copy, None)
476n/a
477n/a def test_get(self):
478n/a BasicTestMappingProtocol.test_get(self)
479n/a d = self._empty_mapping()
480n/a self.assertTrue(d.get('c') is None)
481n/a self.assertEqual(d.get('c', 3), 3)
482n/a d = self._full_mapping({'a' : 1, 'b' : 2})
483n/a self.assertTrue(d.get('c') is None)
484n/a self.assertEqual(d.get('c', 3), 3)
485n/a self.assertEqual(d.get('a'), 1)
486n/a self.assertEqual(d.get('a', 3), 1)
487n/a
488n/a def test_setdefault(self):
489n/a BasicTestMappingProtocol.test_setdefault(self)
490n/a d = self._empty_mapping()
491n/a self.assertTrue(d.setdefault('key0') is None)
492n/a d.setdefault('key0', [])
493n/a self.assertTrue(d.setdefault('key0') is None)
494n/a d.setdefault('key', []).append(3)
495n/a self.assertEqual(d['key'][0], 3)
496n/a d.setdefault('key', []).append(4)
497n/a self.assertEqual(len(d['key']), 2)
498n/a
499n/a def test_popitem(self):
500n/a BasicTestMappingProtocol.test_popitem(self)
501n/a for copymode in -1, +1:
502n/a # -1: b has same structure as a
503n/a # +1: b is a.copy()
504n/a for log2size in range(12):
505n/a size = 2**log2size
506n/a a = self._empty_mapping()
507n/a b = self._empty_mapping()
508n/a for i in range(size):
509n/a a[repr(i)] = i
510n/a if copymode < 0:
511n/a b[repr(i)] = i
512n/a if copymode > 0:
513n/a b = a.copy()
514n/a for i in range(size):
515n/a ka, va = ta = a.popitem()
516n/a self.assertEqual(va, int(ka))
517n/a kb, vb = tb = b.popitem()
518n/a self.assertEqual(vb, int(kb))
519n/a self.assertTrue(not(copymode < 0 and ta != tb))
520n/a self.assertTrue(not a)
521n/a self.assertTrue(not b)
522n/a
523n/a def test_pop(self):
524n/a BasicTestMappingProtocol.test_pop(self)
525n/a
526n/a # Tests for pop with specified key
527n/a d = self._empty_mapping()
528n/a k, v = 'abc', 'def'
529n/a
530n/a self.assertEqual(d.pop(k, v), v)
531n/a d[k] = v
532n/a self.assertEqual(d.pop(k, 1), v)
533n/a
534n/a
535n/aclass TestHashMappingProtocol(TestMappingProtocol):
536n/a
537n/a def test_getitem(self):
538n/a TestMappingProtocol.test_getitem(self)
539n/a class Exc(Exception): pass
540n/a
541n/a class BadEq(object):
542n/a def __eq__(self, other):
543n/a raise Exc()
544n/a def __hash__(self):
545n/a return 24
546n/a
547n/a d = self._empty_mapping()
548n/a d[BadEq()] = 42
549n/a self.assertRaises(KeyError, d.__getitem__, 23)
550n/a
551n/a class BadHash(object):
552n/a fail = False
553n/a def __hash__(self):
554n/a if self.fail:
555n/a raise Exc()
556n/a else:
557n/a return 42
558n/a
559n/a d = self._empty_mapping()
560n/a x = BadHash()
561n/a d[x] = 42
562n/a x.fail = True
563n/a self.assertRaises(Exc, d.__getitem__, x)
564n/a
565n/a def test_fromkeys(self):
566n/a TestMappingProtocol.test_fromkeys(self)
567n/a class mydict(self.type2test):
568n/a def __new__(cls):
569n/a return collections.UserDict()
570n/a ud = mydict.fromkeys('ab')
571n/a self.assertEqual(ud, {'a':None, 'b':None})
572n/a self.assertIsInstance(ud, collections.UserDict)
573n/a
574n/a def test_pop(self):
575n/a TestMappingProtocol.test_pop(self)
576n/a
577n/a class Exc(Exception): pass
578n/a
579n/a class BadHash(object):
580n/a fail = False
581n/a def __hash__(self):
582n/a if self.fail:
583n/a raise Exc()
584n/a else:
585n/a return 42
586n/a
587n/a d = self._empty_mapping()
588n/a x = BadHash()
589n/a d[x] = 42
590n/a x.fail = True
591n/a self.assertRaises(Exc, d.pop, x)
592n/a
593n/a def test_mutatingiteration(self):
594n/a d = self._empty_mapping()
595n/a d[1] = 1
596n/a try:
597n/a for i in d:
598n/a d[i+1] = 1
599n/a except RuntimeError:
600n/a pass
601n/a else:
602n/a self.fail("changing dict size during iteration doesn't raise Error")
603n/a
604n/a def test_repr(self):
605n/a d = self._empty_mapping()
606n/a self.assertEqual(repr(d), '{}')
607n/a d[1] = 2
608n/a self.assertEqual(repr(d), '{1: 2}')
609n/a d = self._empty_mapping()
610n/a d[1] = d
611n/a self.assertEqual(repr(d), '{1: {...}}')
612n/a
613n/a class Exc(Exception): pass
614n/a
615n/a class BadRepr(object):
616n/a def __repr__(self):
617n/a raise Exc()
618n/a
619n/a d = self._full_mapping({1: BadRepr()})
620n/a self.assertRaises(Exc, repr, d)
621n/a
622n/a def test_eq(self):
623n/a self.assertEqual(self._empty_mapping(), self._empty_mapping())
624n/a self.assertEqual(self._full_mapping({1: 2}),
625n/a self._full_mapping({1: 2}))
626n/a
627n/a class Exc(Exception): pass
628n/a
629n/a class BadCmp(object):
630n/a def __eq__(self, other):
631n/a raise Exc()
632n/a def __hash__(self):
633n/a return 1
634n/a
635n/a d1 = self._full_mapping({BadCmp(): 1})
636n/a d2 = self._full_mapping({1: 1})
637n/a self.assertRaises(Exc, lambda: BadCmp()==1)
638n/a self.assertRaises(Exc, lambda: d1==d2)
639n/a
640n/a def test_setdefault(self):
641n/a TestMappingProtocol.test_setdefault(self)
642n/a
643n/a class Exc(Exception): pass
644n/a
645n/a class BadHash(object):
646n/a fail = False
647n/a def __hash__(self):
648n/a if self.fail:
649n/a raise Exc()
650n/a else:
651n/a return 42
652n/a
653n/a d = self._empty_mapping()
654n/a x = BadHash()
655n/a d[x] = 42
656n/a x.fail = True
657n/a self.assertRaises(Exc, d.setdefault, x, [])