Skip to content

Commit f522bbc

Browse files
Issue #22958: Constructor and update method of weakref.WeakValueDictionary
now accept the self keyword argument.
1 parent cab4566 commit f522bbc

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Lib/test/test_weakref.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,18 @@ def make_weak_keyed_dict(self):
11971197
dict[o] = o.arg
11981198
return dict, objects
11991199

1200+
def test_make_weak_valued_dict_misc(self):
1201+
# errors
1202+
self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__)
1203+
self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {})
1204+
self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ())
1205+
# special keyword arguments
1206+
o = Object(3)
1207+
for kw in 'self', 'other', 'iterable':
1208+
d = weakref.WeakValueDictionary(**{kw: o})
1209+
self.assertEqual(list(d.keys()), [kw])
1210+
self.assertEqual(d[kw], o)
1211+
12001212
def make_weak_valued_dict(self):
12011213
dict = weakref.WeakValueDictionary()
12021214
objects = map(Object, range(self.COUNT))
@@ -1279,6 +1291,19 @@ def check_update(self, klass, dict):
12791291
def test_weak_valued_dict_update(self):
12801292
self.check_update(weakref.WeakValueDictionary,
12811293
{1: C(), 'a': C(), C(): C()})
1294+
# errors
1295+
self.assertRaises(TypeError, weakref.WeakValueDictionary.update)
1296+
d = weakref.WeakValueDictionary()
1297+
self.assertRaises(TypeError, d.update, {}, {})
1298+
self.assertRaises(TypeError, d.update, (), ())
1299+
self.assertEqual(list(d.keys()), [])
1300+
# special keyword arguments
1301+
o = Object(3)
1302+
for kw in 'self', 'dict', 'other', 'iterable':
1303+
d = weakref.WeakValueDictionary()
1304+
d.update(**{kw: o})
1305+
self.assertEqual(list(d.keys()), [kw])
1306+
self.assertEqual(d[kw], o)
12821307

12831308
def test_weak_keyed_dict_update(self):
12841309
self.check_update(weakref.WeakKeyDictionary,

Lib/weakref.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ class WeakValueDictionary(UserDict.UserDict):
4444
# objects are unwrapped on the way out, and we always wrap on the
4545
# way in).
4646

47-
def __init__(self, *args, **kw):
47+
def __init__(*args, **kw):
48+
if not args:
49+
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
50+
"object needs an argument")
51+
self = args[0]
52+
args = args[1:]
53+
if len(args) > 1:
54+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
4855
def remove(wr, selfref=ref(self)):
4956
self = selfref()
5057
if self is not None:
@@ -214,7 +221,15 @@ def setdefault(self, key, default=None):
214221
else:
215222
return wr()
216223

217-
def update(self, dict=None, **kwargs):
224+
def update(*args, **kwargs):
225+
if not args:
226+
raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
227+
"object needs an argument")
228+
self = args[0]
229+
args = args[1:]
230+
if len(args) > 1:
231+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
232+
dict = args[0] if args else None
218233
if self._pending_removals:
219234
self._commit_removals()
220235
d = self.data

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #22958: Constructor and update method of weakref.WeakValueDictionary
41+
now accept the self keyword argument.
42+
4043
- Issue #22609: Constructor and the update method of collections.UserDict now
4144
accept the self keyword argument.
4245

0 commit comments

Comments
 (0)