[ovs-dev,30/55] python: Remove reamining direct type comarisons.
diff mbox

Message ID 1450730875-18083-31-git-send-email-russell@ovn.org
State Deferred
Headers show

Commit Message

Russell Bryant Dec. 21, 2015, 8:47 p.m. UTC
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>
---
 python/ovs/db/data.py     |  6 +++---
 python/ovs/db/idl.py      | 28 ++++++++++++++--------------
 python/ovs/db/parser.py   |  6 +++---
 python/ovs/db/schema.py   |  2 +-
 python/ovs/json.py        |  4 ++--
 python/ovs/jsonrpc.py     |  4 ++--
 python/ovs/socket_util.py |  2 +-
 7 files changed, 26 insertions(+), 26 deletions(-)

Patch
diff mbox

diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py
index c2915e1..61063b1 100644
--- a/python/ovs/db/data.py
+++ b/python/ovs/db/data.py
@@ -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
diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index 26cf6d7..632bab0 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -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(self, 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
@@ -818,7 +818,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])):
@@ -1141,7 +1141,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:
@@ -1156,7 +1156,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":
@@ -1202,7 +1202,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
@@ -1353,8 +1353,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
@@ -1366,7 +1366,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):
@@ -1401,7 +1401,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]
diff --git a/python/ovs/db/parser.py b/python/ovs/db/parser.py
index d48a790..b93fdf7 100644
--- a/python/ovs/db/parser.py
+++ b/python/ovs/db/parser.py
@@ -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
diff --git a/python/ovs/db/schema.py b/python/ovs/db/schema.py
index 28b14d3..92782df 100644
--- a/python/ovs/db/schema.py
+++ b/python/ovs/db/schema.py
@@ -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:
diff --git a/python/ovs/json.py b/python/ovs/json.py
index db74397..42e697d 100644
--- a/python/ovs/json.py
+++ b/python/ovs/json.py
@@ -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
diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py
index f576b25..efe0f6c 100644
--- a/python/ovs/jsonrpc.py
+++ b/python/ovs/jsonrpc.py
@@ -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.
diff --git a/python/ovs/socket_util.py b/python/ovs/socket_util.py
index 641ea47..071d67b 100644
--- a/python/ovs/socket_util.py
+++ b/python/ovs/socket_util.py
@@ -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