mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +00:00
python: Remove reamining direct type comparisons.
I've hit several bugs in this Python 3 work where the fix was some code needed to be converted to use isinstance(). This has been primarily around deadling with the changes to unicode handling. Go ahead and convert the rest of the direct type comparisons to use isinstance(), as it could avoid a bug I haven't hit yet and it's more Pythonic, anyway. Signed-off-by: Russell Bryant <russell@ovn.org> Acked-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
@@ -317,7 +317,7 @@ class Datum(object):
|
||||
that this function accepts."""
|
||||
is_map = type_.is_map()
|
||||
if (is_map or
|
||||
(type(json) == list and len(json) > 0 and json[0] == "set")):
|
||||
(isinstance(json, list) and len(json) > 0 and json[0] == "set")):
|
||||
if is_map:
|
||||
class_ = "map"
|
||||
else:
|
||||
@@ -481,12 +481,12 @@ class Datum(object):
|
||||
Raises ovs.db.error.Error if 'value' is not in an appropriate form for
|
||||
'type_'."""
|
||||
d = {}
|
||||
if type(value) == dict:
|
||||
if isinstance(value, dict):
|
||||
for k, v in six.iteritems(value):
|
||||
ka = Atom.from_python(type_.key, row_to_uuid(k))
|
||||
va = Atom.from_python(type_.value, row_to_uuid(v))
|
||||
d[ka] = va
|
||||
elif type(value) in (list, tuple):
|
||||
elif isinstance(value, (list, tuple)):
|
||||
for k in value:
|
||||
ka = Atom.from_python(type_.key, row_to_uuid(k))
|
||||
d[ka] = None
|
||||
|
||||
@@ -324,14 +324,14 @@ class Idl(object):
|
||||
|
||||
def __parse_lock_reply(self, result):
|
||||
self._lock_request_id = None
|
||||
got_lock = type(result) == dict and result.get("locked") is True
|
||||
got_lock = isinstance(result, dict) and result.get("locked") is True
|
||||
self.__update_has_lock(got_lock)
|
||||
if not got_lock:
|
||||
self.is_lock_contended = True
|
||||
|
||||
def __parse_lock_notify(self, params, new_has_lock):
|
||||
if (self.lock_name is not None
|
||||
and type(params) in (list, tuple)
|
||||
and isinstance(params, (list, tuple))
|
||||
and params
|
||||
and params[0] == self.lock_name):
|
||||
self.__update_has_lock(new_has_lock)
|
||||
@@ -361,7 +361,7 @@ class Idl(object):
|
||||
% (self._session.get_name(), e))
|
||||
|
||||
def __do_parse_update(self, table_updates):
|
||||
if type(table_updates) != dict:
|
||||
if not isinstance(table_updates, dict):
|
||||
raise error.Error("<table-updates> is not an object",
|
||||
table_updates)
|
||||
|
||||
@@ -371,7 +371,7 @@ class Idl(object):
|
||||
raise error.Error('<table-updates> includes unknown '
|
||||
'table "%s"' % table_name)
|
||||
|
||||
if type(table_update) != dict:
|
||||
if not isinstance(table_update, dict):
|
||||
raise error.Error('<table-update> for table "%s" is not '
|
||||
'an object' % table_name, table_update)
|
||||
|
||||
@@ -383,7 +383,7 @@ class Idl(object):
|
||||
table_update)
|
||||
uuid = ovs.ovsuuid.from_string(uuid_string)
|
||||
|
||||
if type(row_update) != dict:
|
||||
if not isinstance(row_update, dict):
|
||||
raise error.Error('<table-update> for table "%s" '
|
||||
'contains <row-update> for %s that '
|
||||
'is not an object'
|
||||
@@ -498,7 +498,7 @@ def _uuid_to_row(atom, base):
|
||||
|
||||
|
||||
def _row_to_uuid(value):
|
||||
if type(value) == Row:
|
||||
if isinstance(value, Row):
|
||||
return value.uuid
|
||||
else:
|
||||
return value
|
||||
@@ -827,7 +827,7 @@ class Transaction(object):
|
||||
poller.immediate_wake()
|
||||
|
||||
def _substitute_uuids(self, json):
|
||||
if type(json) in (list, tuple):
|
||||
if isinstance(json, (list, tuple)):
|
||||
if (len(json) == 2
|
||||
and json[0] == 'uuid'
|
||||
and ovs.ovsuuid.is_valid_string(json[1])):
|
||||
@@ -1150,7 +1150,7 @@ class Transaction(object):
|
||||
def _process_reply(self, msg):
|
||||
if msg.type == ovs.jsonrpc.Message.T_ERROR:
|
||||
self._status = Transaction.ERROR
|
||||
elif type(msg.result) not in (list, tuple):
|
||||
elif not isinstance(msg.result, (list, tuple)):
|
||||
# XXX rate-limit
|
||||
vlog.warn('reply to "transact" is not JSON array')
|
||||
else:
|
||||
@@ -1165,7 +1165,7 @@ class Transaction(object):
|
||||
# prior operation failed, so make sure that we know about
|
||||
# it.
|
||||
soft_errors = True
|
||||
elif type(op) == dict:
|
||||
elif isinstance(op, dict):
|
||||
error = op.get("error")
|
||||
if error is not None:
|
||||
if error == "timed out":
|
||||
@@ -1211,7 +1211,7 @@ class Transaction(object):
|
||||
# XXX rate-limit
|
||||
vlog.warn("%s is missing" % name)
|
||||
return False
|
||||
elif type(json) not in types:
|
||||
elif not isinstance(json, tuple(types)):
|
||||
# XXX rate-limit
|
||||
vlog.warn("%s has unexpected type %s" % (name, type(json)))
|
||||
return False
|
||||
@@ -1362,8 +1362,8 @@ class SchemaHelper(object):
|
||||
'readonly' must be a list of strings.
|
||||
"""
|
||||
|
||||
assert type(table) is str
|
||||
assert type(columns) is list
|
||||
assert isinstance(table, six.string_types)
|
||||
assert isinstance(columns, list)
|
||||
|
||||
columns = set(columns) | self._tables.get(table, set())
|
||||
self._tables[table] = columns
|
||||
@@ -1375,7 +1375,7 @@ class SchemaHelper(object):
|
||||
|
||||
'table' must be a string
|
||||
"""
|
||||
assert type(table) is str
|
||||
assert isinstance(table, six.string_types)
|
||||
self._tables[table] = set() # empty set means all columns in the table
|
||||
|
||||
def register_all(self):
|
||||
@@ -1410,7 +1410,7 @@ class SchemaHelper(object):
|
||||
|
||||
new_columns = {}
|
||||
for column_name in columns:
|
||||
assert type(column_name) is str
|
||||
assert isinstance(column_name, six.string_types)
|
||||
assert column_name in table.columns
|
||||
|
||||
new_columns[column_name] = table.columns[column_name]
|
||||
|
||||
@@ -23,7 +23,7 @@ class Parser(object):
|
||||
def __init__(self, json, name):
|
||||
self.name = name
|
||||
self.json = json
|
||||
if type(json) != dict:
|
||||
if not isinstance(json, dict):
|
||||
self.__raise_error("Object expected.")
|
||||
self.used = set()
|
||||
|
||||
@@ -70,7 +70,7 @@ class Parser(object):
|
||||
|
||||
def float_to_int(x):
|
||||
# XXX still needed?
|
||||
if type(x) == float:
|
||||
if isinstance(x, float):
|
||||
integer = int(x)
|
||||
if integer == x and -2 ** 53 <= integer < 2 ** 53:
|
||||
return integer
|
||||
@@ -113,6 +113,6 @@ def unwrap_json(json, name, types, desc):
|
||||
|
||||
|
||||
def parse_json_pair(json):
|
||||
if type(json) != list or len(json) != 2:
|
||||
if not isinstance(json, list) or len(json) != 2:
|
||||
raise error.Error("expected 2-element array", json)
|
||||
return json
|
||||
|
||||
@@ -148,7 +148,7 @@ class IdlSchema(DbSchema):
|
||||
def column_set_from_json(json, columns):
|
||||
if json is None:
|
||||
return tuple(columns)
|
||||
elif type(json) != list:
|
||||
elif not isinstance(json, list):
|
||||
raise error.Error("array of distinct column names expected", json)
|
||||
else:
|
||||
for column_name in json:
|
||||
|
||||
@@ -498,7 +498,7 @@ class Parser(object):
|
||||
|
||||
def __put_value(self, value):
|
||||
top = self.stack[-1]
|
||||
if type(top) == dict:
|
||||
if isinstance(top, dict):
|
||||
top[self.member_name] = value
|
||||
else:
|
||||
top.append(value)
|
||||
@@ -527,7 +527,7 @@ class Parser(object):
|
||||
else:
|
||||
self.stack.pop()
|
||||
top = self.stack[-1]
|
||||
if type(top) == list:
|
||||
if isinstance(top, list):
|
||||
self.parse_state = Parser.__parse_array_next
|
||||
else:
|
||||
self.parse_state = Parser.__parse_object_next
|
||||
|
||||
@@ -90,7 +90,7 @@ class Message(object):
|
||||
return "%s %s have \"%s\"" % (type_name, verb, name)
|
||||
|
||||
def is_valid(self):
|
||||
if self.params is not None and type(self.params) != list:
|
||||
if self.params is not None and not isinstance(self.params, list):
|
||||
return "\"params\" must be JSON array"
|
||||
|
||||
pattern = {Message.T_REQUEST: 0x11001,
|
||||
@@ -109,7 +109,7 @@ class Message(object):
|
||||
|
||||
@staticmethod
|
||||
def from_json(json):
|
||||
if type(json) != dict:
|
||||
if not isinstance(json, dict):
|
||||
return "message is not a JSON object"
|
||||
|
||||
# Make a copy to avoid modifying the caller's dict.
|
||||
|
||||
@@ -244,7 +244,7 @@ def get_exception_errno(e):
|
||||
exception is documented as having two completely different forms of
|
||||
arguments: either a string or a (errno, string) tuple. We only want the
|
||||
errno."""
|
||||
if type(e.args) == tuple:
|
||||
if isinstance(e.args, tuple):
|
||||
return e.args[0]
|
||||
else:
|
||||
return errno.EPROTO
|
||||
|
||||
Reference in New Issue
Block a user