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>
This commit is contained in:
YAMAMOTO Takashi 2015-01-16 17:19:57 +09:00 committed by FUJITA Tomonori
parent ef909210b7
commit 22aaa699c9
12 changed files with 37 additions and 24 deletions

View File

@ -16,6 +16,8 @@
# client for ryu.app.ofctl.service # client for ryu.app.ofctl.service
import numbers
from ryu.base import app_manager from ryu.base import app_manager
import event import event
@ -29,7 +31,7 @@ def get_datapath(app, dpid):
Returns None on error. Returns None on error.
""" """
assert isinstance(dpid, (int, long)) assert isinstance(dpid, numbers.Integral)
return app.send_request(event.GetDatapathRequest(dpid=dpid))() return app.send_request(event.GetDatapathRequest(dpid=dpid))()

View File

@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import numbers
from ryu.controller import event from ryu.controller import event
@ -32,7 +34,7 @@ class _ReplyBase(event.EventReplyBase):
class GetDatapathRequest(_RequestBase): class GetDatapathRequest(_RequestBase):
def __init__(self, dpid): def __init__(self, dpid):
assert isinstance(dpid, (int, long)) assert isinstance(dpid, numbers.Integral)
super(GetDatapathRequest, self).__init__() super(GetDatapathRequest, self).__init__()
self.dpid = dpid self.dpid = dpid

View File

@ -16,6 +16,8 @@
# ofctl service # ofctl service
import numbers
from ryu.base import app_manager from ryu.base import app_manager
from ryu.controller import ofp_event from ryu.controller import ofp_event
@ -71,7 +73,7 @@ class OfctlService(app_manager.RyuApp):
def _switch_features_handler(self, ev): def _switch_features_handler(self, ev):
datapath = ev.msg.datapath datapath = ev.msg.datapath
id = datapath.id id = datapath.id
assert isinstance(id, (int, long)) assert isinstance(id, numbers.Integral)
old_info = self._switches.get(id, None) old_info = self._switches.get(id, None)
new_info = _SwitchInfo(datapath=datapath) new_info = _SwitchInfo(datapath=datapath)
self.logger.debug('add dpid %s datapath %s new_info %s old_info %s' % 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) @set_ev_cls(event.GetDatapathRequest, MAIN_DISPATCHER)
def _handle_get_datapath(self, req): def _handle_get_datapath(self, req):
id = req.dpid id = req.dpid
assert isinstance(id, (int, long)) assert isinstance(id, numbers.Integral)
try: try:
datapath = self._switches[id].datapath datapath = self._switches[id].datapath
except KeyError: except KeyError:

View File

@ -15,6 +15,7 @@
import logging import logging
import numbers
import socket import socket
import struct import struct
@ -1875,7 +1876,7 @@ def ipv4_apply_mask(address, prefix_len, err_msg=None):
def ipv4_int_to_text(ip_int): 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)) return addrconv.ipv4.bin_to_text(struct.pack('!I', ip_int))

View File

@ -27,6 +27,7 @@ import six
import struct import struct
import copy import copy
import netaddr import netaddr
import numbers
from ryu.ofproto.ofproto_parser import msg_pack_into from ryu.ofproto.ofproto_parser import msg_pack_into
from ryu.lib.stringify import StringifyMixin from ryu.lib.stringify import StringifyMixin
@ -1026,7 +1027,7 @@ class RouteTargetMembershipNLRI(StringifyMixin):
""" """
valid = True valid = True
# AS number should be a 16 bit number # 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))): (asn > ((2 ** 16) - 1))):
valid = False valid = False

View File

@ -119,7 +119,7 @@ class RyuBGPSpeaker(RyuApp):
""" """
if not port: if not port:
raise ApplicationException(desc='Invalid rpc port number.') raise ApplicationException(desc='Invalid rpc port number.')
if not isinstance(port, (int, long)) and isinstance(port, str): if isinstance(port, str):
port = int(port) port = int(port)
return port return port

View File

@ -300,7 +300,7 @@ def _validate_rpc_port(port):
""" """
if not port: if not port:
raise NetworkControllerError(desc='Invalid rpc port number.') raise NetworkControllerError(desc='Invalid rpc port number.')
if not isinstance(port, (int, long)) and isinstance(port, str): if isinstance(port, str):
port = int(port) port = int(port)
if port <= 0: if port <= 0:

View File

@ -18,6 +18,7 @@
""" """
from abc import ABCMeta from abc import ABCMeta
from abc import abstractmethod from abc import abstractmethod
import numbers
import logging import logging
import uuid import uuid
from types import BooleanType from types import BooleanType
@ -563,9 +564,9 @@ def validate_stats_log_enabled(stats_log_enabled):
@validate(name=ConfWithStats.STATS_TIME) @validate(name=ConfWithStats.STATS_TIME)
def validate_stats_time(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 ' 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: if stats_time < 10:
raise ConfigValueError(desc='Statistics log timer cannot be set to ' raise ConfigValueError(desc='Statistics log timer cannot be set to '
'less then 10 sec, given timer value %s.' % 'less then 10 sec, given timer value %s.' %

View File

@ -17,6 +17,7 @@
Runtime configuration that applies to all bgp sessions, i.e. global settings. Runtime configuration that applies to all bgp sessions, i.e. global settings.
""" """
import logging 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_ipv4
from ryu.services.protocols.bgp.utils.validation import is_valid_old_asn 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) @validate(name=REFRESH_STALEPATH_TIME)
def validate_refresh_stalepath_time(rst): 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 ' raise ConfigTypeError(desc=('Configuration value for %s has to be '
'int/long' % REFRESH_STALEPATH_TIME)) 'integral type' % REFRESH_STALEPATH_TIME))
if rst < 0: if rst < 0:
raise ConfigValueError(desc='Invalid refresh stalepath time %s' % rst) 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) @validate(name=REFRESH_MAX_EOR_TIME)
def validate_refresh_max_eor_time(rmet): 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 ' 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: if rmet < 0:
raise ConfigValueError(desc='Invalid refresh stalepath time %s' % rmet) 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): def validate_label_range(label_range):
min_label, max_label = label_range min_label, max_label = label_range
if (not min_label or not max_label if (not min_label or not max_label
or not isinstance(min_label, (int, long)) or not isinstance(min_label, numbers.Integral)
or not isinstance(max_label, (int, long)) or min_label < 17 or not isinstance(max_label, numbers.Integral) or min_label < 17
or min_label >= max_label): or min_label >= max_label):
raise ConfigValueError(desc=('Invalid label_range configuration value:' raise ConfigValueError(desc=('Invalid label_range configuration value:'
' (%s).' % label_range)) ' (%s).' % label_range))
@ -140,7 +141,7 @@ def validate_label_range(label_range):
@validate(name=BGP_SERVER_PORT) @validate(name=BGP_SERVER_PORT)
def validate_bgp_server_port(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 ' raise ConfigTypeError(desc=('Invalid bgp sever port configuration '
'value %s' % server_port)) 'value %s' % server_port))
if server_port <= 0 or server_port > 65535: 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): def validate_tcp_conn_timeout(tcp_conn_timeout):
# TODO(apgw-dev) made-up some valid values for this settings, check if we # TODO(apgw-dev) made-up some valid values for this settings, check if we
# have a standard value in any routers # 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 ' raise ConfigTypeError(desc=('Invalid tcp connection timeout '
'configuration value %s' % 'configuration value %s' %
tcp_conn_timeout)) tcp_conn_timeout))
@ -168,7 +169,7 @@ def validate_tcp_conn_timeout(tcp_conn_timeout):
@validate(name=BGP_CONN_RETRY_TIME) @validate(name=BGP_CONN_RETRY_TIME)
def validate_bgp_conn_retry_time(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 ' raise ConfigTypeError(desc=('Invalid bgp conn. retry time '
'configuration value %s' % 'configuration value %s' %
bgp_conn_retry_time)) bgp_conn_retry_time))

View File

@ -19,6 +19,7 @@
from abc import abstractmethod from abc import abstractmethod
import logging import logging
import netaddr import netaddr
import numbers
from ryu.lib.packet.bgp import RF_IPv4_UC from ryu.lib.packet.bgp import RF_IPv4_UC
from ryu.lib.packet.bgp import RF_IPv6_UC from ryu.lib.packet.bgp import RF_IPv6_UC
@ -170,7 +171,7 @@ def validate_password(password):
@validate(name=LOCAL_PORT) @validate(name=LOCAL_PORT)
def validate_local_port(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) raise ConfigTypeError(desc='Invalid local port: %s' % port)
if port < 1025 or port > 65535: if port < 1025 or port > 65535:
raise ConfigValueError(desc='Invalid local port value: %s, has to be' raise ConfigValueError(desc='Invalid local port value: %s, has to be'

View File

@ -16,6 +16,7 @@
""" """
Module provides utilities for validation. Module provides utilities for validation.
""" """
import numbers
import socket import socket
@ -120,7 +121,7 @@ def is_valid_old_asn(asn):
""" """
valid = True valid = True
# AS number should be a 16 bit number # 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))): (asn > ((2 ** 16) - 1))):
valid = False valid = False
@ -165,7 +166,7 @@ def is_valid_med(med):
""" """
valid = True valid = True
if not isinstance(med, (int, long)): if not isinstance(med, numbers.Integral):
valid = False valid = False
else: else:
if med < 0 or med > (2 ** 32) - 1: if med < 0 or med > (2 ** 32) - 1:
@ -187,7 +188,7 @@ def is_valid_mpls_label(label):
""" """
valid = True valid = True
if (not isinstance(label, (int, long)) or if (not isinstance(label, numbers.Integral) or
(label >= 4 and label <= 15) or (label >= 4 and label <= 15) or
(label < 0 or label > 2 ** 20)): (label < 0 or label > 2 ** 20)):
valid = False valid = False

View File

@ -17,6 +17,7 @@
# limitations under the License. # limitations under the License.
import sys import sys
import numbers
import time import time
import unittest import unittest
from nose.tools import raises from nose.tools import raises
@ -136,7 +137,7 @@ class Test_rpc(unittest.TestCase):
c = rpc.Client(self._client_sock) c = rpc.Client(self._client_sock)
# NOTE: the python type of this value is int for 64-bit arch # NOTE: the python type of this value is int for 64-bit arch
obj = -0x8000000000000000 # min value for msgpack obj = -0x8000000000000000 # min value for msgpack
assert isinstance(obj, (int, long)) assert isinstance(obj, numbers.Integral)
result = c.call("resp", [obj]) result = c.call("resp", [obj])
assert result == obj assert result == obj
assert isinstance(result, type(obj)) assert isinstance(result, type(obj))