Skip to content

Commit 5186d90

Browse files
committed
gh-124748: Fix handling kwargs in WeakKeyDictionary.update()
1 parent 6f4d64b commit 5186d90

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Lib/test/test_weakref.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,11 @@ def test_weak_valued_union_operators(self):
17921792
def test_weak_keyed_dict_update(self):
17931793
self.check_update(weakref.WeakKeyDictionary,
17941794
{C(): 1, C(): 2, C(): 3})
1795+
d = weakref.WeakKeyDictionary()
1796+
msg = ("Keyword arguments are not supported. "
1797+
"Can not create weak references to `str` keys.")
1798+
with self.assertRaisesRegex(TypeError, msg):
1799+
d.update(k='v')
17951800

17961801
def test_weak_keyed_delitem(self):
17971802
d = weakref.WeakKeyDictionary()

Lib/weakref.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,17 @@ def setdefault(self, key, default=None):
508508
return self.data.setdefault(ref(key, self._remove),default)
509509

510510
def update(self, dict=None, /, **kwargs):
511+
if kwargs:
512+
raise TypeError(
513+
"Keyword arguments are not supported. "
514+
"Can not create weak references to `str` keys."
515+
)
511516
d = self.data
512517
if dict is not None:
513518
if not hasattr(dict, "items"):
514519
dict = type({})(dict)
515520
for key, value in dict.items():
516521
d[ref(key, self._remove)] = value
517-
if len(kwargs):
518-
self.update(kwargs)
519522

520523
def __ior__(self, other):
521524
self.update(other)

0 commit comments

Comments
 (0)