Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5102e3

Browse files
Issue #22958: Constructor and update method of weakref.WeakValueDictionary
now accept the self and the dict keyword arguments.
1 parent 68f5ef2 commit b5102e3

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

Lib/test/test_weakref.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,18 @@ def test_make_weak_valued_dict_from_weak_valued_dict(self):
14081408
dict2 = weakref.WeakValueDictionary(dict)
14091409
self.assertEqual(dict[364], o)
14101410

1411+
def test_make_weak_valued_dict_misc(self):
1412+
# errors
1413+
self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__)
1414+
self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {})
1415+
self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ())
1416+
# special keyword arguments
1417+
o = Object(3)
1418+
for kw in 'self', 'dict', 'other', 'iterable':
1419+
d = weakref.WeakValueDictionary(**{kw: o})
1420+
self.assertEqual(list(d.keys()), [kw])
1421+
self.assertEqual(d[kw], o)
1422+
14111423
def make_weak_valued_dict(self):
14121424
dict = weakref.WeakValueDictionary()
14131425
objects = list(map(Object, range(self.COUNT)))
@@ -1488,6 +1500,19 @@ def check_update(self, klass, dict):
14881500
def test_weak_valued_dict_update(self):
14891501
self.check_update(weakref.WeakValueDictionary,
14901502
{1: C(), 'a': C(), C(): C()})
1503+
# errors
1504+
self.assertRaises(TypeError, weakref.WeakValueDictionary.update)
1505+
d = weakref.WeakValueDictionary()
1506+
self.assertRaises(TypeError, d.update, {}, {})
1507+
self.assertRaises(TypeError, d.update, (), ())
1508+
self.assertEqual(list(d.keys()), [])
1509+
# special keyword arguments
1510+
o = Object(3)
1511+
for kw in 'self', 'dict', 'other', 'iterable':
1512+
d = weakref.WeakValueDictionary()
1513+
d.update(**{kw: o})
1514+
self.assertEqual(list(d.keys()), [kw])
1515+
self.assertEqual(d[kw], o)
14911516

14921517
def test_weak_keyed_dict_update(self):
14931518
self.check_update(weakref.WeakKeyDictionary,

Lib/weakref.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ class WeakValueDictionary(collections.MutableMapping):
9898
# objects are unwrapped on the way out, and we always wrap on the
9999
# way in).
100100

101-
def __init__(self, *args, **kw):
101+
def __init__(*args, **kw):
102+
if not args:
103+
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
104+
"object needs an argument")
105+
self, *args = args
106+
if len(args) > 1:
107+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
102108
def remove(wr, selfref=ref(self)):
103109
self = selfref()
104110
if self is not None:
@@ -252,7 +258,14 @@ def setdefault(self, key, default=None):
252258
else:
253259
return wr()
254260

255-
def update(self, dict=None, **kwargs):
261+
def update(*args, **kwargs):
262+
if not args:
263+
raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
264+
"object needs an argument")
265+
self, *args = args
266+
if len(args) > 1:
267+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
268+
dict = args[0] if args else None
256269
if self._pending_removals:
257270
self._commit_removals()
258271
d = self.data

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Core and Builtins
7878
Library
7979
-------
8080

81+
- Issue #22958: Constructor and update method of weakref.WeakValueDictionary
82+
now accept the self and the dict keyword arguments.
83+
8184
- Issue #22609: Constructor of collections.UserDict now accepts the self keyword
8285
argument.
8386

0 commit comments

Comments
 (0)