From patchwork Mon Dec 21 20:47:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell Bryant X-Patchwork-Id: 559727 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (unknown [IPv6:2600:3c00::f03c:91ff:fe6e:bdf7]) by ozlabs.org (Postfix) with ESMTP id 955E0140BB2 for ; Tue, 22 Dec 2015 07:53:45 +1100 (AEDT) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 5745E10759; Mon, 21 Dec 2015 12:48:40 -0800 (PST) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx1e4.cudamail.com (mx1.cudamail.com [69.90.118.67]) by archives.nicira.com (Postfix) with ESMTPS id C4E91105E5 for ; Mon, 21 Dec 2015 12:48:36 -0800 (PST) Received: from bar5.cudamail.com (unknown [192.168.21.12]) by mx1e4.cudamail.com (Postfix) with ESMTPS id 35B0B1E0125 for ; Mon, 21 Dec 2015 13:48:36 -0700 (MST) X-ASG-Debug-ID: 1450730915-09eadd632ec67a0001-byXFYA Received: from mx1-pf2.cudamail.com ([192.168.24.2]) by bar5.cudamail.com with ESMTP id FyqduNXzaGqyrXBD (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 21 Dec 2015 13:48:35 -0700 (MST) X-Barracuda-Envelope-From: russell@ovn.org X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.2 Received: from unknown (HELO mx1.redhat.com) (209.132.183.28) by mx1-pf2.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 21 Dec 2015 20:48:35 -0000 Received-SPF: neutral (mx1-pf2.cudamail.com: 209.132.183.28 is neither permitted nor denied by SPF record at ovn.org) X-Barracuda-Apparent-Source-IP: 209.132.183.28 X-Barracuda-RBL-IP: 209.132.183.28 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id E0ACE8F511; Mon, 21 Dec 2015 20:48:34 +0000 (UTC) Received: from x1c.redhat.com ([10.3.112.11]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBLKm0dr002018; Mon, 21 Dec 2015 15:48:34 -0500 X-CudaMail-Envelope-Sender: russell@ovn.org From: Russell Bryant To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E2-1220082826 X-CudaMail-DTE: 122115 X-CudaMail-Originating-IP: 209.132.183.28 Date: Mon, 21 Dec 2015 15:47:30 -0500 X-ASG-Orig-Subj: [##CM-E2-1220082826##][PATCH 30/55] python: Remove reamining direct type comarisons. Message-Id: <1450730875-18083-31-git-send-email-russell@ovn.org> In-Reply-To: <1450730875-18083-1-git-send-email-russell@ovn.org> References: <1450730875-18083-1-git-send-email-russell@ovn.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Barracuda-Connect: UNKNOWN[192.168.24.2] X-Barracuda-Start-Time: 1450730915 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Subject: [ovs-dev] [PATCH 30/55] python: Remove reamining direct type comarisons. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" 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 --- 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(-) 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(" is not an object", table_updates) @@ -371,7 +371,7 @@ class Idl(object): raise error.Error(' includes unknown ' 'table "%s"' % table_name) - if type(table_update) != dict: + if not isinstance(table_update, dict): raise error.Error(' 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(' for table "%s" ' 'contains 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