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>
This commit is contained in:
Nathaniel McCallum 2014-02-21 11:38:32 -05:00 committed by Petr Viktorin
parent be7b1b94e3
commit 4499b25be9
3 changed files with 14 additions and 12 deletions

View File

@ -32,9 +32,6 @@ except:
except: except:
FQDN = None FQDN = None
# The parameter system treats all these values as None:
NULLS = (None, '', u'', tuple(), [])
# regular expression NameSpace member names must match: # regular expression NameSpace member names must match:
NAME_REGEX = r'^[a-z][_a-z0-9]*[a-z0-9]$|^[a-z]$' NAME_REGEX = r'^[a-z][_a-z0-9]*[a-z0-9]$|^[a-z]$'

View File

@ -109,11 +109,14 @@ from text import _ as ugettext
from plugable import ReadOnly, lock, check_name from plugable import ReadOnly, lock, check_name
from errors import ConversionError, RequirementError, ValidationError from errors import ConversionError, RequirementError, ValidationError
from errors import PasswordMismatch, Base64DecodeError 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 text import Gettext, FixMe
from util import json_serialize from util import json_serialize
from ipapython.dn import DN from ipapython.dn import DN
def _is_null(value):
return not value and value != 0 # NOTE: False == 0
class DefaultFrom(ReadOnly): class DefaultFrom(ReadOnly):
""" """
Derive a default value from other supplied values. Derive a default value from other supplied values.
@ -550,7 +553,7 @@ class Param(ReadOnly):
""" """
One stop shopping. One stop shopping.
""" """
if value in NULLS: if _is_null(value):
value = self.get_default(**kw) value = self.get_default(**kw)
else: else:
value = self.convert(self.normalize(value)) value = self.convert(self.normalize(value))
@ -740,16 +743,16 @@ class Param(ReadOnly):
(Note that `Str` is a subclass of `Param`.) (Note that `Str` is a subclass of `Param`.)
All values in `constants.NULLS` will be converted to ``None``. For All non-numeric, non-boolean values which evaluate to False will be
example: converted to None. For example:
>>> scalar.convert(u'') is None # An empty string >>> scalar.convert(u'') is None # An empty string
True True
>>> scalar.convert([]) is None # An empty list >>> scalar.convert([]) is None # An empty list
True True
Likewise, values in `constants.NULLS` will be filtered out of a Likewise, they will be filtered out of a multivalue parameter.
multivalue parameter. For example: For example:
>>> multi = Str('my_multi', multivalue=True) >>> multi = Str('my_multi', multivalue=True)
>>> multi.convert([1.5, '', 17, None, u'Hello']) >>> multi.convert([1.5, '', 17, None, u'Hello'])
@ -772,14 +775,14 @@ class Param(ReadOnly):
:param value: A proposed value for this parameter. :param value: A proposed value for this parameter.
""" """
if value in NULLS: if _is_null(value):
return return
if self.multivalue: if self.multivalue:
if type(value) not in (tuple, list): if type(value) not in (tuple, list):
value = (value,) value = (value,)
values = tuple( values = tuple(
self._convert_scalar(v, i) for (i, v) in filter( 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: if len(values) == 0:

View File

@ -31,11 +31,13 @@ from ipatests.util import raises, ClassChecker, read_only
from ipatests.util import dummy_ugettext, assert_equal from ipatests.util import dummy_ugettext, assert_equal
from ipatests.data import binary_bytes, utf8_bytes, unicode_str from ipatests.data import binary_bytes, utf8_bytes, unicode_str
from ipalib import parameters, text, errors, config from ipalib import parameters, text, errors, config
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR, NULLS from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR
from ipalib.errors import ValidationError, ConversionError from ipalib.errors import ValidationError, ConversionError
from ipalib import _ from ipalib import _
from xmlrpclib import MAXINT, MININT from xmlrpclib import MAXINT, MININT
NULLS = (None, '', u'', tuple(), [])
class test_DefaultFrom(ClassChecker): class test_DefaultFrom(ClassChecker):
""" """
Test the `ipalib.parameters.DefaultFrom` class. Test the `ipalib.parameters.DefaultFrom` class.