Use six.integer_types instead of (long, int)

Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
This commit is contained in:
Petr Viktorin
2015-09-01 11:42:01 +02:00
committed by Jan Cholasta
parent c27cb295a5
commit fbacc26a6a
7 changed files with 21 additions and 18 deletions
+8 -8
View File
@@ -1031,7 +1031,7 @@ class Number(Param):
""" """
if type(value) in self.allowed_types: if type(value) in self.allowed_types:
return value return value
if type(value) in (unicode, int, long, float): if type(value) in (unicode, float) + six.integer_types:
try: try:
return self.type(value) return self.type(value)
except ValueError: except ValueError:
@@ -1050,12 +1050,12 @@ class Int(Number):
""" """
type = int type = int
allowed_types = int, long allowed_types = six.integer_types
type_error = _('must be an integer') type_error = _('must be an integer')
kwargs = Param.kwargs + ( kwargs = Param.kwargs + (
('minvalue', (int, long), int(MININT)), ('minvalue', six.integer_types, int(MININT)),
('maxvalue', (int, long), int(MAXINT)), ('maxvalue', six.integer_types, int(MAXINT)),
) )
@staticmethod @staticmethod
@@ -1097,7 +1097,7 @@ class Int(Number):
""" """
Check min constraint. Check min constraint.
""" """
assert type(value) in (int, long) assert type(value) in six.integer_types
if value < self.minvalue: if value < self.minvalue:
return _('must be at least %(minvalue)d') % dict( return _('must be at least %(minvalue)d') % dict(
minvalue=self.minvalue, minvalue=self.minvalue,
@@ -1107,7 +1107,7 @@ class Int(Number):
""" """
Check max constraint. Check max constraint.
""" """
assert type(value) in (int, long) assert type(value) in six.integer_types
if value > self.maxvalue: if value > self.maxvalue:
return _('can be at most %(maxvalue)d') % dict( return _('can be at most %(maxvalue)d') % dict(
maxvalue=self.maxvalue, maxvalue=self.maxvalue,
@@ -1413,7 +1413,7 @@ class Str(Data):
""" """
if type(value) in self.allowed_types: if type(value) in self.allowed_types:
return value return value
if type(value) in (int, long, float, decimal.Decimal): if type(value) in (float, decimal.Decimal) + six.integer_types:
return self.type(value) return self.type(value)
if type(value) in (tuple, list): if type(value) in (tuple, list):
raise ConversionError(name=self.name, index=index, raise ConversionError(name=self.name, index=index,
@@ -1567,7 +1567,7 @@ class IntEnum(Enum):
""" """
type = int type = int
allowed_types = int, long allowed_types = six.integer_types
type_error = Int.type_error type_error = Int.type_error
def _convert_scalar(self, value, index=None): def _convert_scalar(self, value, index=None):
+2 -2
View File
@@ -166,7 +166,7 @@ def xml_wrap(value, version):
if type(value) is Decimal: if type(value) is Decimal:
# transfer Decimal as a string # transfer Decimal as a string
return unicode(value) return unicode(value)
if isinstance(value, (int, long)) and (value < MININT or value > MAXINT): if isinstance(value, six.integer_types) and (value < MININT or value > MAXINT):
return unicode(value) return unicode(value)
if isinstance(value, DN): if isinstance(value, DN):
return str(value) return str(value)
@@ -184,7 +184,7 @@ def xml_wrap(value, version):
else: else:
return unicode(value) return unicode(value)
assert type(value) in (unicode, int, long, float, bool, NoneType) assert type(value) in (unicode, float, bool, NoneType) + six.integer_types
return value return value
+1 -1
View File
@@ -52,7 +52,7 @@ def json_serialize(obj):
return [json_serialize(o) for o in obj] return [json_serialize(o) for o in obj]
if isinstance(obj, dict): if isinstance(obj, dict):
return {k: json_serialize(v) for (k, v) in obj.items()} return {k: json_serialize(v) for (k, v) in obj.items()}
if isinstance(obj, (bool, float, int, long, unicode, NoneType)): if isinstance(obj, (bool, float, unicode, NoneType, six.integer_types)):
return obj return obj
if isinstance(obj, str): if isinstance(obj, str):
return obj.decode('utf-8') return obj.decode('utf-8')
+2 -2
View File
@@ -391,7 +391,7 @@ class Cookie(object):
self._timestamp = None self._timestamp = None
elif isinstance(value, datetime.datetime): elif isinstance(value, datetime.datetime):
self._timestamp = value self._timestamp = value
elif isinstance(value, (int, long, float)): elif isinstance(value, (six.integer_types, float)):
self._timestamp = datetime.datetime.utcfromtimestamp(value) self._timestamp = datetime.datetime.utcfromtimestamp(value)
elif isinstance(value, six.string_types): elif isinstance(value, six.string_types):
self._timestamp = Cookie.parse_datetime(value) self._timestamp = Cookie.parse_datetime(value)
@@ -417,7 +417,7 @@ class Cookie(object):
self._expires = None self._expires = None
elif isinstance(value, datetime.datetime): elif isinstance(value, datetime.datetime):
self._expires = value self._expires = value
elif isinstance(value, (int, long, float)): elif isinstance(value, (six.integer_types, float)):
self._expires = datetime.datetime.utcfromtimestamp(value) self._expires = datetime.datetime.utcfromtimestamp(value)
elif isinstance(value, six.string_types): elif isinstance(value, six.string_types):
self._expires = Cookie.parse_datetime(value) self._expires = Cookie.parse_datetime(value)
+2 -2
View File
@@ -833,7 +833,7 @@ class RDN(object):
return len(self._avas) return len(self._avas)
def __getitem__(self, key): def __getitem__(self, key):
if isinstance(key, (int, long)): if isinstance(key, six.integer_types):
return self._get_ava(self._avas[key]) return self._get_ava(self._avas[key])
if isinstance(key, slice): if isinstance(key, slice):
return [self._get_ava(ava) for ava in self._avas[key]] return [self._get_ava(ava) for ava in self._avas[key]]
@@ -1139,7 +1139,7 @@ class DN(object):
return len(self.rdns) return len(self.rdns)
def __getitem__(self, key): def __getitem__(self, key):
if isinstance(key, (int, long)): if isinstance(key, six.integer_types):
return self._get_rdn(self.rdns[key]) return self._get_rdn(self.rdns[key])
if isinstance(key, slice): if isinstance(key, slice):
cls = self.__class__ cls = self.__class__
+1 -1
View File
@@ -831,7 +831,7 @@ class LDAPClient(object):
return 'TRUE' return 'TRUE'
else: else:
return 'FALSE' return 'FALSE'
elif isinstance(val, (unicode, float, int, long, Decimal, DN)): elif isinstance(val, (unicode, six.integer_types, long, Decimal, DN)):
return value_to_utf8(val) return value_to_utf8(val)
elif isinstance(val, DNSName): elif isinstance(val, DNSName):
return str(val) return str(val)
+5 -2
View File
@@ -31,6 +31,10 @@ import sys
from types import NoneType from types import NoneType
from decimal import Decimal from decimal import Decimal
from inspect import isclass from inspect import isclass
from xmlrpclib import MAXINT, MININT
import six
from ipatests.util import raises, ClassChecker, read_only 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
@@ -38,7 +42,6 @@ from ipalib import parameters, text, errors, config
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR 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
NULLS = (None, '', u'', tuple(), []) NULLS = (None, '', u'', tuple(), [])
@@ -1246,7 +1249,7 @@ class test_Int(ClassChecker):
# Test with no kwargs: # Test with no kwargs:
o = self.cls('my_number') o = self.cls('my_number')
assert o.type == int assert o.type == int
assert o.allowed_types == (int, long) assert o.allowed_types == six.integer_types
assert isinstance(o, parameters.Int) assert isinstance(o, parameters.Int)
assert o.minvalue == int(MININT) assert o.minvalue == int(MININT)
assert o.maxvalue == int(MAXINT) assert o.maxvalue == int(MAXINT)