]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #22958: Constructor and update method of weakref.WeakValueDictionary
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Sep 2015 20:51:27 +0000 (23:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Sep 2015 20:51:27 +0000 (23:51 +0300)
now accept the self keyword argument.

Lib/test/test_weakref.py
Lib/weakref.py
Misc/NEWS

index cc0a755edc416478e6e0e97f1dd7e4b64a904fb4..b7f985c939a770a9fb534e9fe3a94ba700c08e11 100644 (file)
@@ -1197,6 +1197,18 @@ class MappingTestCase(TestBase):
             dict[o] = o.arg
         return dict, objects
 
+    def test_make_weak_valued_dict_misc(self):
+        # errors
+        self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__)
+        self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {})
+        self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ())
+        # special keyword arguments
+        o = Object(3)
+        for kw in 'self', 'other', 'iterable':
+            d = weakref.WeakValueDictionary(**{kw: o})
+            self.assertEqual(list(d.keys()), [kw])
+            self.assertEqual(d[kw], o)
+
     def make_weak_valued_dict(self):
         dict = weakref.WeakValueDictionary()
         objects = map(Object, range(self.COUNT))
@@ -1279,6 +1291,19 @@ class MappingTestCase(TestBase):
     def test_weak_valued_dict_update(self):
         self.check_update(weakref.WeakValueDictionary,
                           {1: C(), 'a': C(), C(): C()})
+        # errors
+        self.assertRaises(TypeError, weakref.WeakValueDictionary.update)
+        d = weakref.WeakValueDictionary()
+        self.assertRaises(TypeError, d.update, {}, {})
+        self.assertRaises(TypeError, d.update, (), ())
+        self.assertEqual(list(d.keys()), [])
+        # special keyword arguments
+        o = Object(3)
+        for kw in 'self', 'dict', 'other', 'iterable':
+            d = weakref.WeakValueDictionary()
+            d.update(**{kw: o})
+            self.assertEqual(list(d.keys()), [kw])
+            self.assertEqual(d[kw], o)
 
     def test_weak_keyed_dict_update(self):
         self.check_update(weakref.WeakKeyDictionary,
index 787c8851dd3d3453e75511051358c4f16f74c49e..ca37f8752875991233ddafb9da5a262a23138a74 100644 (file)
@@ -44,7 +44,14 @@ class WeakValueDictionary(UserDict.UserDict):
     # objects are unwrapped on the way out, and we always wrap on the
     # way in).
 
-    def __init__(self, *args, **kw):
+    def __init__(*args, **kw):
+        if not args:
+            raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
+                            "object needs an argument")
+        self = args[0]
+        args = args[1:]
+        if len(args) > 1:
+            raise TypeError('expected at most 1 arguments, got %d' % len(args))
         def remove(wr, selfref=ref(self)):
             self = selfref()
             if self is not None:
@@ -214,7 +221,15 @@ class WeakValueDictionary(UserDict.UserDict):
         else:
             return wr()
 
-    def update(self, dict=None, **kwargs):
+    def update(*args, **kwargs):
+        if not args:
+            raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
+                            "object needs an argument")
+        self = args[0]
+        args = args[1:]
+        if len(args) > 1:
+            raise TypeError('expected at most 1 arguments, got %d' % len(args))
+        dict = args[0] if args else None
         if self._pending_removals:
             self._commit_removals()
         d = self.data
index 036c6c448c037e458bdc1ae8489421caec14318a..442cec4c7b9cf3e26ce5e18bb83616c2ca92b089 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22958: Constructor and update method of weakref.WeakValueDictionary
+  now accept the self keyword argument.
+
 - Issue #22609: Constructor and the update method of collections.UserDict now
   accept the self keyword argument.