Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
test.test_dict.DictTest Class Reference

List of all members.

Public Member Functions

def test_invalid_keyword_arguments
def test_constructor
def test_literal_constructor
def test_bool
def test_keys
def test_values
def test_items
def test_contains
def test_len
def test_getitem
def test_clear
def test_update
def test_fromkeys
def test_copy
def test_get
def test_setdefault
def test_popitem
def test_pop
def test_mutatingiteration
def test_repr
def test_eq
def test_keys_contained
def helper_keys_contained
def test_errors_in_view_containment_check
def test_dictview_set_operations_on_keys
def test_dictview_set_operations_on_items
def test_dictview_mixed_set_operations
def test_missing
def test_tuple_keyerror
def test_bad_key
def test_resize1
def test_resize2
def test_empty_presized_dict_in_freelist
def test_container_iterator
def test_track_literals
def test_track_dynamic
def test_track_subtypes

Public Attributes

 d
 i

Private Member Functions

def _not_tracked
def _tracked

Private Attributes

 __missing__

Detailed Description

Definition at line 8 of file test_dict.py.


Member Function Documentation

def test.test_dict.DictTest._not_tracked (   self,
  t 
) [private]

Definition at line 658 of file test_dict.py.

00658 
00659     def _not_tracked(self, t):
00660         # Nested containers can take several collections to untrack
00661         gc.collect()
00662         gc.collect()
00663         self.assertFalse(gc.is_tracked(t), t)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_dict.DictTest._tracked (   self,
  t 
) [private]

Definition at line 664 of file test_dict.py.

00664 
00665     def _tracked(self, t):
00666         self.assertTrue(gc.is_tracked(t), t)
00667         gc.collect()
00668         gc.collect()
00669         self.assertTrue(gc.is_tracked(t), t)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 410 of file test_dict.py.

00410 
00411     def helper_keys_contained(self, fn):
00412         # Test rich comparisons against dict key views, which should behave the
00413         # same as sets.
00414         empty = fn(dict())
00415         empty2 = fn(dict())
00416         smaller = fn({1:1, 2:2})
00417         larger = fn({1:1, 2:2, 3:3})
00418         larger2 = fn({1:1, 2:2, 3:3})
00419         larger3 = fn({4:1, 2:2, 3:3})
00420 
00421         self.assertTrue(smaller <  larger)
00422         self.assertTrue(smaller <= larger)
00423         self.assertTrue(larger >  smaller)
00424         self.assertTrue(larger >= smaller)
00425 
00426         self.assertFalse(smaller >= larger)
00427         self.assertFalse(smaller >  larger)
00428         self.assertFalse(larger  <= smaller)
00429         self.assertFalse(larger  <  smaller)
00430 
00431         self.assertFalse(smaller <  larger3)
00432         self.assertFalse(smaller <= larger3)
00433         self.assertFalse(larger3 >  smaller)
00434         self.assertFalse(larger3 >= smaller)
00435 
00436         # Inequality strictness
00437         self.assertTrue(larger2 >= larger)
00438         self.assertTrue(larger2 <= larger)
00439         self.assertFalse(larger2 > larger)
00440         self.assertFalse(larger2 < larger)
00441 
00442         self.assertTrue(larger == larger2)
00443         self.assertTrue(smaller != larger)
00444 
00445         # There is an optimization on the zero-element case.
00446         self.assertTrue(empty == empty2)
00447         self.assertFalse(empty != empty2)
00448         self.assertFalse(empty == smaller)
00449         self.assertTrue(empty != smaller)
00450 
00451         # With the same size, an elementwise compare happens
00452         self.assertTrue(larger != larger3)
00453         self.assertFalse(larger == larger3)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 568 of file test_dict.py.

00568 
00569     def test_bad_key(self):
00570         # Dictionary lookups should fail if __eq__() raises an exception.
00571         class CustomException(Exception):
00572             pass
00573 
00574         class BadDictKey:
00575             def __hash__(self):
00576                 return hash(self.__class__)
00577 
00578             def __eq__(self, other):
00579                 if isinstance(other, self.__class__):
00580                     raise CustomException
00581                 return other
00582 
00583         d = {}
00584         x1 = BadDictKey()
00585         x2 = BadDictKey()
00586         d[x1] = 1
00587         for stmt in ['d[x2] = 2',
00588                      'z = d[x2]',
00589                      'x2 in d',
00590                      'd.get(x2)',
00591                      'd.setdefault(x2, 42)',
00592                      'd.pop(x2)',
00593                      'd.update({x2: 2})']:
00594             with self.assertRaises(CustomException):
00595                 exec(stmt, locals())

Definition at line 35 of file test_dict.py.

00035 
00036     def test_bool(self):
00037         self.assertIs(not {}, True)
00038         self.assertTrue({1: 2})
00039         self.assertIs(bool({}), False)
00040         self.assertIs(bool({1: 2}), True)

Here is the call graph for this function:

Definition at line 124 of file test_dict.py.

00124 
00125     def test_clear(self):
00126         d = {1:1, 2:2, 3:3}
00127         d.clear()
00128         self.assertEqual(d, {})
00129 
00130         self.assertRaises(TypeError, d.clear, None)

Here is the call graph for this function:

Definition at line 19 of file test_dict.py.

00019 
00020     def test_constructor(self):
00021         # calling built-in types without argument must return empty
00022         self.assertEqual(dict(), {})
00023         self.assertIsNot(dict(), {})

Here is the call graph for this function:

Definition at line 642 of file test_dict.py.

00642 
00643     def test_container_iterator(self):
00644         # Bug #3680: tp_traverse was not implemented for dictiter and
00645         # dictview objects.
00646         class C(object):
00647             pass
00648         views = (dict.items, dict.values, dict.keys)
00649         for v in views:
00650             obj = C()
00651             ref = weakref.ref(obj)
00652             container = {obj: 1}
00653             obj.v = v(container)
00654             obj.x = iter(obj.v)
00655             del obj, container
00656             gc.collect()
00657             self.assertIs(ref(), None, "Cycle was not collected")

Definition at line 68 of file test_dict.py.

00068 
00069     def test_contains(self):
00070         d = {}
00071         self.assertNotIn('a', d)
00072         self.assertFalse('a' in d)
00073         self.assertTrue('a' not in d)
00074         d = {'a': 1, 'b': 2}
00075         self.assertIn('a', d)
00076         self.assertIn('b', d)
00077         self.assertNotIn('c', d)
00078 
00079         self.assertRaises(TypeError, d.__contains__)

Here is the call graph for this function:

Definition at line 257 of file test_dict.py.

00257 
00258     def test_copy(self):
00259         d = {1:1, 2:2, 3:3}
00260         self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
00261         self.assertEqual({}.copy(), {})
00262         self.assertRaises(TypeError, d.copy, None)

Here is the call graph for this function:

Definition at line 506 of file test_dict.py.

00506 
00507     def test_dictview_mixed_set_operations(self):
00508         # Just a few for .keys()
00509         self.assertTrue({1:1}.keys() == {1})
00510         self.assertTrue({1} == {1:1}.keys())
00511         self.assertEqual({1:1}.keys() | {2}, {1, 2})
00512         self.assertEqual({2} | {1:1}.keys(), {1, 2})
00513         # And a few for .items()
00514         self.assertTrue({1:1}.items() == {(1,1)})
00515         self.assertTrue({(1,1)} == {1:1}.items())
00516         self.assertEqual({1:1}.items() | {2}, {(1,1), 2})
00517         self.assertEqual({2} | {1:1}.items(), {(1,1), 2})

Here is the call graph for this function:

Definition at line 491 of file test_dict.py.

00491 
00492     def test_dictview_set_operations_on_items(self):
00493         k1 = {1:1, 2:2}.items()
00494         k2 = {1:1, 2:2, 3:3}.items()
00495         k3 = {4:4}.items()
00496 
00497         self.assertEqual(k1 - k2, set())
00498         self.assertEqual(k1 - k3, {(1,1), (2,2)})
00499         self.assertEqual(k2 - k1, {(3,3)})
00500         self.assertEqual(k3 - k1, {(4,4)})
00501         self.assertEqual(k1 & k2, {(1,1), (2,2)})
00502         self.assertEqual(k1 & k3, set())
00503         self.assertEqual(k1 | k2, {(1,1), (2,2), (3,3)})
00504         self.assertEqual(k1 ^ k2, {(3,3)})
00505         self.assertEqual(k1 ^ k3, {(1,1), (2,2), (4,4)})

Here is the call graph for this function:

Definition at line 476 of file test_dict.py.

00476 
00477     def test_dictview_set_operations_on_keys(self):
00478         k1 = {1:1, 2:2}.keys()
00479         k2 = {1:1, 2:2, 3:3}.keys()
00480         k3 = {4:4}.keys()
00481 
00482         self.assertEqual(k1 - k2, set())
00483         self.assertEqual(k1 - k3, {1,2})
00484         self.assertEqual(k2 - k1, {3})
00485         self.assertEqual(k3 - k1, {4})
00486         self.assertEqual(k1 & k2, {1,2})
00487         self.assertEqual(k1 & k3, set())
00488         self.assertEqual(k1 | k2, {1,2,3})
00489         self.assertEqual(k1 ^ k2, {3})
00490         self.assertEqual(k1 ^ k3, {1,2,4})

Here is the call graph for this function:

Definition at line 634 of file test_dict.py.

00634 
00635     def test_empty_presized_dict_in_freelist(self):
00636         # Bug #3537: if an empty but presized dict with a size larger
00637         # than 7 was in the freelist, it triggered an assertion failure
00638         with self.assertRaises(ZeroDivisionError):
00639             d = {'a': 1 // 0, 'b': None, 'c': None, 'd': None, 'e': None,
00640                  'f': None, 'g': None, 'h': None}
00641         d = {}

Here is the call graph for this function:

Definition at line 388 of file test_dict.py.

00388 
00389     def test_eq(self):
00390         self.assertEqual({}, {})
00391         self.assertEqual({1: 2}, {1: 2})
00392 
00393         class Exc(Exception): pass
00394 
00395         class BadCmp(object):
00396             def __eq__(self, other):
00397                 raise Exc()
00398             def __hash__(self):
00399                 return 1
00400 
00401         d1 = {BadCmp(): 1}
00402         d2 = {1: 1}
00403 
00404         with self.assertRaises(Exc):
00405             d1 == d2

Here is the call graph for this function:

Definition at line 454 of file test_dict.py.

00454 
00455     def test_errors_in_view_containment_check(self):
00456         class C:
00457             def __eq__(self, other):
00458                 raise RuntimeError
00459 
00460         d1 = {1: C()}
00461         d2 = {1: C()}
00462         with self.assertRaises(RuntimeError):
00463             d1.items() == d2.items()
00464         with self.assertRaises(RuntimeError):
00465             d1.items() != d2.items()
00466         with self.assertRaises(RuntimeError):
00467             d1.items() <= d2.items()
00468         with self.assertRaises(RuntimeError):
00469             d1.items() >= d2.items()
00470 
00471         d3 = {1: C(), 2: C()}
00472         with self.assertRaises(RuntimeError):
00473             d2.items() < d3.items()
00474         with self.assertRaises(RuntimeError):
00475             d3.items() > d2.items()

Here is the call graph for this function:

Definition at line 207 of file test_dict.py.

00207 
00208     def test_fromkeys(self):
00209         self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
00210         d = {}
00211         self.assertIsNot(d.fromkeys('abc'), d)
00212         self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
00213         self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
00214         self.assertEqual(d.fromkeys([]), {})
00215         def g():
00216             yield 1
00217         self.assertEqual(d.fromkeys(g()), {1:None})
00218         self.assertRaises(TypeError, {}.fromkeys, 3)
00219         class dictlike(dict): pass
00220         self.assertEqual(dictlike.fromkeys('a'), {'a':None})
00221         self.assertEqual(dictlike().fromkeys('a'), {'a':None})
00222         self.assertIsInstance(dictlike.fromkeys('a'), dictlike)
00223         self.assertIsInstance(dictlike().fromkeys('a'), dictlike)
00224         class mydict(dict):
00225             def __new__(cls):
00226                 return collections.UserDict()
00227         ud = mydict.fromkeys('ab')
00228         self.assertEqual(ud, {'a':None, 'b':None})
00229         self.assertIsInstance(ud, collections.UserDict)
00230         self.assertRaises(TypeError, dict.fromkeys)
00231 
00232         class Exc(Exception): pass
00233 
00234         class baddict1(dict):
00235             def __init__(self):
00236                 raise Exc()
00237 
00238         self.assertRaises(Exc, baddict1.fromkeys, [1])
00239 
00240         class BadSeq(object):
00241             def __iter__(self):
00242                 return self
00243             def __next__(self):
00244                 raise Exc()
00245 
00246         self.assertRaises(Exc, dict.fromkeys, BadSeq())
00247 
00248         class baddict2(dict):
00249             def __setitem__(self, key, value):
00250                 raise Exc()
00251 
00252         self.assertRaises(Exc, baddict2.fromkeys, [1])
00253 
00254         # test fast path for dictionary inputs
00255         d = dict(zip(range(6), range(6)))
00256         self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))

Here is the call graph for this function:

Definition at line 263 of file test_dict.py.

00263 
00264     def test_get(self):
00265         d = {}
00266         self.assertIs(d.get('c'), None)
00267         self.assertEqual(d.get('c', 3), 3)
00268         d = {'a': 1, 'b': 2}
00269         self.assertIs(d.get('c'), None)
00270         self.assertEqual(d.get('c', 3), 3)
00271         self.assertEqual(d.get('a'), 1)
00272         self.assertEqual(d.get('a', 3), 1)
00273         self.assertRaises(TypeError, d.get)
00274         self.assertRaises(TypeError, d.get, None, None, None)

Here is the call graph for this function:

Definition at line 86 of file test_dict.py.

00086 
00087     def test_getitem(self):
00088         d = {'a': 1, 'b': 2}
00089         self.assertEqual(d['a'], 1)
00090         self.assertEqual(d['b'], 2)
00091         d['c'] = 3
00092         d['a'] = 4
00093         self.assertEqual(d['c'], 3)
00094         self.assertEqual(d['a'], 4)
00095         del d['b']
00096         self.assertEqual(d, {'a': 4, 'c': 3})
00097 
00098         self.assertRaises(TypeError, d.__getitem__)
00099 
00100         class BadEq(object):
00101             def __eq__(self, other):
00102                 raise Exc()
00103             def __hash__(self):
00104                 return 24
00105 
00106         d = {}
00107         d[BadEq()] = 42
00108         self.assertRaises(KeyError, d.__getitem__, 23)
00109 
00110         class Exc(Exception): pass
00111 
00112         class BadHash(object):
00113             fail = False
00114             def __hash__(self):
00115                 if self.fail:
00116                     raise Exc()
00117                 else:
00118                     return 42
00119 
00120         x = BadHash()
00121         d[x] = 42
00122         x.fail = True
00123         self.assertRaises(Exc, d.__getitem__, x)

Here is the call graph for this function:

Definition at line 10 of file test_dict.py.

00010 
00011     def test_invalid_keyword_arguments(self):
00012         class Custom(dict):
00013             pass
00014         for invalid in {1 : 2}, Custom({1 : 2}):
00015             with self.assertRaises(TypeError):
00016                 dict(**invalid)
00017             with self.assertRaises(TypeError):
00018                 {}.update(**invalid)

Definition at line 59 of file test_dict.py.

00059 
00060     def test_items(self):
00061         d = {}
00062         self.assertEqual(set(d.items()), set())
00063 
00064         d = {1:2}
00065         self.assertEqual(set(d.items()), {(1, 2)})
00066         self.assertRaises(TypeError, d.items, None)
00067         self.assertEqual(repr(dict(a=1).items()), "dict_items([('a', 1)])")

Here is the call graph for this function:

Definition at line 41 of file test_dict.py.

00041 
00042     def test_keys(self):
00043         d = {}
00044         self.assertEqual(set(d.keys()), set())
00045         d = {'a': 1, 'b': 2}
00046         k = d.keys()
00047         self.assertIn('a', d)
00048         self.assertIn('b', d)
00049         self.assertRaises(TypeError, d.keys, None)
00050         self.assertEqual(repr(dict(a=1).keys()), "dict_keys(['a'])")

Here is the call graph for this function:

Definition at line 406 of file test_dict.py.

00406 
00407     def test_keys_contained(self):
00408         self.helper_keys_contained(lambda x: x.keys())
00409         self.helper_keys_contained(lambda x: x.items())

Here is the call graph for this function:

Definition at line 80 of file test_dict.py.

00080 
00081     def test_len(self):
00082         d = {}
00083         self.assertEqual(len(d), 0)
00084         d = {'a': 1, 'b': 2}
00085         self.assertEqual(len(d), 2)

Here is the call graph for this function:

Definition at line 24 of file test_dict.py.

00024 
00025     def test_literal_constructor(self):
00026         # check literal constructor for different sized dicts
00027         # (to exercise the BUILD_MAP oparg).
00028         for n in (0, 1, 6, 256, 400):
00029             items = [(''.join(random.sample(string.ascii_letters, 8)), i)
00030                      for i in range(n)]
00031             random.shuffle(items)
00032             formatted_items = ('{!r}: {:d}'.format(k, v) for k, v in items)
00033             dictliteral = '{' + ', '.join(formatted_items) + '}'
00034             self.assertEqual(eval(dictliteral), dict(items))

Here is the call graph for this function:

Definition at line 518 of file test_dict.py.

00518 
00519     def test_missing(self):
00520         # Make sure dict doesn't have a __missing__ method
00521         self.assertFalse(hasattr(dict, "__missing__"))
00522         self.assertFalse(hasattr({}, "__missing__"))
00523         # Test several cases:
00524         # (D) subclass defines __missing__ method returning a value
00525         # (E) subclass defines __missing__ method raising RuntimeError
00526         # (F) subclass sets __missing__ instance variable (no effect)
00527         # (G) subclass doesn't define __missing__ at a all
00528         class D(dict):
00529             def __missing__(self, key):
00530                 return 42
00531         d = D({1: 2, 3: 4})
00532         self.assertEqual(d[1], 2)
00533         self.assertEqual(d[3], 4)
00534         self.assertNotIn(2, d)
00535         self.assertNotIn(2, d.keys())
00536         self.assertEqual(d[2], 42)
00537 
00538         class E(dict):
00539             def __missing__(self, key):
00540                 raise RuntimeError(key)
00541         e = E()
00542         with self.assertRaises(RuntimeError) as c:
00543             e[42]
00544         self.assertEqual(c.exception.args, (42,))
00545 
00546         class F(dict):
00547             def __init__(self):
00548                 # An instance variable __missing__ should have no effect
00549                 self.__missing__ = lambda key: None
00550         f = F()
00551         with self.assertRaises(KeyError) as c:
00552             f[42]
00553         self.assertEqual(c.exception.args, (42,))
00554 
00555         class G(dict):
00556             pass
00557         g = G()
00558         with self.assertRaises(KeyError) as c:
00559             g[42]
00560         self.assertEqual(c.exception.args, (42,))

Here is the call graph for this function:

Definition at line 362 of file test_dict.py.

00362 
00363     def test_mutatingiteration(self):
00364         # changing dict size during iteration
00365         d = {}
00366         d[1] = 1
00367         with self.assertRaises(RuntimeError):
00368             for i in d:
00369                 d[i+1] = 1

Here is the call graph for this function:

Definition at line 329 of file test_dict.py.

00329 
00330     def test_pop(self):
00331         # Tests for pop with specified key
00332         d = {}
00333         k, v = 'abc', 'def'
00334         d[k] = v
00335         self.assertRaises(KeyError, d.pop, 'ghi')
00336 
00337         self.assertEqual(d.pop(k), v)
00338         self.assertEqual(len(d), 0)
00339 
00340         self.assertRaises(KeyError, d.pop, k)
00341 
00342         self.assertEqual(d.pop(k, v), v)
00343         d[k] = v
00344         self.assertEqual(d.pop(k, 1), v)
00345 
00346         self.assertRaises(TypeError, d.pop)
00347 
00348         class Exc(Exception): pass
00349 
00350         class BadHash(object):
00351             fail = False
00352             def __hash__(self):
00353                 if self.fail:
00354                     raise Exc()
00355                 else:
00356                     return 42
00357 
00358         x = BadHash()
00359         d[x] = 42
00360         x.fail = True
00361         self.assertRaises(Exc, d.pop, x)

Here is the call graph for this function:

Definition at line 302 of file test_dict.py.

00302 
00303     def test_popitem(self):
00304         # dict.popitem()
00305         for copymode in -1, +1:
00306             # -1: b has same structure as a
00307             # +1: b is a.copy()
00308             for log2size in range(12):
00309                 size = 2**log2size
00310                 a = {}
00311                 b = {}
00312                 for i in range(size):
00313                     a[repr(i)] = i
00314                     if copymode < 0:
00315                         b[repr(i)] = i
00316                 if copymode > 0:
00317                     b = a.copy()
00318                 for i in range(size):
00319                     ka, va = ta = a.popitem()
00320                     self.assertEqual(va, int(ka))
00321                     kb, vb = tb = b.popitem()
00322                     self.assertEqual(vb, int(kb))
00323                     self.assertFalse(copymode < 0 and ta != tb)
00324                 self.assertFalse(a)
00325                 self.assertFalse(b)
00326 
00327         d = {}
00328         self.assertRaises(KeyError, d.popitem)

Here is the call graph for this function:

Definition at line 370 of file test_dict.py.

00370 
00371     def test_repr(self):
00372         d = {}
00373         self.assertEqual(repr(d), '{}')
00374         d[1] = 2
00375         self.assertEqual(repr(d), '{1: 2}')
00376         d = {}
00377         d[1] = d
00378         self.assertEqual(repr(d), '{1: {...}}')
00379 
00380         class Exc(Exception): pass
00381 
00382         class BadRepr(object):
00383             def __repr__(self):
00384                 raise Exc()
00385 
00386         d = {1: BadRepr()}
00387         self.assertRaises(Exc, repr, d)

Here is the call graph for this function:

Definition at line 596 of file test_dict.py.

00596 
00597     def test_resize1(self):
00598         # Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
00599         # This version got an assert failure in debug build, infinite loop in
00600         # release build.  Unfortunately, provoking this kind of stuff requires
00601         # a mix of inserts and deletes hitting exactly the right hash codes in
00602         # exactly the right order, and I can't think of a randomized approach
00603         # that would be *likely* to hit a failing case in reasonable time.
00604 
00605         d = {}
00606         for i in range(5):
00607             d[i] = i
00608         for i in range(5):
00609             del d[i]
00610         for i in range(5, 9):  # i==8 was the problem
00611             d[i] = i

Definition at line 612 of file test_dict.py.

00612 
00613     def test_resize2(self):
00614         # Another dict resizing bug (SF bug #1456209).
00615         # This caused Segmentation faults or Illegal instructions.
00616 
00617         class X(object):
00618             def __hash__(self):
00619                 return 5
00620             def __eq__(self, other):
00621                 if resizing:
00622                     d.clear()
00623                 return False
00624         d = {}
00625         resizing = False
00626         d[X()] = 1
00627         d[X()] = 2
00628         d[X()] = 3
00629         d[X()] = 4
00630         d[X()] = 5
00631         # now trigger a resize
00632         resizing = True
00633         d[9] = 6

Definition at line 275 of file test_dict.py.

00275 
00276     def test_setdefault(self):
00277         # dict.setdefault()
00278         d = {}
00279         self.assertIs(d.setdefault('key0'), None)
00280         d.setdefault('key0', [])
00281         self.assertIs(d.setdefault('key0'), None)
00282         d.setdefault('key', []).append(3)
00283         self.assertEqual(d['key'][0], 3)
00284         d.setdefault('key', []).append(4)
00285         self.assertEqual(len(d['key']), 2)
00286         self.assertRaises(TypeError, d.setdefault)
00287 
00288         class Exc(Exception): pass
00289 
00290         class BadHash(object):
00291             fail = False
00292             def __hash__(self):
00293                 if self.fail:
00294                     raise Exc()
00295                 else:
00296                     return 42
00297 
00298         x = BadHash()
00299         d[x] = 42
00300         x.fail = True
00301         self.assertRaises(Exc, d.setdefault, x, [])

Here is the call graph for this function:

Definition at line 689 of file test_dict.py.

00689 
00690     def test_track_dynamic(self):
00691         # Test GC-optimization of dynamically-created dicts
00692         class MyObject(object):
00693             pass
00694         x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject()
00695 
00696         d = dict()
00697         self._not_tracked(d)
00698         d[1] = "a"
00699         self._not_tracked(d)
00700         d[y] = 2
00701         self._not_tracked(d)
00702         d[z] = 3
00703         self._not_tracked(d)
00704         self._not_tracked(d.copy())
00705         d[4] = w
00706         self._tracked(d)
00707         self._tracked(d.copy())
00708         d[4] = None
00709         self._not_tracked(d)
00710         self._not_tracked(d.copy())
00711 
00712         # dd isn't tracked right now, but it may mutate and therefore d
00713         # which contains it must be tracked.
00714         d = dict()
00715         dd = dict()
00716         d[1] = dd
00717         self._not_tracked(dd)
00718         self._tracked(d)
00719         dd[1] = d
00720         self._tracked(dd)
00721 
00722         d = dict.fromkeys([x, y, z])
00723         self._not_tracked(d)
00724         dd = dict()
00725         dd.update(d)
00726         self._not_tracked(dd)
00727         d = dict.fromkeys([x, y, z, o])
00728         self._tracked(d)
00729         dd = dict()
00730         dd.update(d)
00731         self._tracked(dd)
00732 
00733         d = dict(x=x, y=y, z=z)
00734         self._not_tracked(d)
00735         d = dict(x=x, y=y, z=z, w=w)
00736         self._tracked(d)
00737         d = dict()
00738         d.update(x=x, y=y, z=z)
00739         self._not_tracked(d)
00740         d.update(w=w)
00741         self._tracked(d)
00742 
00743         d = dict([(x, y), (z, 1)])
00744         self._not_tracked(d)
00745         d = dict([(x, y), (z, w)])
00746         self._tracked(d)
00747         d = dict()
00748         d.update([(x, y), (z, 1)])
00749         self._not_tracked(d)
00750         d.update([(x, y), (z, w)])
00751         self._tracked(d)

Definition at line 671 of file test_dict.py.

00671 
00672     def test_track_literals(self):
00673         # Test GC-optimization of dict literals
00674         x, y, z, w = 1.5, "a", (1, None), []
00675 
00676         self._not_tracked({})
00677         self._not_tracked({x:(), y:x, z:1})
00678         self._not_tracked({1: "a", "b": 2})
00679         self._not_tracked({1: 2, (None, True, False, ()): int})
00680         self._not_tracked({1: object()})
00681 
00682         # Dicts with mutable elements are always tracked, even if those
00683         # elements are not tracked right now.
00684         self._tracked({1: []})
00685         self._tracked({1: ([],)})
00686         self._tracked({1: {}})
00687         self._tracked({1: set()})

Here is the call graph for this function:

Definition at line 753 of file test_dict.py.

00753 
00754     def test_track_subtypes(self):
00755         # Dict subtypes are always tracked
00756         class MyDict(dict):
00757             pass
00758         self._tracked(MyDict())
00759 

Definition at line 561 of file test_dict.py.

00561 
00562     def test_tuple_keyerror(self):
00563         # SF #1576657
00564         d = {}
00565         with self.assertRaises(KeyError) as c:
00566             d[(1,)]
00567         self.assertEqual(c.exception.args, ((1,),))

Here is the call graph for this function:

Definition at line 131 of file test_dict.py.

00131 
00132     def test_update(self):
00133         d = {}
00134         d.update({1:100})
00135         d.update({2:20})
00136         d.update({1:1, 2:2, 3:3})
00137         self.assertEqual(d, {1:1, 2:2, 3:3})
00138 
00139         d.update()
00140         self.assertEqual(d, {1:1, 2:2, 3:3})
00141 
00142         self.assertRaises((TypeError, AttributeError), d.update, None)
00143 
00144         class SimpleUserDict:
00145             def __init__(self):
00146                 self.d = {1:1, 2:2, 3:3}
00147             def keys(self):
00148                 return self.d.keys()
00149             def __getitem__(self, i):
00150                 return self.d[i]
00151         d.clear()
00152         d.update(SimpleUserDict())
00153         self.assertEqual(d, {1:1, 2:2, 3:3})
00154 
00155         class Exc(Exception): pass
00156 
00157         d.clear()
00158         class FailingUserDict:
00159             def keys(self):
00160                 raise Exc
00161         self.assertRaises(Exc, d.update, FailingUserDict())
00162 
00163         class FailingUserDict:
00164             def keys(self):
00165                 class BogonIter:
00166                     def __init__(self):
00167                         self.i = 1
00168                     def __iter__(self):
00169                         return self
00170                     def __next__(self):
00171                         if self.i:
00172                             self.i = 0
00173                             return 'a'
00174                         raise Exc
00175                 return BogonIter()
00176             def __getitem__(self, key):
00177                 return key
00178         self.assertRaises(Exc, d.update, FailingUserDict())
00179 
00180         class FailingUserDict:
00181             def keys(self):
00182                 class BogonIter:
00183                     def __init__(self):
00184                         self.i = ord('a')
00185                     def __iter__(self):
00186                         return self
00187                     def __next__(self):
00188                         if self.i <= ord('z'):
00189                             rtn = chr(self.i)
00190                             self.i += 1
00191                             return rtn
00192                         raise StopIteration
00193                 return BogonIter()
00194             def __getitem__(self, key):
00195                 raise Exc
00196         self.assertRaises(Exc, d.update, FailingUserDict())
00197 
00198         class badseq(object):
00199             def __iter__(self):
00200                 return self
00201             def __next__(self):
00202                 raise Exc()
00203 
00204         self.assertRaises(Exc, {}.update, badseq())
00205 
00206         self.assertRaises(ValueError, {}.update, [(1, 2, 3)])

Here is the call graph for this function:

Definition at line 51 of file test_dict.py.

00051 
00052     def test_values(self):
00053         d = {}
00054         self.assertEqual(set(d.values()), set())
00055         d = {1:2}
00056         self.assertEqual(set(d.values()), {2})
00057         self.assertRaises(TypeError, d.values, None)
00058         self.assertEqual(repr(dict(a=1).values()), "dict_values([1])")

Here is the call graph for this function:


Member Data Documentation

Definition at line 548 of file test_dict.py.

Definition at line 145 of file test_dict.py.

Definition at line 166 of file test_dict.py.


The documentation for this class was generated from the following file: