summaryrefslogtreecommitdiffstats
path: root/ipalib/parameters.py
diff options
context:
space:
mode:
authorNathaniel McCallum <npmccallum@redhat.com>2014-02-21 11:38:32 -0500
committerPetr Viktorin <pviktori@redhat.com>2014-02-25 16:05:19 +0100
commit4499b25be90227e54fc4a9f54598cadc8d2f6394 (patch)
tree2b1a855b42534751426a9f5b5865587b9bc17e1a /ipalib/parameters.py
parentbe7b1b94e300b137c34bab80df3dc91195259c89 (diff)
downloadfreeipa-4499b25be90227e54fc4a9f54598cadc8d2f6394.tar.gz
freeipa-4499b25be90227e54fc4a9f54598cadc8d2f6394.tar.xz
freeipa-4499b25be90227e54fc4a9f54598cadc8d2f6394.zip
Remove NULLS from constants.py
In the parameters system, we have been checking for a positive list of values which get converted to None. The problem is that this method can in some cases throw warnings when type coercion doesn't work (particularly, string to unicode). Instead, any values that evaluate to False that are neither numeric nor boolean should be converted to None. Reviewed-By: Jan Pazdziora <jpazdziora@redhat.com>
Diffstat (limited to 'ipalib/parameters.py')
-rw-r--r--ipalib/parameters.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index b4fb3402d..0b354969d 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -109,11 +109,14 @@ from text import _ as ugettext
from plugable import ReadOnly, lock, check_name
from errors import ConversionError, RequirementError, ValidationError
from errors import PasswordMismatch, Base64DecodeError
-from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
+from constants import TYPE_ERROR, CALLABLE_ERROR
from text import Gettext, FixMe
from util import json_serialize
from ipapython.dn import DN
+def _is_null(value):
+ return not value and value != 0 # NOTE: False == 0
+
class DefaultFrom(ReadOnly):
"""
Derive a default value from other supplied values.
@@ -550,7 +553,7 @@ class Param(ReadOnly):
"""
One stop shopping.
"""
- if value in NULLS:
+ if _is_null(value):
value = self.get_default(**kw)
else:
value = self.convert(self.normalize(value))
@@ -740,16 +743,16 @@ class Param(ReadOnly):
(Note that `Str` is a subclass of `Param`.)
- All values in `constants.NULLS` will be converted to ``None``. For
- example:
+ All non-numeric, non-boolean values which evaluate to False will be
+ converted to None. For example:
>>> scalar.convert(u'') is None # An empty string
True
>>> scalar.convert([]) is None # An empty list
True
- Likewise, values in `constants.NULLS` will be filtered out of a
- multivalue parameter. For example:
+ Likewise, they will be filtered out of a multivalue parameter.
+ For example:
>>> multi = Str('my_multi', multivalue=True)
>>> multi.convert([1.5, '', 17, None, u'Hello'])
@@ -772,14 +775,14 @@ class Param(ReadOnly):
:param value: A proposed value for this parameter.
"""
- if value in NULLS:
+ if _is_null(value):
return
if self.multivalue:
if type(value) not in (tuple, list):
value = (value,)
values = tuple(
self._convert_scalar(v, i) for (i, v) in filter(
- lambda iv: iv[1] not in NULLS, enumerate(value)
+ lambda iv: not _is_null(iv[1]), enumerate(value)
)
)
if len(values) == 0: