PyXR

c:\python24\lib \ test \ mapping_tests.py



0001 # tests common to dict and UserDict
0002 import unittest
0003 import UserDict
0004 
0005 
0006 class BasicTestMappingProtocol(unittest.TestCase):
0007     # This base class can be used to check that an object conforms to the
0008     # mapping protocol
0009 
0010     # Functions that can be useful to override to adapt to dictionary
0011     # semantics
0012     type2test = None # which class is being tested (overwrite in subclasses)
0013 
0014     def _reference(self):
0015         """Return a dictionary of values which are invariant by storage
0016         in the object under test."""
0017         return {1:2, "key1":"value1", "key2":(1,2,3)}
0018     def _empty_mapping(self):
0019         """Return an empty mapping object"""
0020         return self.type2test()
0021     def _full_mapping(self, data):
0022         """Return a mapping object with the value contained in data
0023         dictionary"""
0024         x = self._empty_mapping()
0025         for key, value in data.items():
0026             x[key] = value
0027         return x
0028 
0029     def __init__(self, *args, **kw):
0030         unittest.TestCase.__init__(self, *args, **kw)
0031         self.reference = self._reference().copy()
0032 
0033         # A (key, value) pair not in the mapping
0034         key, value = self.reference.popitem()
0035         self.other = {key:value}
0036 
0037         # A (key, value) pair in the mapping
0038         key, value = self.reference.popitem()
0039         self.inmapping = {key:value}
0040         self.reference[key] = value
0041 
0042     def test_read(self):
0043         # Test for read only operations on mapping
0044         p = self._empty_mapping()
0045         p1 = dict(p) #workaround for singleton objects
0046         d = self._full_mapping(self.reference)
0047         if d is p:
0048             p = p1
0049         #Indexing
0050         for key, value in self.reference.items():
0051             self.assertEqual(d[key], value)
0052         knownkey = self.other.keys()[0]
0053         self.failUnlessRaises(KeyError, lambda:d[knownkey])
0054         #len
0055         self.assertEqual(len(p), 0)
0056         self.assertEqual(len(d), len(self.reference))
0057         #has_key
0058         for k in self.reference:
0059             self.assert_(d.has_key(k))
0060             self.assert_(k in d)
0061         for k in self.other:
0062             self.failIf(d.has_key(k))
0063             self.failIf(k in d)
0064         #cmp
0065         self.assertEqual(cmp(p,p), 0)
0066         self.assertEqual(cmp(d,d), 0)
0067         self.assertEqual(cmp(p,d), -1)
0068         self.assertEqual(cmp(d,p), 1)
0069         #__non__zero__
0070         if p: self.fail("Empty mapping must compare to False")
0071         if not d: self.fail("Full mapping must compare to True")
0072         # keys(), items(), iterkeys() ...
0073         def check_iterandlist(iter, lst, ref):
0074             self.assert_(hasattr(iter, 'next'))
0075             self.assert_(hasattr(iter, '__iter__'))
0076             x = list(iter)
0077             self.assert_(set(x)==set(lst)==set(ref))
0078         check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
0079         check_iterandlist(iter(d), d.keys(), self.reference.keys())
0080         check_iterandlist(d.itervalues(), d.values(), self.reference.values())
0081         check_iterandlist(d.iteritems(), d.items(), self.reference.items())
0082         #get
0083         key, value = d.iteritems().next()
0084         knownkey, knownvalue = self.other.iteritems().next()
0085         self.assertEqual(d.get(key, knownvalue), value)
0086         self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
0087         self.failIf(knownkey in d)
0088 
0089     def test_write(self):
0090         # Test for write operations on mapping
0091         p = self._empty_mapping()
0092         #Indexing
0093         for key, value in self.reference.items():
0094             p[key] = value
0095             self.assertEqual(p[key], value)
0096         for key in self.reference.keys():
0097             del p[key]
0098             self.failUnlessRaises(KeyError, lambda:p[key])
0099         p = self._empty_mapping()
0100         #update
0101         p.update(self.reference)
0102         self.assertEqual(dict(p), self.reference)
0103         items = p.items()
0104         p = self._empty_mapping()
0105         p.update(items)
0106         self.assertEqual(dict(p), self.reference)
0107         d = self._full_mapping(self.reference)
0108         #setdefault
0109         key, value = d.iteritems().next()
0110         knownkey, knownvalue = self.other.iteritems().next()
0111         self.assertEqual(d.setdefault(key, knownvalue), value)
0112         self.assertEqual(d[key], value)
0113         self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
0114         self.assertEqual(d[knownkey], knownvalue)
0115         #pop
0116         self.assertEqual(d.pop(knownkey), knownvalue)
0117         self.failIf(knownkey in d)
0118         self.assertRaises(KeyError, d.pop, knownkey)
0119         default = 909
0120         d[knownkey] = knownvalue
0121         self.assertEqual(d.pop(knownkey, default), knownvalue)
0122         self.failIf(knownkey in d)
0123         self.assertEqual(d.pop(knownkey, default), default)
0124         #popitem
0125         key, value = d.popitem()
0126         self.failIf(key in d)
0127         self.assertEqual(value, self.reference[key])
0128         p=self._empty_mapping()
0129         self.assertRaises(KeyError, p.popitem)
0130 
0131     def test_constructor(self):
0132         self.assertEqual(self._empty_mapping(), self._empty_mapping())
0133 
0134     def test_bool(self):
0135         self.assert_(not self._empty_mapping())
0136         self.assert_(self.reference)
0137         self.assert_(bool(self._empty_mapping()) is False)
0138         self.assert_(bool(self.reference) is True)
0139 
0140     def test_keys(self):
0141         d = self._empty_mapping()
0142         self.assertEqual(d.keys(), [])
0143         d = self.reference
0144         self.assert_(self.inmapping.keys()[0] in d.keys())
0145         self.assert_(self.other.keys()[0] not in d.keys())
0146         self.assertRaises(TypeError, d.keys, None)
0147 
0148     def test_values(self):
0149         d = self._empty_mapping()
0150         self.assertEqual(d.values(), [])
0151 
0152         self.assertRaises(TypeError, d.values, None)
0153 
0154     def test_items(self):
0155         d = self._empty_mapping()
0156         self.assertEqual(d.items(), [])
0157 
0158         self.assertRaises(TypeError, d.items, None)
0159 
0160     def test_len(self):
0161         d = self._empty_mapping()
0162         self.assertEqual(len(d), 0)
0163 
0164     def test_getitem(self):
0165         d = self.reference
0166         self.assertEqual(d[self.inmapping.keys()[0]], self.inmapping.values()[0])
0167 
0168         self.assertRaises(TypeError, d.__getitem__)
0169 
0170     def test_update(self):
0171         # mapping argument
0172         d = self._empty_mapping()
0173         d.update(self.other)
0174         self.assertEqual(d.items(), self.other.items())
0175 
0176         # No argument
0177         d = self._empty_mapping()
0178         d.update()
0179         self.assertEqual(d, self._empty_mapping())
0180 
0181         # item sequence
0182         d = self._empty_mapping()
0183         d.update(self.other.items())
0184         self.assertEqual(d.items(), self.other.items())
0185 
0186         # Iterator
0187         d = self._empty_mapping()
0188         d.update(self.other.iteritems())
0189         self.assertEqual(d.items(), self.other.items())
0190 
0191         # FIXME: Doesn't work with UserDict
0192         # self.assertRaises((TypeError, AttributeError), d.update, None)
0193         self.assertRaises((TypeError, AttributeError), d.update, 42)
0194 
0195         outerself = self
0196         class SimpleUserDict:
0197             def __init__(self):
0198                 self.d = outerself.reference
0199             def keys(self):
0200                 return self.d.keys()
0201             def __getitem__(self, i):
0202                 return self.d[i]
0203         d.clear()
0204         d.update(SimpleUserDict())
0205         i1 = d.items()
0206         i2 = self.reference.items()
0207         i1.sort()
0208         i2.sort()
0209         self.assertEqual(i1, i2)
0210 
0211         class Exc(Exception): pass
0212 
0213         d = self._empty_mapping()
0214         class FailingUserDict:
0215             def keys(self):
0216                 raise Exc
0217         self.assertRaises(Exc, d.update, FailingUserDict())
0218 
0219         d.clear()
0220 
0221         class FailingUserDict:
0222             def keys(self):
0223                 class BogonIter:
0224                     def __init__(self):
0225                         self.i = 1
0226                     def __iter__(self):
0227                         return self
0228                     def next(self):
0229                         if self.i:
0230                             self.i = 0
0231                             return 'a'
0232                         raise Exc
0233                 return BogonIter()
0234             def __getitem__(self, key):
0235                 return key
0236         self.assertRaises(Exc, d.update, FailingUserDict())
0237 
0238         class FailingUserDict:
0239             def keys(self):
0240                 class BogonIter:
0241                     def __init__(self):
0242                         self.i = ord('a')
0243                     def __iter__(self):
0244                         return self
0245                     def next(self):
0246                         if self.i <= ord('z'):
0247                             rtn = chr(self.i)
0248                             self.i += 1
0249                             return rtn
0250                         raise StopIteration
0251                 return BogonIter()
0252             def __getitem__(self, key):
0253                 raise Exc
0254         self.assertRaises(Exc, d.update, FailingUserDict())
0255 
0256         d = self._empty_mapping()
0257         class badseq(object):
0258             def __iter__(self):
0259                 return self
0260             def next(self):
0261                 raise Exc()
0262 
0263         self.assertRaises(Exc, d.update, badseq())
0264 
0265         self.assertRaises(ValueError, d.update, [(1, 2, 3)])
0266 
0267     # no test_fromkeys or test_copy as both os.environ and selves don't support it
0268 
0269     def test_get(self):
0270         d = self._empty_mapping()
0271         self.assert_(d.get(self.other.keys()[0]) is None)
0272         self.assertEqual(d.get(self.other.keys()[0], 3), 3)
0273         d = self.reference
0274         self.assert_(d.get(self.other.keys()[0]) is None)
0275         self.assertEqual(d.get(self.other.keys()[0], 3), 3)
0276         self.assertEqual(d.get(self.inmapping.keys()[0]), self.inmapping.values()[0])
0277         self.assertEqual(d.get(self.inmapping.keys()[0], 3), self.inmapping.values()[0])
0278         self.assertRaises(TypeError, d.get)
0279         self.assertRaises(TypeError, d.get, None, None, None)
0280 
0281     def test_setdefault(self):
0282         d = self._empty_mapping()
0283         self.assertRaises(TypeError, d.setdefault)
0284 
0285     def test_popitem(self):
0286         d = self._empty_mapping()
0287         self.assertRaises(KeyError, d.popitem)
0288         self.assertRaises(TypeError, d.popitem, 42)
0289 
0290     def test_pop(self):
0291         d = self._empty_mapping()
0292         k, v = self.inmapping.items()[0]
0293         d[k] = v
0294         self.assertRaises(KeyError, d.pop, self.other.keys()[0])
0295 
0296         self.assertEqual(d.pop(k), v)
0297         self.assertEqual(len(d), 0)
0298 
0299         self.assertRaises(KeyError, d.pop, k)
0300 
0301 
0302 class TestMappingProtocol(BasicTestMappingProtocol):
0303     def test_constructor(self):
0304         BasicTestMappingProtocol.test_constructor(self)
0305         self.assert_(self._empty_mapping() is not self._empty_mapping())
0306         self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2})
0307 
0308     def test_bool(self):
0309         BasicTestMappingProtocol.test_bool(self)
0310         self.assert_(not self._empty_mapping())
0311         self.assert_(self._full_mapping({"x": "y"}))
0312         self.assert_(bool(self._empty_mapping()) is False)
0313         self.assert_(bool(self._full_mapping({"x": "y"})) is True)
0314 
0315     def test_keys(self):
0316         BasicTestMappingProtocol.test_keys(self)
0317         d = self._empty_mapping()
0318         self.assertEqual(d.keys(), [])
0319         d = self._full_mapping({'a': 1, 'b': 2})
0320         k = d.keys()
0321         self.assert_('a' in k)
0322         self.assert_('b' in k)
0323         self.assert_('c' not in k)
0324 
0325     def test_values(self):
0326         BasicTestMappingProtocol.test_values(self)
0327         d = self._full_mapping({1:2})
0328         self.assertEqual(d.values(), [2])
0329 
0330     def test_items(self):
0331         BasicTestMappingProtocol.test_items(self)
0332 
0333         d = self._full_mapping({1:2})
0334         self.assertEqual(d.items(), [(1, 2)])
0335 
0336     def test_has_key(self):
0337         d = self._empty_mapping()
0338         self.assert_(not d.has_key('a'))
0339         d = self._full_mapping({'a': 1, 'b': 2})
0340         k = d.keys()
0341         k.sort()
0342         self.assertEqual(k, ['a', 'b'])
0343 
0344         self.assertRaises(TypeError, d.has_key)
0345 
0346     def test_contains(self):
0347         d = self._empty_mapping()
0348         self.assert_(not ('a' in d))
0349         self.assert_('a' not in d)
0350         d = self._full_mapping({'a': 1, 'b': 2})
0351         self.assert_('a' in d)
0352         self.assert_('b' in d)
0353         self.assert_('c' not in d)
0354 
0355         self.assertRaises(TypeError, d.__contains__)
0356 
0357     def test_len(self):
0358         BasicTestMappingProtocol.test_len(self)
0359         d = self._full_mapping({'a': 1, 'b': 2})
0360         self.assertEqual(len(d), 2)
0361 
0362     def test_getitem(self):
0363         BasicTestMappingProtocol.test_getitem(self)
0364         d = self._full_mapping({'a': 1, 'b': 2})
0365         self.assertEqual(d['a'], 1)
0366         self.assertEqual(d['b'], 2)
0367         d['c'] = 3
0368         d['a'] = 4
0369         self.assertEqual(d['c'], 3)
0370         self.assertEqual(d['a'], 4)
0371         del d['b']
0372         self.assertEqual(d, {'a': 4, 'c': 3})
0373 
0374         self.assertRaises(TypeError, d.__getitem__)
0375 
0376     def test_clear(self):
0377         d = self._full_mapping({1:1, 2:2, 3:3})
0378         d.clear()
0379         self.assertEqual(d, {})
0380 
0381         self.assertRaises(TypeError, d.clear, None)
0382 
0383     def test_update(self):
0384         BasicTestMappingProtocol.test_update(self)
0385         # mapping argument
0386         d = self._empty_mapping()
0387         d.update({1:100})
0388         d.update({2:20})
0389         d.update({1:1, 2:2, 3:3})
0390         self.assertEqual(d, {1:1, 2:2, 3:3})
0391 
0392         # no argument
0393         d.update()
0394         self.assertEqual(d, {1:1, 2:2, 3:3})
0395 
0396         # keyword arguments
0397         d = self._empty_mapping()
0398         d.update(x=100)
0399         d.update(y=20)
0400         d.update(x=1, y=2, z=3)
0401         self.assertEqual(d, {"x":1, "y":2, "z":3})
0402 
0403         # item sequence
0404         d = self._empty_mapping()
0405         d.update([("x", 100), ("y", 20)])
0406         self.assertEqual(d, {"x":100, "y":20})
0407 
0408         # Both item sequence and keyword arguments
0409         d = self._empty_mapping()
0410         d.update([("x", 100), ("y", 20)], x=1, y=2)
0411         self.assertEqual(d, {"x":1, "y":2})
0412 
0413         # iterator
0414         d = self._full_mapping({1:3, 2:4})
0415         d.update(self._full_mapping({1:2, 3:4, 5:6}).iteritems())
0416         self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
0417 
0418         class SimpleUserDict:
0419             def __init__(self):
0420                 self.d = {1:1, 2:2, 3:3}
0421             def keys(self):
0422                 return self.d.keys()
0423             def __getitem__(self, i):
0424                 return self.d[i]
0425         d.clear()
0426         d.update(SimpleUserDict())
0427         self.assertEqual(d, {1:1, 2:2, 3:3})
0428 
0429     def test_fromkeys(self):
0430         self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
0431         d = self._empty_mapping()
0432         self.assert_(not(d.fromkeys('abc') is d))
0433         self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
0434         self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
0435         self.assertEqual(d.fromkeys([]), {})
0436         def g():
0437             yield 1
0438         self.assertEqual(d.fromkeys(g()), {1:None})
0439         self.assertRaises(TypeError, {}.fromkeys, 3)
0440         class dictlike(self.type2test): pass
0441         self.assertEqual(dictlike.fromkeys('a'), {'a':None})
0442         self.assertEqual(dictlike().fromkeys('a'), {'a':None})
0443         self.assert_(dictlike.fromkeys('a').__class__ is dictlike)
0444         self.assert_(dictlike().fromkeys('a').__class__ is dictlike)
0445         # FIXME: the following won't work with UserDict, because it's an old style class
0446         # self.assert_(type(dictlike.fromkeys('a')) is dictlike)
0447         class mydict(self.type2test):
0448             def __new__(cls):
0449                 return UserDict.UserDict()
0450         ud = mydict.fromkeys('ab')
0451         self.assertEqual(ud, {'a':None, 'b':None})
0452         # FIXME: the following won't work with UserDict, because it's an old style class
0453         # self.assert_(isinstance(ud, UserDict.UserDict))
0454         self.assertRaises(TypeError, dict.fromkeys)
0455 
0456         class Exc(Exception): pass
0457 
0458         class baddict1(self.type2test):
0459             def __init__(self):
0460                 raise Exc()
0461 
0462         self.assertRaises(Exc, baddict1.fromkeys, [1])
0463 
0464         class BadSeq(object):
0465             def __iter__(self):
0466                 return self
0467             def next(self):
0468                 raise Exc()
0469 
0470         self.assertRaises(Exc, self.type2test.fromkeys, BadSeq())
0471 
0472         class baddict2(self.type2test):
0473             def __setitem__(self, key, value):
0474                 raise Exc()
0475 
0476         self.assertRaises(Exc, baddict2.fromkeys, [1])
0477 
0478     def test_copy(self):
0479         d = self._full_mapping({1:1, 2:2, 3:3})
0480         self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
0481         d = self._empty_mapping()
0482         self.assertEqual(d.copy(), d)
0483         self.assert_(isinstance(d.copy(), d.__class__))
0484         self.assertRaises(TypeError, d.copy, None)
0485 
0486     def test_get(self):
0487         BasicTestMappingProtocol.test_get(self)
0488         d = self._empty_mapping()
0489         self.assert_(d.get('c') is None)
0490         self.assertEqual(d.get('c', 3), 3)
0491         d = self._full_mapping({'a' : 1, 'b' : 2})
0492         self.assert_(d.get('c') is None)
0493         self.assertEqual(d.get('c', 3), 3)
0494         self.assertEqual(d.get('a'), 1)
0495         self.assertEqual(d.get('a', 3), 1)
0496 
0497     def test_setdefault(self):
0498         BasicTestMappingProtocol.test_setdefault(self)
0499         d = self._empty_mapping()
0500         self.assert_(d.setdefault('key0') is None)
0501         d.setdefault('key0', [])
0502         self.assert_(d.setdefault('key0') is None)
0503         d.setdefault('key', []).append(3)
0504         self.assertEqual(d['key'][0], 3)
0505         d.setdefault('key', []).append(4)
0506         self.assertEqual(len(d['key']), 2)
0507 
0508     def test_popitem(self):
0509         BasicTestMappingProtocol.test_popitem(self)
0510         for copymode in -1, +1:
0511             # -1: b has same structure as a
0512             # +1: b is a.copy()
0513             for log2size in range(12):
0514                 size = 2**log2size
0515                 a = self._empty_mapping()
0516                 b = self._empty_mapping()
0517                 for i in range(size):
0518                     a[repr(i)] = i
0519                     if copymode < 0:
0520                         b[repr(i)] = i
0521                 if copymode > 0:
0522                     b = a.copy()
0523                 for i in range(size):
0524                     ka, va = ta = a.popitem()
0525                     self.assertEqual(va, int(ka))
0526                     kb, vb = tb = b.popitem()
0527                     self.assertEqual(vb, int(kb))
0528                     self.assert_(not(copymode < 0 and ta != tb))
0529                 self.assert_(not a)
0530                 self.assert_(not b)
0531 
0532     def test_pop(self):
0533         BasicTestMappingProtocol.test_pop(self)
0534 
0535         # Tests for pop with specified key
0536         d = self._empty_mapping()
0537         k, v = 'abc', 'def'
0538 
0539         # verify longs/ints get same value when key > 32 bits (for 64-bit archs)
0540         # see SF bug #689659
0541         x = 4503599627370496L
0542         y = 4503599627370496
0543         h = self._full_mapping({x: 'anything', y: 'something else'})
0544         self.assertEqual(h[x], h[y])
0545 
0546         self.assertEqual(d.pop(k, v), v)
0547         d[k] = v
0548         self.assertEqual(d.pop(k, 1), v)
0549 
0550 
0551 class TestHashMappingProtocol(TestMappingProtocol):
0552 
0553     def test_getitem(self):
0554         TestMappingProtocol.test_getitem(self)
0555         class Exc(Exception): pass
0556 
0557         class BadEq(object):
0558             def __eq__(self, other):
0559                 raise Exc()
0560 
0561         d = self._empty_mapping()
0562         d[BadEq()] = 42
0563         self.assertRaises(KeyError, d.__getitem__, 23)
0564 
0565         class BadHash(object):
0566             fail = False
0567             def __hash__(self):
0568                 if self.fail:
0569                     raise Exc()
0570                 else:
0571                     return 42
0572 
0573         d = self._empty_mapping()
0574         x = BadHash()
0575         d[x] = 42
0576         x.fail = True
0577         self.assertRaises(Exc, d.__getitem__, x)
0578 
0579     def test_fromkeys(self):
0580         TestMappingProtocol.test_fromkeys(self)
0581         class mydict(self.type2test):
0582             def __new__(cls):
0583                 return UserDict.UserDict()
0584         ud = mydict.fromkeys('ab')
0585         self.assertEqual(ud, {'a':None, 'b':None})
0586         self.assert_(isinstance(ud, UserDict.UserDict))
0587 
0588     def test_pop(self):
0589         TestMappingProtocol.test_pop(self)
0590 
0591         class Exc(Exception): pass
0592 
0593         class BadHash(object):
0594             fail = False
0595             def __hash__(self):
0596                 if self.fail:
0597                     raise Exc()
0598                 else:
0599                     return 42
0600 
0601         d = self._empty_mapping()
0602         x = BadHash()
0603         d[x] = 42
0604         x.fail = True
0605         self.assertRaises(Exc, d.pop, x)
0606 
0607     def test_mutatingiteration(self):
0608         d = self._empty_mapping()
0609         d[1] = 1
0610         try:
0611             for i in d:
0612                 d[i+1] = 1
0613         except RuntimeError:
0614             pass
0615         else:
0616             self.fail("changing dict size during iteration doesn't raise Error")
0617 
0618     def test_repr(self):
0619         d = self._empty_mapping()
0620         self.assertEqual(repr(d), '{}')
0621         d[1] = 2
0622         self.assertEqual(repr(d), '{1: 2}')
0623         d = self._empty_mapping()
0624         d[1] = d
0625         self.assertEqual(repr(d), '{1: {...}}')
0626 
0627         class Exc(Exception): pass
0628 
0629         class BadRepr(object):
0630             def __repr__(self):
0631                 raise Exc()
0632 
0633         d = self._full_mapping({1: BadRepr()})
0634         self.assertRaises(Exc, repr, d)
0635 
0636     def test_le(self):
0637         self.assert_(not (self._empty_mapping() < self._empty_mapping()))
0638         self.assert_(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
0639 
0640         class Exc(Exception): pass
0641 
0642         class BadCmp(object):
0643             def __eq__(self, other):
0644                 raise Exc()
0645 
0646         d1 = self._full_mapping({BadCmp(): 1})
0647         d2 = self._full_mapping({1: 1})
0648         try:
0649             d1 < d2
0650         except Exc:
0651             pass
0652         else:
0653             self.fail("< didn't raise Exc")
0654 
0655     def test_setdefault(self):
0656         TestMappingProtocol.test_setdefault(self)
0657 
0658         class Exc(Exception): pass
0659 
0660         class BadHash(object):
0661             fail = False
0662             def __hash__(self):
0663                 if self.fail:
0664                     raise Exc()
0665                 else:
0666                     return 42
0667 
0668         d = self._empty_mapping()
0669         x = BadHash()
0670         d[x] = 42
0671         x.fail = True
0672         self.assertRaises(Exc, d.setdefault, x, [])
0673 

Generated by PyXR 0.9.4
SourceForge.net Logo