Skip to content

Commit

Permalink
python: Drop usage of long type.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
russellb committed Feb 2, 2016
1 parent 490bafe commit 8f80884
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
20 changes: 12 additions & 8 deletions python/ovs/db/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ def is_default(self):
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))
Expand Down Expand Up @@ -237,13 +241,13 @@ def to_string(self):

@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
Expand Down
4 changes: 2 additions & 2 deletions python/ovs/db/idl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ def __process_inc_reply(self, ops):
# __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:
Expand All @@ -1284,7 +1284,7 @@ def __process_inc_reply(self, ops):
'"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
Expand Down
15 changes: 10 additions & 5 deletions python/ovs/db/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import re

import six

from ovs.db import error


Expand Down Expand Up @@ -80,17 +82,20 @@ def is_identifier(s):


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"
elif type_ == bool:
elif issubclass(type_, bool):
return "boolean"
elif type_ == dict:
elif issubclass(type_, dict):
return "object"
elif type_ == list:
elif issubclass(type_, list):
return "array"
elif type_ in [int, long, float]:
elif issubclass(type_, number_types):
return "number"
elif type_ in [str, unicode]:
elif issubclass(type_, (str, unicode)):
return "string"
else:
return "<invalid>"
Expand Down
20 changes: 13 additions & 7 deletions python/ovs/db/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import sys
import uuid

import six

from ovs.db import error
import ovs.db.parser
import ovs.db.data
Expand Down Expand Up @@ -54,9 +56,13 @@ def to_json(self):
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, ())
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,))
Expand Down Expand Up @@ -148,7 +154,7 @@ def __ne__(self, other):

@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:
Expand All @@ -173,14 +179,14 @@ def from_json(json):
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:
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)
Expand Down
21 changes: 12 additions & 9 deletions python/ovs/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def serialize(self, obj):
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)
elif type(obj) == float:
elif isinstance(obj, float):
self.stream.write("%.15g" % obj)
elif type(obj) == unicode:
elif isinstance(obj, unicode):
self.__serialize_string(obj)
elif type(obj) == str:
elif isinstance(obj, str):
self.__serialize_string(unicode(obj))
elif type(obj) == dict:
elif isinstance(obj, dict):
self.stream.write(u"{")

self.depth += 1
Expand All @@ -87,7 +87,7 @@ def serialize(self, obj):

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

Expand Down Expand Up @@ -248,7 +248,7 @@ def __lex_finish_number(self):
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

Expand All @@ -264,7 +264,7 @@ def __lex_finish_number(self):
if fraction is not None:
pow10 -= len(fraction)
if exp is not None:
pow10 += long(exp)
pow10 += int(exp)

if significand == 0:
self.__parser_input(0)
Expand Down Expand Up @@ -527,7 +527,10 @@ def __parser_pop(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion python/ovstest/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def bandwidth(string):
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):
Expand Down
4 changes: 2 additions & 2 deletions python/ovstest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def do_tcp_tests(receiver, sender, duration):

time.sleep(duration + 1)

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

Expand Down

0 comments on commit 8f80884

Please sign in to comment.