diff options
author | YAMAMOTO Takashi <yamamoto@valinux.co.jp> | 2015-01-16 17:19:57 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-01-19 00:51:55 +0900 |
commit | 22aaa699c93dac486d9b3030d5dc8468beef666f (patch) | |
tree | d00ed77523c45b3a04480361ec9b4c8cc3d154d5 | |
parent | ef909210b7617620c52580fceff397a2e417cb5e (diff) |
Cleanup isinstance(foo, (int, long))
Use numbers where appropriate.
Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/ofctl/api.py | 4 | ||||
-rw-r--r-- | ryu/app/ofctl/event.py | 4 | ||||
-rw-r--r-- | ryu/app/ofctl/service.py | 6 | ||||
-rw-r--r-- | ryu/app/rest_router.py | 3 | ||||
-rw-r--r-- | ryu/lib/packet/bgp.py | 3 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/application.py | 2 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/net_ctrl.py | 2 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/rtconf/base.py | 5 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/rtconf/common.py | 19 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/rtconf/neighbors.py | 3 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/utils/validation.py | 7 | ||||
-rw-r--r-- | ryu/tests/unit/lib/test_rpc.py | 3 |
12 files changed, 37 insertions, 24 deletions
diff --git a/ryu/app/ofctl/api.py b/ryu/app/ofctl/api.py index 8a62125a..d6485244 100644 --- a/ryu/app/ofctl/api.py +++ b/ryu/app/ofctl/api.py @@ -16,6 +16,8 @@ # client for ryu.app.ofctl.service +import numbers + from ryu.base import app_manager import event @@ -29,7 +31,7 @@ def get_datapath(app, dpid): Returns None on error. """ - assert isinstance(dpid, (int, long)) + assert isinstance(dpid, numbers.Integral) return app.send_request(event.GetDatapathRequest(dpid=dpid))() diff --git a/ryu/app/ofctl/event.py b/ryu/app/ofctl/event.py index 909f2a84..8919452e 100644 --- a/ryu/app/ofctl/event.py +++ b/ryu/app/ofctl/event.py @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numbers + from ryu.controller import event @@ -32,7 +34,7 @@ class _ReplyBase(event.EventReplyBase): class GetDatapathRequest(_RequestBase): def __init__(self, dpid): - assert isinstance(dpid, (int, long)) + assert isinstance(dpid, numbers.Integral) super(GetDatapathRequest, self).__init__() self.dpid = dpid diff --git a/ryu/app/ofctl/service.py b/ryu/app/ofctl/service.py index cb82d72f..fd0b7a7f 100644 --- a/ryu/app/ofctl/service.py +++ b/ryu/app/ofctl/service.py @@ -16,6 +16,8 @@ # ofctl service +import numbers + from ryu.base import app_manager from ryu.controller import ofp_event @@ -71,7 +73,7 @@ class OfctlService(app_manager.RyuApp): def _switch_features_handler(self, ev): datapath = ev.msg.datapath id = datapath.id - assert isinstance(id, (int, long)) + assert isinstance(id, numbers.Integral) old_info = self._switches.get(id, None) new_info = _SwitchInfo(datapath=datapath) self.logger.debug('add dpid %s datapath %s new_info %s old_info %s' % @@ -96,7 +98,7 @@ class OfctlService(app_manager.RyuApp): @set_ev_cls(event.GetDatapathRequest, MAIN_DISPATCHER) def _handle_get_datapath(self, req): id = req.dpid - assert isinstance(id, (int, long)) + assert isinstance(id, numbers.Integral) try: datapath = self._switches[id].datapath except KeyError: diff --git a/ryu/app/rest_router.py b/ryu/app/rest_router.py index c57e003f..5cf2e24c 100644 --- a/ryu/app/rest_router.py +++ b/ryu/app/rest_router.py @@ -15,6 +15,7 @@ import logging +import numbers import socket import struct @@ -1875,7 +1876,7 @@ def ipv4_apply_mask(address, prefix_len, err_msg=None): def ipv4_int_to_text(ip_int): - assert isinstance(ip_int, (int, long)) + assert isinstance(ip_int, numbers.Integral) return addrconv.ipv4.bin_to_text(struct.pack('!I', ip_int)) diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py index 4660c303..12808c9d 100644 --- a/ryu/lib/packet/bgp.py +++ b/ryu/lib/packet/bgp.py @@ -27,6 +27,7 @@ import six import struct import copy import netaddr +import numbers from ryu.ofproto.ofproto_parser import msg_pack_into from ryu.lib.stringify import StringifyMixin @@ -1026,7 +1027,7 @@ class RouteTargetMembershipNLRI(StringifyMixin): """ valid = True # AS number should be a 16 bit number - if (not isinstance(asn, (int, long)) or (asn < 0) or + if (not isinstance(asn, numbers.Integral) or (asn < 0) or (asn > ((2 ** 16) - 1))): valid = False diff --git a/ryu/services/protocols/bgp/application.py b/ryu/services/protocols/bgp/application.py index 43aac8b9..69647b3d 100644 --- a/ryu/services/protocols/bgp/application.py +++ b/ryu/services/protocols/bgp/application.py @@ -119,7 +119,7 @@ class RyuBGPSpeaker(RyuApp): """ if not port: raise ApplicationException(desc='Invalid rpc port number.') - if not isinstance(port, (int, long)) and isinstance(port, str): + if isinstance(port, str): port = int(port) return port diff --git a/ryu/services/protocols/bgp/net_ctrl.py b/ryu/services/protocols/bgp/net_ctrl.py index f0dd888d..01a2a692 100644 --- a/ryu/services/protocols/bgp/net_ctrl.py +++ b/ryu/services/protocols/bgp/net_ctrl.py @@ -300,7 +300,7 @@ def _validate_rpc_port(port): """ if not port: raise NetworkControllerError(desc='Invalid rpc port number.') - if not isinstance(port, (int, long)) and isinstance(port, str): + if isinstance(port, str): port = int(port) if port <= 0: diff --git a/ryu/services/protocols/bgp/rtconf/base.py b/ryu/services/protocols/bgp/rtconf/base.py index 8dc46658..5925e6ed 100644 --- a/ryu/services/protocols/bgp/rtconf/base.py +++ b/ryu/services/protocols/bgp/rtconf/base.py @@ -18,6 +18,7 @@ """ from abc import ABCMeta from abc import abstractmethod +import numbers import logging import uuid from types import BooleanType @@ -563,9 +564,9 @@ def validate_stats_log_enabled(stats_log_enabled): @validate(name=ConfWithStats.STATS_TIME) def validate_stats_time(stats_time): - if not isinstance(stats_time, (int, long)): + if not isinstance(stats_time, numbers.Integral): raise ConfigTypeError(desc='Statistics log timer value has to be of ' - 'type int/long but got: %r' % stats_time) + 'integral type but got: %r' % stats_time) if stats_time < 10: raise ConfigValueError(desc='Statistics log timer cannot be set to ' 'less then 10 sec, given timer value %s.' % diff --git a/ryu/services/protocols/bgp/rtconf/common.py b/ryu/services/protocols/bgp/rtconf/common.py index 05d3a7ac..b4ac8daf 100644 --- a/ryu/services/protocols/bgp/rtconf/common.py +++ b/ryu/services/protocols/bgp/rtconf/common.py @@ -17,6 +17,7 @@ Runtime configuration that applies to all bgp sessions, i.e. global settings. """ import logging +import numbers from ryu.services.protocols.bgp.utils.validation import is_valid_ipv4 from ryu.services.protocols.bgp.utils.validation import is_valid_old_asn @@ -105,9 +106,9 @@ def validate_router_id(router_id): @validate(name=REFRESH_STALEPATH_TIME) def validate_refresh_stalepath_time(rst): - if not isinstance(rst, (int, long)): + if not isinstance(rst, numbers.Integral): raise ConfigTypeError(desc=('Configuration value for %s has to be ' - 'int/long' % REFRESH_STALEPATH_TIME)) + 'integral type' % REFRESH_STALEPATH_TIME)) if rst < 0: raise ConfigValueError(desc='Invalid refresh stalepath time %s' % rst) @@ -116,9 +117,9 @@ def validate_refresh_stalepath_time(rst): @validate(name=REFRESH_MAX_EOR_TIME) def validate_refresh_max_eor_time(rmet): - if not isinstance(rmet, (int, long)): + if not isinstance(rmet, numbers.Integral): raise ConfigTypeError(desc=('Configuration value for %s has to be of ' - 'type int/long ' % REFRESH_MAX_EOR_TIME)) + 'integral type ' % REFRESH_MAX_EOR_TIME)) if rmet < 0: raise ConfigValueError(desc='Invalid refresh stalepath time %s' % rmet) @@ -129,8 +130,8 @@ def validate_refresh_max_eor_time(rmet): def validate_label_range(label_range): min_label, max_label = label_range if (not min_label or not max_label - or not isinstance(min_label, (int, long)) - or not isinstance(max_label, (int, long)) or min_label < 17 + or not isinstance(min_label, numbers.Integral) + or not isinstance(max_label, numbers.Integral) or min_label < 17 or min_label >= max_label): raise ConfigValueError(desc=('Invalid label_range configuration value:' ' (%s).' % label_range)) @@ -140,7 +141,7 @@ def validate_label_range(label_range): @validate(name=BGP_SERVER_PORT) def validate_bgp_server_port(server_port): - if not isinstance(server_port, (int, long)): + if not isinstance(server_port, numbers.Integral): raise ConfigTypeError(desc=('Invalid bgp sever port configuration ' 'value %s' % server_port)) if server_port <= 0 or server_port > 65535: @@ -153,7 +154,7 @@ def validate_bgp_server_port(server_port): def validate_tcp_conn_timeout(tcp_conn_timeout): # TODO(apgw-dev) made-up some valid values for this settings, check if we # have a standard value in any routers - if not isinstance(tcp_conn_timeout, (int, long)): + if not isinstance(tcp_conn_timeout, numbers.Integral): raise ConfigTypeError(desc=('Invalid tcp connection timeout ' 'configuration value %s' % tcp_conn_timeout)) @@ -168,7 +169,7 @@ def validate_tcp_conn_timeout(tcp_conn_timeout): @validate(name=BGP_CONN_RETRY_TIME) def validate_bgp_conn_retry_time(bgp_conn_retry_time): - if not isinstance(bgp_conn_retry_time, (int, long)): + if not isinstance(bgp_conn_retry_time, numbers.Integral): raise ConfigTypeError(desc=('Invalid bgp conn. retry time ' 'configuration value %s' % bgp_conn_retry_time)) diff --git a/ryu/services/protocols/bgp/rtconf/neighbors.py b/ryu/services/protocols/bgp/rtconf/neighbors.py index 29a000f5..2e10c81f 100644 --- a/ryu/services/protocols/bgp/rtconf/neighbors.py +++ b/ryu/services/protocols/bgp/rtconf/neighbors.py @@ -19,6 +19,7 @@ from abc import abstractmethod import logging import netaddr +import numbers from ryu.lib.packet.bgp import RF_IPv4_UC from ryu.lib.packet.bgp import RF_IPv6_UC @@ -170,7 +171,7 @@ def validate_password(password): @validate(name=LOCAL_PORT) def validate_local_port(port): - if not isinstance(port, (int, long)): + if not isinstance(port, numbers.Integral): raise ConfigTypeError(desc='Invalid local port: %s' % port) if port < 1025 or port > 65535: raise ConfigValueError(desc='Invalid local port value: %s, has to be' diff --git a/ryu/services/protocols/bgp/utils/validation.py b/ryu/services/protocols/bgp/utils/validation.py index 577e98ca..9851998f 100644 --- a/ryu/services/protocols/bgp/utils/validation.py +++ b/ryu/services/protocols/bgp/utils/validation.py @@ -16,6 +16,7 @@ """ Module provides utilities for validation. """ +import numbers import socket @@ -120,7 +121,7 @@ def is_valid_old_asn(asn): """ valid = True # AS number should be a 16 bit number - if (not isinstance(asn, (int, long)) or (asn < 0) or + if (not isinstance(asn, numbers.Integral) or (asn < 0) or (asn > ((2 ** 16) - 1))): valid = False @@ -165,7 +166,7 @@ def is_valid_med(med): """ valid = True - if not isinstance(med, (int, long)): + if not isinstance(med, numbers.Integral): valid = False else: if med < 0 or med > (2 ** 32) - 1: @@ -187,7 +188,7 @@ def is_valid_mpls_label(label): """ valid = True - if (not isinstance(label, (int, long)) or + if (not isinstance(label, numbers.Integral) or (label >= 4 and label <= 15) or (label < 0 or label > 2 ** 20)): valid = False diff --git a/ryu/tests/unit/lib/test_rpc.py b/ryu/tests/unit/lib/test_rpc.py index 77b24393..1c9bf711 100644 --- a/ryu/tests/unit/lib/test_rpc.py +++ b/ryu/tests/unit/lib/test_rpc.py @@ -17,6 +17,7 @@ # limitations under the License. import sys +import numbers import time import unittest from nose.tools import raises @@ -136,7 +137,7 @@ class Test_rpc(unittest.TestCase): c = rpc.Client(self._client_sock) # NOTE: the python type of this value is int for 64-bit arch obj = -0x8000000000000000 # min value for msgpack - assert isinstance(obj, (int, long)) + assert isinstance(obj, numbers.Integral) result = c.call("resp", [obj]) assert result == obj assert isinstance(result, type(obj)) |