Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,11 @@ def test_weak_valued_union_operators(self):
def test_weak_keyed_dict_update(self):
self.check_update(weakref.WeakKeyDictionary,
{C(): 1, C(): 2, C(): 3})
d = weakref.WeakKeyDictionary()
msg = ("Keyword arguments are not supported: "
"cannot create weak reference to 'str' object")
with self.assertRaisesRegex(TypeError, msg):
d.update(k='v')

def test_weak_keyed_delitem(self):
d = weakref.WeakKeyDictionary()
Expand Down
6 changes: 4 additions & 2 deletions Lib/weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,16 @@ def setdefault(self, key, default=None):
return self.data.setdefault(ref(key, self._remove),default)

def update(self, dict=None, /, **kwargs):
if kwargs:
msg = ("Keyword arguments are not supported: "
"cannot create weak reference to 'str' object")
raise TypeError(msg)
d = self.data
if dict is not None:
if not hasattr(dict, "items"):
dict = type({})(dict)
for key, value in dict.items():
d[ref(key, self._remove)] = value
if len(kwargs):
self.update(kwargs)

def __ior__(self, other):
self.update(other)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Impove :exc:`TypeError` error message
when :meth:`!weakref.WeakKeyDictionary.update`

is used with keyword-only parameters.
Loading