Avoid exceptions on copy

This commit is contained in:
Eugenio Lacuesta 2022-05-27 19:56:42 -03:00
parent 1b1d518e2b
commit 2c65066ad9
No known key found for this signature in database
GPG Key ID: DA3EF2D0913E9810
2 changed files with 12 additions and 3 deletions

View File

@ -94,8 +94,11 @@ class CaseInsensitiveDict(collections.UserDict):
def __setitem__(self, key: AnyStr, value: Any) -> None:
normalized_key = self._normkey(key)
if normalized_key.lower() in self._keys:
del self[self._keys[normalized_key.lower()]]
try:
lower_key = self._keys[normalized_key.lower()]
del self[lower_key]
except KeyError:
pass
super().__setitem__(normalized_key, self._normvalue(value))
self._keys[normalized_key.lower()] = normalized_key

View File

@ -187,9 +187,15 @@ class CaseInsensitiveDictMixin:
def test_copy(self):
h1 = self.dict_class({'header1': 'value'})
h2 = copy.copy(h1)
assert isinstance(h2, self.dict_class)
self.assertEqual(h1, h2)
self.assertEqual(h1.get('header1'), h2.get('header1'))
assert isinstance(h2, self.dict_class)
self.assertEqual(h1.get('header1'), h2.get('HEADER1'))
h3 = h1.copy()
assert isinstance(h3, self.dict_class)
self.assertEqual(h1, h3)
self.assertEqual(h1.get('header1'), h3.get('header1'))
self.assertEqual(h1.get('header1'), h3.get('HEADER1'))
class CaseInsensitiveDictTest(CaseInsensitiveDictMixin, unittest.TestCase):