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
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))()

View File

@ -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

View File

@ -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:

View File

@ -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))

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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.' %

View File

@ -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))

View File

@ -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'

View File

@ -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

View File

@ -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))