From 2a2cef38feb7f259ea6b5735a6ce3e46812e217d Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 14 Mar 2018 15:25:04 +0100 Subject: [PATCH] cidict: Rewrite in terms of MutableMapping The UserDict API is not specified when it comes to extending: it is not clear which methods can be safely overridden. The MutableMapping ABC fixes this problem by providing an explicit set of abstract methods, in terms of which the rest of the API is implemented. Switch to using MutableMapping for cidict. --- Lib/ldap/cidict.py | 71 +++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/Lib/ldap/cidict.py b/Lib/ldap/cidict.py index 875d5f5..db57468 100644 --- a/Lib/ldap/cidict.py +++ b/Lib/ldap/cidict.py @@ -5,57 +5,58 @@ See https://www.python-ldap.org/ for details. """ +from collections import MutableMapping import warnings from ldap import __version__ -from ldap.compat import IterableUserDict +class cidict(MutableMapping): + """ + Case-insensitive but case-respecting dictionary. + """ -class cidict(IterableUserDict): - """ - Case-insensitive but case-respecting dictionary. - """ + def __init__(self, default=None): + self._keys = {} + self._data = {} + if default: + self.update(default) + + # MutableMapping abstract methods - def __init__(self,default=None): - self._keys = {} - IterableUserDict.__init__(self,{}) - self.update(default or {}) + def __getitem__(self, key): + return self._data[key.lower()] - def __getitem__(self,key): - return self.data[key.lower()] + def __setitem__(self, key, value): + lower_key = key.lower() + self._keys[lower_key] = key + self._data[lower_key] = value - def __setitem__(self,key,value): - lower_key = key.lower() - self._keys[lower_key] = key - self.data[lower_key] = value + def __delitem__(self, key): + lower_key = key.lower() + del self._keys[lower_key] + del self._data[lower_key] - def __delitem__(self,key): - lower_key = key.lower() - del self._keys[lower_key] - del self.data[lower_key] + def __iter__(self): + return iter(self._keys.values()) - def update(self,dict): - for key, value in dict.items(): - self[key] = value + def __len__(self): + return len(self._keys) - def has_key(self,key): - return key in self + # Specializations for performance - def __contains__(self,key): - return IterableUserDict.__contains__(self, key.lower()) + def __contains__(self, key): + return key.lower() in self._keys - def __iter__(self): - return iter(self.keys()) + def clear(self): + self._keys.clear() + self._data.clear() - def keys(self): - return self._keys.values() + # Backwards compatibility - def items(self): - result = [] - for k in self._keys.values(): - result.append((k,self[k])) - return result + def has_key(self, key): + """Compatibility with python-ldap 2.x""" + return key in self def strlist_minus(a,b):