Python 2 has both long and int types. Python 3 only has int, which
behaves like long.
In the case of needing a set of integer types, we can use
six.integer_types which includes int and long for Python 2 and just int
for Python 3.
We can convert all cases of long(value) to int(value), because as of
Python 2.4, when the result of an operation would be too big for an int,
the type is automatically converted to a long.
There were several places in this patch doing type comparisons. The
preferred way to do this is using the isinstance() or issubclass()
built-in functions, so I converted the similar checks nearby while I was
at it.
Signed-off-by: Russell Bryant <russell@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
def from_json(base, json, symtab=None):
type_ = base.type
json = ovs.db.parser.float_to_int(json)
def from_json(base, json, symtab=None):
type_ = base.type
json = ovs.db.parser.float_to_int(json)
- if ((type_ == ovs.db.types.IntegerType and type(json) in [int, long])
+ real_types = list(six.integer_types)
+ real_types.extend([float])
+ real_types = tuple(real_types)
+ if ((type_ == ovs.db.types.IntegerType
+ and isinstance(json, six.integer_types))
or (type_ == ovs.db.types.RealType
or (type_ == ovs.db.types.RealType
- and type(json) in [int, long, float])
- or (type_ == ovs.db.types.BooleanType and type(json) == bool)
+ and isinstance(json, real_types))
+ or (type_ == ovs.db.types.BooleanType and isinstance(json, bool))
or (type_ == ovs.db.types.StringType
or (type_ == ovs.db.types.StringType
- and type(json) in [str, unicode])):
+ and isinstance(json, (str, unicode)))):
atom = Atom(type_, json)
elif type_ == ovs.db.types.UuidType:
atom = Atom(type_, ovs.ovsuuid.from_json(json, symtab))
atom = Atom(type_, json)
elif type_ == ovs.db.types.UuidType:
atom = Atom(type_, ovs.ovsuuid.from_json(json, symtab))
@staticmethod
def new(x):
@staticmethod
def new(x):
- if type(x) in [int, long]:
+ if isinstance(x, six.integer_types):
t = ovs.db.types.IntegerType
t = ovs.db.types.IntegerType
+ elif isinstance(x, float):
t = ovs.db.types.RealType
t = ovs.db.types.RealType
- elif x in [False, True]:
+ elif isinstance(x, bool):
t = ovs.db.types.BooleanType
t = ovs.db.types.BooleanType
- elif type(x) in [str, unicode]:
+ elif isinstance(x, (str, unicode)):
t = ovs.db.types.StringType
elif isinstance(x, uuid):
t = ovs.db.types.UuidType
t = ovs.db.types.StringType
elif isinstance(x, uuid):
t = ovs.db.types.UuidType
# __process_reply() already checked.
mutate = ops[self._inc_index]
count = mutate.get("count")
# __process_reply() already checked.
mutate = ops[self._inc_index]
count = mutate.get("count")
- if not Transaction.__check_json_type(count, (int, long),
+ if not Transaction.__check_json_type(count, six.integer_types,
'"mutate" reply "count"'):
return False
if count != 1:
'"mutate" reply "count"'):
return False
if count != 1:
'"select" reply row'):
return False
column = row.get(self._inc_column)
'"select" reply row'):
return False
column = row.get(self._inc_column)
- if not Transaction.__check_json_type(column, (int, long),
+ if not Transaction.__check_json_type(column, six.integer_types,
'"select" reply inc column'):
return False
self._inc_new_value = column
'"select" reply inc column'):
return False
self._inc_new_value = column
def json_type_to_string(type_):
def json_type_to_string(type_):
+ number_types = list(six.integer_types)
+ number_types.extend([float])
+ number_types = tuple(number_types)
if type_ is None:
return "null"
if type_ is None:
return "null"
+ elif issubclass(type_, bool):
+ elif issubclass(type_, dict):
+ elif issubclass(type_, list):
- elif type_ in [int, long, float]:
+ elif issubclass(type_, number_types):
- elif type_ in [str, unicode]:
+ elif issubclass(type_, (str, unicode)):
return "string"
else:
return "<invalid>"
return "string"
else:
return "<invalid>"
from ovs.db import error
import ovs.db.parser
import ovs.db.data
from ovs.db import error
import ovs.db.parser
import ovs.db.data
def default_atom(self):
return ovs.db.data.Atom(self, self.default)
def default_atom(self):
return ovs.db.data.Atom(self, self.default)
+REAL_PYTHON_TYPES = list(six.integer_types)
+REAL_PYTHON_TYPES.extend([float])
+REAL_PYTHON_TYPES = tuple(REAL_PYTHON_TYPES)
+
VoidType = AtomicType("void", None, ())
VoidType = AtomicType("void", None, ())
-IntegerType = AtomicType("integer", 0, (int, long))
-RealType = AtomicType("real", 0.0, (int, long, float))
+IntegerType = AtomicType("integer", 0, six.integer_types)
+RealType = AtomicType("real", 0.0, REAL_PYTHON_TYPES)
BooleanType = AtomicType("boolean", False, (bool,))
StringType = AtomicType("string", "", (str, unicode))
UuidType = AtomicType("uuid", ovs.ovsuuid.zero(), (uuid.UUID,))
BooleanType = AtomicType("boolean", False, (bool,))
StringType = AtomicType("string", "", (str, unicode))
UuidType = AtomicType("uuid", ovs.ovsuuid.zero(), (uuid.UUID,))
@staticmethod
def __parse_uint(parser, name, default):
@staticmethod
def __parse_uint(parser, name, default):
- value = parser.get_optional(name, [int, long])
+ value = parser.get_optional(name, six.integer_types)
if value is None:
value = default
else:
if value is None:
value = default
else:
base.enum = ovs.db.data.Datum.from_json(
BaseType.get_enum_type(base.type), enum)
elif base.type == IntegerType:
base.enum = ovs.db.data.Datum.from_json(
BaseType.get_enum_type(base.type), enum)
elif base.type == IntegerType:
- base.min = parser.get_optional("minInteger", [int, long])
- base.max = parser.get_optional("maxInteger", [int, long])
+ base.min = parser.get_optional("minInteger", six.integer_types)
+ base.max = parser.get_optional("maxInteger", six.integer_types)
if (base.min is not None and base.max is not None
and base.min > base.max):
raise error.Error("minInteger exceeds maxInteger", json)
elif base.type == RealType:
if (base.min is not None and base.max is not None
and base.min > base.max):
raise error.Error("minInteger exceeds maxInteger", json)
elif base.type == RealType:
- base.min = parser.get_optional("minReal", [int, long, float])
- base.max = parser.get_optional("maxReal", [int, long, float])
+ base.min = parser.get_optional("minReal", REAL_PYTHON_TYPES)
+ base.max = parser.get_optional("maxReal", REAL_PYTHON_TYPES)
if (base.min is not None and base.max is not None
and base.min > base.max):
raise error.Error("minReal exceeds maxReal", json)
if (base.min is not None and base.max is not None
and base.min > base.max):
raise error.Error("minReal exceeds maxReal", json)
self.stream.write(u"false")
elif obj is True:
self.stream.write(u"true")
self.stream.write(u"false")
elif obj is True:
self.stream.write(u"true")
- elif type(obj) in (int, long):
+ elif isinstance(obj, six.integer_types):
self.stream.write(u"%d" % obj)
self.stream.write(u"%d" % obj)
- elif type(obj) == float:
+ elif isinstance(obj, float):
self.stream.write("%.15g" % obj)
self.stream.write("%.15g" % obj)
- elif type(obj) == unicode:
+ elif isinstance(obj, unicode):
self.__serialize_string(obj)
self.__serialize_string(obj)
+ elif isinstance(obj, str):
self.__serialize_string(unicode(obj))
self.__serialize_string(unicode(obj))
- elif type(obj) == dict:
+ elif isinstance(obj, dict):
self.stream.write(u"{")
self.depth += 1
self.stream.write(u"{")
self.depth += 1
self.stream.write(u"}")
self.depth -= 1
self.stream.write(u"}")
self.depth -= 1
- elif type(obj) in (list, tuple):
+ elif isinstance(obj, (list, tuple)):
self.stream.write(u"[")
self.depth += 1
self.stream.write(u"[")
self.depth += 1
if m:
sign, integer, fraction, exp = m.groups()
if (exp is not None and
if m:
sign, integer, fraction, exp = m.groups()
if (exp is not None and
- (long(exp) > sys.maxint or long(exp) < -sys.maxint - 1)):
+ (int(exp) > sys.maxint or int(exp) < -sys.maxint - 1)):
self.__error("exponent outside valid range")
return
self.__error("exponent outside valid range")
return
if fraction is not None:
pow10 -= len(fraction)
if exp is not None:
if fraction is not None:
pow10 -= len(fraction)
if exp is not None:
if significand == 0:
self.__parser_input(0)
if significand == 0:
self.__parser_input(0)
self.parse_state = Parser.__parse_object_next
def __parse_value(self, token, string, next_state):
self.parse_state = Parser.__parse_object_next
def __parse_value(self, token, string, next_state):
- if token in [False, None, True] or type(token) in [int, long, float]:
+ number_types = list(six.integer_types)
+ number_types.extend([float])
+ number_types = tuple(number_types)
+ if token in [False, None, True] or isinstance(token, number_types):
self.__put_value(token)
elif token == 'string':
self.__put_value(string)
self.__put_value(token)
elif token == 'string':
self.__put_value(string)
raise argparse.ArgumentTypeError("Not a valid target bandwidth")
bwidth = string.replace("M", "000000")
bwidth = bwidth.replace("K", "000")
raise argparse.ArgumentTypeError("Not a valid target bandwidth")
bwidth = string.replace("M", "000000")
bwidth = bwidth.replace("K", "000")
- return long(bwidth) / 8 # Convert from bits to bytes
+ return int(bwidth) / 8 # Convert from bits to bytes
def tunnel_types(string):
def tunnel_types(string):
- rcv_bytes = long(server1.get_tcp_listener_results(listen_handle))
- snt_bytes = long(server2.get_tcp_sender_results(send_handle))
+ rcv_bytes = int(server1.get_tcp_listener_results(listen_handle))
+ snt_bytes = int(server2.get_tcp_sender_results(send_handle))
bwidth = rcv_bytes / duration
bwidth = rcv_bytes / duration