mirror of
https://github.com/python/cpython.git
synced 2026-02-21 22:50:55 +00:00
gh-124748: Fix handling kwargs in WeakKeyDictionary.update() (#124783)
This commit is contained in:
parent
112d8ac972
commit
1636630390
3 changed files with 11 additions and 2 deletions
|
|
@ -1815,6 +1815,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()
|
||||
|
|
|
|||
|
|
@ -408,14 +408,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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Improve :exc:`TypeError` error message when :meth:`!weakref.WeakKeyDictionary.update`
|
||||
is used with keyword-only parameters.
|
||||
Loading…
Add table
Add a link
Reference in a new issue