mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +00:00
python: Drop usage of long type.
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>
This commit is contained in:
@@ -108,12 +108,16 @@ class Atom(object):
|
||||
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
|
||||
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
|
||||
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))
|
||||
@@ -237,13 +241,13 @@ class Atom(object):
|
||||
|
||||
@staticmethod
|
||||
def new(x):
|
||||
if type(x) in [int, long]:
|
||||
if isinstance(x, six.integer_types):
|
||||
t = ovs.db.types.IntegerType
|
||||
elif type(x) == float:
|
||||
elif isinstance(x, float):
|
||||
t = ovs.db.types.RealType
|
||||
elif x in [False, True]:
|
||||
elif isinstance(x, bool):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user