Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_weakref.MappingTestCase Class Reference
Inheritance diagram for test.test_weakref.MappingTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_weakref.MappingTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_weak_values
def test_weak_keys
def test_weak_keyed_iters
def test_weak_valued_iters
def check_iters
def check_weak_destroy_while_iterating
def check_weak_destroy_and_mutate_while_iterating
def test_weak_keys_destroy_while_iterating
def test_weak_values_destroy_while_iterating
def test_make_weak_keyed_dict_from_dict
def test_make_weak_keyed_dict_from_weak_keyed_dict
def make_weak_keyed_dict
def test_make_weak_valued_dict_from_dict
def test_make_weak_valued_dict_from_weak_valued_dict
def make_weak_valued_dict
def check_popitem
def test_weak_valued_dict_popitem
def test_weak_keyed_dict_popitem
def check_setdefault
def test_weak_valued_dict_setdefault
def test_weak_keyed_dict_setdefault
def check_update
def test_weak_valued_dict_update
def test_weak_keyed_dict_update
def test_weak_keyed_delitem
def test_weak_valued_delitem
def test_weak_keyed_bad_delitem
def test_weak_keyed_cascading_deletes
def setUp
def callback

Public Attributes

 value
 cbcalled

Static Public Attributes

int COUNT = 10

Detailed Description

Definition at line 816 of file test_weakref.py.


Member Function Documentation

def test.test_weakref.TestBase.callback (   self,
  ref 
) [inherited]

Definition at line 40 of file test_weakref.py.

00040 
00041     def callback(self, ref):
00042         self.cbcalled += 1
00043 

Here is the caller graph for this function:

Definition at line 929 of file test_weakref.py.

00929 
00930     def check_iters(self, dict):
00931         # item iterator:
00932         items = list(dict.items())
00933         for item in dict.items():
00934             items.remove(item)
00935         self.assertFalse(items, "items() did not touch all items")
00936 
00937         # key iterator, via __iter__():
00938         keys = list(dict.keys())
00939         for k in dict:
00940             keys.remove(k)
00941         self.assertFalse(keys, "__iter__() did not touch all keys")
00942 
00943         # key iterator, via iterkeys():
00944         keys = list(dict.keys())
00945         for k in dict.keys():
00946             keys.remove(k)
00947         self.assertFalse(keys, "iterkeys() did not touch all keys")
00948 
00949         # value iterator:
00950         values = list(dict.values())
00951         for v in dict.values():
00952             values.remove(v)
00953         self.assertFalse(values,
00954                      "itervalues() did not touch all values")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_weakref.MappingTestCase.check_popitem (   self,
  klass,
  key1,
  value1,
  key2,
  value2 
)

Definition at line 1072 of file test_weakref.py.

01072 
01073     def check_popitem(self, klass, key1, value1, key2, value2):
01074         weakdict = klass()
01075         weakdict[key1] = value1
01076         weakdict[key2] = value2
01077         self.assertEqual(len(weakdict), 2)
01078         k, v = weakdict.popitem()
01079         self.assertEqual(len(weakdict), 1)
01080         if k is key1:
01081             self.assertTrue(v is value1)
01082         else:
01083             self.assertTrue(v is value2)
01084         k, v = weakdict.popitem()
01085         self.assertEqual(len(weakdict), 0)
01086         if k is key1:
01087             self.assertTrue(v is value1)
01088         else:
01089             self.assertTrue(v is value2)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_weakref.MappingTestCase.check_setdefault (   self,
  klass,
  key,
  value1,
  value2 
)

Definition at line 1098 of file test_weakref.py.

01098 
01099     def check_setdefault(self, klass, key, value1, value2):
01100         self.assertTrue(value1 is not value2,
01101                      "invalid test"
01102                      " -- value parameters must be distinct objects")
01103         weakdict = klass()
01104         o = weakdict.setdefault(key, value1)
01105         self.assertTrue(o is value1)
01106         self.assertIn(key, weakdict)
01107         self.assertTrue(weakdict.get(key) is value1)
01108         self.assertTrue(weakdict[key] is value1)
01109 
01110         o = weakdict.setdefault(key, value2)
01111         self.assertTrue(o is value1)
01112         self.assertIn(key, weakdict)
01113         self.assertTrue(weakdict.get(key) is value1)
01114         self.assertTrue(weakdict[key] is value1)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_weakref.MappingTestCase.check_update (   self,
  klass,
  dict 
)

Definition at line 1123 of file test_weakref.py.

01123 
01124     def check_update(self, klass, dict):
01125         #
01126         #  This exercises d.update(), len(d), d.keys(), k in d,
01127         #  d.get(), d[].
01128         #
01129         weakdict = klass()
01130         weakdict.update(dict)
01131         self.assertEqual(len(weakdict), len(dict))
01132         for k in weakdict.keys():
01133             self.assertIn(k, dict, "mysterious new key appeared in weak dict")
01134             v = dict.get(k)
01135             self.assertTrue(v is weakdict[k])
01136             self.assertTrue(v is weakdict.get(k))
01137         for k in dict.keys():
01138             self.assertIn(k, weakdict, "original key disappeared in weak dict")
01139             v = dict[k]
01140             self.assertTrue(v is weakdict[k])
01141             self.assertTrue(v is weakdict.get(k))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 968 of file test_weakref.py.

00968 
00969     def check_weak_destroy_and_mutate_while_iterating(self, dict, testcontext):
00970         # Check that we can explicitly mutate the weak dict without
00971         # interfering with delayed removal.
00972         # `testcontext` should create an iterator, destroy one of the
00973         # weakref'ed objects and then return a new key/value pair corresponding
00974         # to the destroyed object.
00975         with testcontext() as (k, v):
00976             self.assertNotIn(k, dict)
00977         with testcontext() as (k, v):
00978             self.assertRaises(KeyError, dict.__delitem__, k)
00979         self.assertNotIn(k, dict)
00980         with testcontext() as (k, v):
00981             self.assertRaises(KeyError, dict.pop, k)
00982         self.assertNotIn(k, dict)
00983         with testcontext() as (k, v):
00984             dict[k] = v
00985         self.assertEqual(dict[k], v)
00986         ddict = copy.copy(dict)
00987         with testcontext() as (k, v):
00988             dict.update(ddict)
00989         self.assertEqual(dict, ddict)
00990         with testcontext() as (k, v):
00991             dict.clear()
00992         self.assertEqual(len(dict), 0)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_weakref.MappingTestCase.check_weak_destroy_while_iterating (   self,
  dict,
  objects,
  iter_name 
)

Definition at line 955 of file test_weakref.py.

00955 
00956     def check_weak_destroy_while_iterating(self, dict, objects, iter_name):
00957         n = len(dict)
00958         it = iter(getattr(dict, iter_name)())
00959         next(it)             # Trigger internal iteration
00960         # Destroy an object
00961         del objects[-1]
00962         gc.collect()    # just in case
00963         # We have removed either the first consumed object, or another one
00964         self.assertIn(len(list(it)), [len(objects), len(objects) - 1])
00965         del it
00966         # The removal has been committed
00967         self.assertEqual(len(dict), n - 1)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1047 of file test_weakref.py.

01047 
01048     def make_weak_keyed_dict(self):
01049         dict = weakref.WeakKeyDictionary()
01050         objects = list(map(Object, range(self.COUNT)))
01051         for o in objects:
01052             dict[o] = o.arg
01053         return dict, objects

Here is the caller graph for this function:

Definition at line 1065 of file test_weakref.py.

01065 
01066     def make_weak_valued_dict(self):
01067         dict = weakref.WeakValueDictionary()
01068         objects = list(map(Object, range(self.COUNT)))
01069         for o in objects:
01070             dict[o.arg] = o
01071         return dict, objects

Here is the caller graph for this function:

def test.test_weakref.TestBase.setUp (   self) [inherited]

Definition at line 37 of file test_weakref.py.

00037 
00038     def setUp(self):
00039         self.cbcalled = 0

Here is the caller graph for this function:

Definition at line 1036 of file test_weakref.py.

01036 
01037     def test_make_weak_keyed_dict_from_dict(self):
01038         o = Object(3)
01039         dict = weakref.WeakKeyDictionary({o:364})
01040         self.assertEqual(dict[o], 364)

Here is the call graph for this function:

Definition at line 1041 of file test_weakref.py.

01041 
01042     def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
01043         o = Object(3)
01044         dict = weakref.WeakKeyDictionary({o:364})
01045         dict2 = weakref.WeakKeyDictionary(dict)
01046         self.assertEqual(dict[o], 364)

Here is the call graph for this function:

Definition at line 1054 of file test_weakref.py.

01054 
01055     def test_make_weak_valued_dict_from_dict(self):
01056         o = Object(3)
01057         dict = weakref.WeakValueDictionary({364:o})
01058         self.assertEqual(dict[364], o)

Here is the call graph for this function:

Definition at line 1059 of file test_weakref.py.

01059 
01060     def test_make_weak_valued_dict_from_weak_valued_dict(self):
01061         o = Object(3)
01062         dict = weakref.WeakValueDictionary({364:o})
01063         dict2 = weakref.WeakValueDictionary(dict)
01064         self.assertEqual(dict[364], o)

Here is the call graph for this function:

Definition at line 1172 of file test_weakref.py.

01172 
01173     def test_weak_keyed_bad_delitem(self):
01174         d = weakref.WeakKeyDictionary()
01175         o = Object('1')
01176         # An attempt to delete an object that isn't there should raise
01177         # KeyError.  It didn't before 2.3.
01178         self.assertRaises(KeyError, d.__delitem__, o)
01179         self.assertRaises(KeyError, d.__getitem__, o)
01180 
01181         # If a key isn't of a weakly referencable type, __getitem__ and
01182         # __setitem__ raise TypeError.  __delitem__ should too.
01183         self.assertRaises(TypeError, d.__delitem__,  13)
01184         self.assertRaises(TypeError, d.__getitem__,  13)
01185         self.assertRaises(TypeError, d.__setitem__,  13, 13)

Here is the call graph for this function:

Definition at line 1186 of file test_weakref.py.

01186 
01187     def test_weak_keyed_cascading_deletes(self):
01188         # SF bug 742860.  For some reason, before 2.3 __delitem__ iterated
01189         # over the keys via self.data.iterkeys().  If things vanished from
01190         # the dict during this (or got added), that caused a RuntimeError.
01191 
01192         d = weakref.WeakKeyDictionary()
01193         mutate = False
01194 
01195         class C(object):
01196             def __init__(self, i):
01197                 self.value = i
01198             def __hash__(self):
01199                 return hash(self.value)
01200             def __eq__(self, other):
01201                 if mutate:
01202                     # Side effect that mutates the dict, by removing the
01203                     # last strong reference to a key.
01204                     del objs[-1]
01205                 return self.value == other.value
01206 
01207         objs = [C(i) for i in range(4)]
01208         for o in objs:
01209             d[o] = o.value
01210         del o   # now the only strong references to keys are in objs
01211         # Find the order in which iterkeys sees the keys.
01212         objs = list(d.keys())
01213         # Reverse it, so that the iteration implementation of __delitem__
01214         # has to keep looping to find the first object we delete.
01215         objs.reverse()
01216 
01217         # Turn on mutation in C.__eq__.  The first time thru the loop,
01218         # under the iterkeys() business the first comparison will delete
01219         # the last item iterkeys() would see, and that causes a
01220         #     RuntimeError: dictionary changed size during iteration
01221         # when the iterkeys() loop goes around to try comparing the next
01222         # key.  After this was fixed, it just deletes the last object *our*
01223         # "for o in obj" loop would have gotten to.
01224         mutate = True
01225         count = 0
01226         for o in objs:
01227             count += 1
01228             del d[o]
01229         self.assertEqual(len(d), 0)
01230         self.assertEqual(count, 2)

Here is the call graph for this function:

Definition at line 1150 of file test_weakref.py.

01150 
01151     def test_weak_keyed_delitem(self):
01152         d = weakref.WeakKeyDictionary()
01153         o1 = Object('1')
01154         o2 = Object('2')
01155         d[o1] = 'something'
01156         d[o2] = 'something'
01157         self.assertEqual(len(d), 2)
01158         del d[o1]
01159         self.assertEqual(len(d), 1)
01160         self.assertEqual(list(d.keys()), [o2])

Here is the call graph for this function:

Definition at line 1094 of file test_weakref.py.

01094 
01095     def test_weak_keyed_dict_popitem(self):
01096         self.check_popitem(weakref.WeakKeyDictionary,
01097                            C(), "value 1", C(), "value 2")

Here is the call graph for this function:

Definition at line 1119 of file test_weakref.py.

01119 
01120     def test_weak_keyed_dict_setdefault(self):
01121         self.check_setdefault(weakref.WeakKeyDictionary,
01122                               C(), "value 1", "value 2")

Here is the call graph for this function:

Definition at line 1146 of file test_weakref.py.

01146 
01147     def test_weak_keyed_dict_update(self):
01148         self.check_update(weakref.WeakKeyDictionary,
01149                           {C(): 1, C(): 2, C(): 3})

Here is the call graph for this function:

Definition at line 877 of file test_weakref.py.

00877 
00878     def test_weak_keyed_iters(self):
00879         dict, objects = self.make_weak_keyed_dict()
00880         self.check_iters(dict)
00881 
00882         # Test keyrefs()
00883         refs = dict.keyrefs()
00884         self.assertEqual(len(refs), len(objects))
00885         objects2 = list(objects)
00886         for wr in refs:
00887             ob = wr()
00888             self.assertIn(ob, dict)
00889             self.assertIn(ob, dict)
00890             self.assertEqual(ob.arg, dict[ob])
00891             objects2.remove(ob)
00892         self.assertEqual(len(objects2), 0)
00893 
00894         # Test iterkeyrefs()
00895         objects2 = list(objects)
00896         self.assertEqual(len(list(dict.keyrefs())), len(objects))
00897         for wr in dict.keyrefs():
00898             ob = wr()
00899             self.assertIn(ob, dict)
00900             self.assertIn(ob, dict)
00901             self.assertEqual(ob.arg, dict[ob])
00902             objects2.remove(ob)
00903         self.assertEqual(len(objects2), 0)

Here is the call graph for this function:

Definition at line 849 of file test_weakref.py.

00849 
00850     def test_weak_keys(self):
00851         #
00852         #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
00853         #  len(d), k in d.
00854         #
00855         dict, objects = self.make_weak_keyed_dict()
00856         for o in objects:
00857             self.assertTrue(weakref.getweakrefcount(o) == 1,
00858                          "wrong number of weak references to %r!" % o)
00859             self.assertTrue(o.arg is dict[o],
00860                          "wrong object returned by weak dict!")
00861         items1 = dict.items()
00862         items2 = dict.copy().items()
00863         self.assertEqual(set(items1), set(items2),
00864                      "cloning of weak-keyed dictionary did not work!")
00865         del items1, items2
00866         self.assertEqual(len(dict), self.COUNT)
00867         del objects[0]
00868         self.assertTrue(len(dict) == (self.COUNT - 1),
00869                      "deleting object did not cause dictionary update")
00870         del objects, o
00871         self.assertTrue(len(dict) == 0,
00872                      "deleting the keys did not clear the dictionary")
00873         o = Object(42)
00874         dict[o] = "What is the meaning of the universe?"
00875         self.assertIn(o, dict)
00876         self.assertNotIn(34, dict)

Here is the call graph for this function:

Definition at line 993 of file test_weakref.py.

00993 
00994     def test_weak_keys_destroy_while_iterating(self):
00995         # Issue #7105: iterators shouldn't crash when a key is implicitly removed
00996         dict, objects = self.make_weak_keyed_dict()
00997         self.check_weak_destroy_while_iterating(dict, objects, 'keys')
00998         self.check_weak_destroy_while_iterating(dict, objects, 'items')
00999         self.check_weak_destroy_while_iterating(dict, objects, 'values')
01000         self.check_weak_destroy_while_iterating(dict, objects, 'keyrefs')
01001         dict, objects = self.make_weak_keyed_dict()
01002         @contextlib.contextmanager
01003         def testcontext():
01004             try:
01005                 it = iter(dict.items())
01006                 next(it)
01007                 # Schedule a key/value for removal and recreate it
01008                 v = objects.pop().arg
01009                 gc.collect()      # just in case
01010                 yield Object(v), v
01011             finally:
01012                 it = None           # should commit all removals
01013         self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext)

Here is the call graph for this function:

Definition at line 1161 of file test_weakref.py.

01161 
01162     def test_weak_valued_delitem(self):
01163         d = weakref.WeakValueDictionary()
01164         o1 = Object('1')
01165         o2 = Object('2')
01166         d['something'] = o1
01167         d['something else'] = o2
01168         self.assertEqual(len(d), 2)
01169         del d['something']
01170         self.assertEqual(len(d), 1)
01171         self.assertTrue(list(d.items()) == [('something else', o2)])

Here is the call graph for this function:

Definition at line 1090 of file test_weakref.py.

01090 
01091     def test_weak_valued_dict_popitem(self):
01092         self.check_popitem(weakref.WeakValueDictionary,
01093                            "key1", C(), "key2", C())

Here is the call graph for this function:

Definition at line 1115 of file test_weakref.py.

Here is the call graph for this function:

Definition at line 1142 of file test_weakref.py.

01142 
01143     def test_weak_valued_dict_update(self):
01144         self.check_update(weakref.WeakValueDictionary,
01145                           {1: C(), 'a': C(), C(): C()})

Here is the call graph for this function:

Definition at line 904 of file test_weakref.py.

00904 
00905     def test_weak_valued_iters(self):
00906         dict, objects = self.make_weak_valued_dict()
00907         self.check_iters(dict)
00908 
00909         # Test valuerefs()
00910         refs = dict.valuerefs()
00911         self.assertEqual(len(refs), len(objects))
00912         objects2 = list(objects)
00913         for wr in refs:
00914             ob = wr()
00915             self.assertEqual(ob, dict[ob.arg])
00916             self.assertEqual(ob.arg, dict[ob.arg].arg)
00917             objects2.remove(ob)
00918         self.assertEqual(len(objects2), 0)
00919 
00920         # Test itervaluerefs()
00921         objects2 = list(objects)
00922         self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
00923         for wr in dict.itervaluerefs():
00924             ob = wr()
00925             self.assertEqual(ob, dict[ob.arg])
00926             self.assertEqual(ob.arg, dict[ob.arg].arg)
00927             objects2.remove(ob)
00928         self.assertEqual(len(objects2), 0)

Here is the call graph for this function:

Definition at line 820 of file test_weakref.py.

00820 
00821     def test_weak_values(self):
00822         #
00823         #  This exercises d.copy(), d.items(), d[], del d[], len(d).
00824         #
00825         dict, objects = self.make_weak_valued_dict()
00826         for o in objects:
00827             self.assertEqual(weakref.getweakrefcount(o), 1)
00828             self.assertTrue(o is dict[o.arg],
00829                          "wrong object returned by weak dict!")
00830         items1 = list(dict.items())
00831         items2 = list(dict.copy().items())
00832         items1.sort()
00833         items2.sort()
00834         self.assertEqual(items1, items2,
00835                      "cloning of weak-valued dictionary did not work!")
00836         del items1, items2
00837         self.assertEqual(len(dict), self.COUNT)
00838         del objects[0]
00839         self.assertEqual(len(dict), self.COUNT - 1,
00840                      "deleting object did not cause dictionary update")
00841         del objects, o
00842         self.assertEqual(len(dict), 0,
00843                      "deleting the values did not clear the dictionary")
00844         # regression on SF bug #447152:
00845         dict = weakref.WeakValueDictionary()
00846         self.assertRaises(KeyError, dict.__getitem__, 1)
00847         dict[2] = C()
00848         self.assertRaises(KeyError, dict.__getitem__, 2)

Here is the call graph for this function:

Definition at line 1014 of file test_weakref.py.

01014 
01015     def test_weak_values_destroy_while_iterating(self):
01016         # Issue #7105: iterators shouldn't crash when a key is implicitly removed
01017         dict, objects = self.make_weak_valued_dict()
01018         self.check_weak_destroy_while_iterating(dict, objects, 'keys')
01019         self.check_weak_destroy_while_iterating(dict, objects, 'items')
01020         self.check_weak_destroy_while_iterating(dict, objects, 'values')
01021         self.check_weak_destroy_while_iterating(dict, objects, 'itervaluerefs')
01022         self.check_weak_destroy_while_iterating(dict, objects, 'valuerefs')
01023         dict, objects = self.make_weak_valued_dict()
01024         @contextlib.contextmanager
01025         def testcontext():
01026             try:
01027                 it = iter(dict.items())
01028                 next(it)
01029                 # Schedule a key/value for removal and recreate it
01030                 k = objects.pop().arg
01031                 gc.collect()      # just in case
01032                 yield k, Object(k)
01033             finally:
01034                 it = None           # should commit all removals
01035         self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext)

Here is the call graph for this function:


Member Data Documentation

Reimplemented in test.test_weakref.ReferencesTestCase.

Definition at line 38 of file test_weakref.py.

Definition at line 818 of file test_weakref.py.

Definition at line 1196 of file test_weakref.py.


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