summaryrefslogtreecommitdiffstats
path: root/ipapython
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2013-02-26 11:49:01 +0100
committerMartin Kosek <mkosek@redhat.com>2013-10-31 18:09:51 +0100
commit5d1d5138493b391c319e8d410b19ae7a8ab712f7 (patch)
tree75e877b72ec09678f50aa1da1126516a2d32a54f /ipapython
parent5aadaa6030f4de1229a6f90f967ba904560b02c2 (diff)
downloadfreeipa-5d1d5138493b391c319e8d410b19ae7a8ab712f7.tar.gz
freeipa-5d1d5138493b391c319e8d410b19ae7a8ab712f7.tar.xz
freeipa-5d1d5138493b391c319e8d410b19ae7a8ab712f7.zip
Always use lists for values in LDAPEntry internally.
Outside of LDAPEntry, it is still possible to use non-lists. Once we enforce lists for attribute values, this will be removed. https://fedorahosted.org/freeipa/ticket/3521
Diffstat (limited to 'ipapython')
-rw-r--r--ipapython/ipaldap.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/ipapython/ipaldap.py b/ipapython/ipaldap.py
index 0b45c9ec3..ef4bc5dbc 100644
--- a/ipapython/ipaldap.py
+++ b/ipapython/ipaldap.py
@@ -619,7 +619,7 @@ class IPASimpleLDAPObject(object):
# r[0] == r.dn
# r[1] == r.data
class LDAPEntry(collections.MutableMapping):
- __slots__ = ('_conn', '_dn', '_names', '_data', '_orig')
+ __slots__ = ('_conn', '_dn', '_names', '_data', '_not_list', '_orig')
def __init__(self, _conn, _dn=None, _obj=None, **kwargs):
"""
@@ -656,12 +656,14 @@ class LDAPEntry(collections.MutableMapping):
self._dn = _dn
self._names = CIDict()
self._data = {}
+ self._not_list = set()
self._orig = self
if isinstance(_obj, LDAPEntry):
#pylint: disable=E1103
self._names = CIDict(_obj._names)
self._data = dict(_obj._data)
+ self._not_list = set(_obj._not_list)
self._orig = _obj._orig
_obj = {}
@@ -703,6 +705,7 @@ class LDAPEntry(collections.MutableMapping):
result._names = deepcopy(self._names)
result._data = deepcopy(self._data)
+ result._not_list = deepcopy(self._not_list)
if self._orig is not self:
result._orig = self._orig.clone()
@@ -739,6 +742,7 @@ class LDAPEntry(collections.MutableMapping):
self._names[altname] = name
del self._data[oldname]
+ self._not_list.discard(oldname)
else:
self._names[name] = name
@@ -750,6 +754,15 @@ class LDAPEntry(collections.MutableMapping):
altname = altname.decode('utf-8')
self._names[altname] = name
+ if not isinstance(value, list):
+ if value is None:
+ value = []
+ else:
+ value = [value]
+ self._not_list.add(name)
+ else:
+ self._not_list.discard(name)
+
self._data[name] = value
def _get_attr_name(self, name):
@@ -765,7 +778,18 @@ class LDAPEntry(collections.MutableMapping):
return self
name = self._get_attr_name(name)
- return self._data[name]
+
+ value = self._data[name]
+ assert isinstance(value, list)
+
+ if name in self._not_list:
+ assert len(value) <= 1
+ if value:
+ value = value[0]
+ else:
+ value = None
+
+ return value
def single_value(self, name, default=_missing):
"""Return a single attribute value
@@ -796,10 +820,12 @@ class LDAPEntry(collections.MutableMapping):
del self._names[altname]
del self._data[name]
+ self._not_list.discard(name)
def clear(self):
self._names.clear()
self._data.clear()
+ self._not_list.clear()
def __len__(self):
return len(self._data)