Changed all type checking to use isinstance

This commit is contained in:
John (J5) Palmieri 2006-10-03 17:06:48 -04:00
parent 7c342e75bf
commit 6f337e0b14
7 changed files with 43 additions and 43 deletions

View File

@ -116,7 +116,7 @@ class Buddy(object):
def __init__(self, bus_name, object_id, service, icon_cache): def __init__(self, bus_name, object_id, service, icon_cache):
if not bus_name: if not bus_name:
raise ValueError("DBus bus name must be valid") raise ValueError("DBus bus name must be valid")
if not object_id or type(object_id) != type(1): if not object_id or not isinstance(object_id, int):
raise ValueError("object id must be a valid number") raise ValueError("object id must be a valid number")
# Normal Buddy objects must be created with a valid service, # Normal Buddy objects must be created with a valid service,
# owner objects do not # owner objects do not

View File

@ -31,7 +31,7 @@ class BuddyIconCache(object):
return data return data
def get_icon(self, printable_hash): def get_icon(self, printable_hash):
if type(printable_hash) != type(u""): if not isinstance(printable_hash, unicode):
raise RuntimeError("printable_hash must be a unicode string.") raise RuntimeError("printable_hash must be a unicode string.")
try: try:
fname = self._cache[printable_hash] fname = self._cache[printable_hash]

View File

@ -16,18 +16,18 @@ class ServiceAdv(object):
def __init__(self, interface, protocol, name, stype, domain, local): def __init__(self, interface, protocol, name, stype, domain, local):
self._interface = interface self._interface = interface
self._protocol = protocol self._protocol = protocol
if type(name) != type(u""): if not isinstance(name, unicode):
raise ValueError("service advertisement name must be unicode.") raise ValueError("service advertisement name must be unicode.")
self._name = name self._name = name
if type(stype) != type(u""): if not isinstance(stype, unicode):
raise ValueError("service advertisement type must be unicode.") raise ValueError("service advertisement type must be unicode.")
self._stype = stype self._stype = stype
if type(domain) != type(u""): if not isinstance(domain, unicode):
raise ValueError("service advertisement domain must be unicode.") raise ValueError("service advertisement domain must be unicode.")
self._domain = domain self._domain = domain
self._service = None self._service = None
if type(local) != type(False): if not isinstance(local, bool):
raise ValueError("local must be a boolean.") raise ValueError("local must be a bool.")
self._local = local self._local = local
self._state = _SA_UNRESOLVED self._state = _SA_UNRESOLVED
self._resolver = None self._resolver = None
@ -714,7 +714,7 @@ class PresenceService(object):
raise ValueError("invalid activity id") raise ValueError("invalid activity id")
owner_nick = self._owner.get_name() owner_nick = self._owner.get_name()
real_name = Service.compose_service_name(owner_nick, activity_id) real_name = Service.compose_service_name(owner_nick, activity_id)
if address and type(address) != type(u""): if address and isinstance(address, unicode):
raise ValueError("address must be a unicode string.") raise ValueError("address must be a unicode string.")
if address == None and stype.endswith('_udp'): if address == None and stype.endswith('_udp'):
# Use random currently unassigned multicast address # Use random currently unassigned multicast address
@ -722,7 +722,7 @@ class PresenceService(object):
random.randint(1, 254)) random.randint(1, 254))
properties['address'] = address properties['address'] = address
properties['port'] = port properties['port'] = port
if port and port != -1 and (type(port) != type(1) or port <= 1024 or port >= 65535): if port and port != -1 and (not isinstance(port, int) or port <= 1024 or port >= 65535):
raise ValueError("port must be a number between 1024 and 65535") raise ValueError("port must be a number between 1024 and 65535")
color = self._owner.get_color() color = self._owner.get_color()
@ -768,7 +768,7 @@ class PresenceService(object):
def register_service_type(self, stype): def register_service_type(self, stype):
"""Requests that the Presence service look for and recognize """Requests that the Presence service look for and recognize
a certain mDNS service types.""" a certain mDNS service types."""
if type(stype) != type(u""): if not isinstance(stype, unicode):
raise ValueError("service type must be a unicode string.") raise ValueError("service type must be a unicode string.")
# If we've already registered it as a service type, ref it and return # If we've already registered it as a service type, ref it and return
@ -795,7 +795,7 @@ class PresenceService(object):
def unregister_service_type(self, stype): def unregister_service_type(self, stype):
"""Stop tracking a certain mDNS service.""" """Stop tracking a certain mDNS service."""
if type(stype) != type(u""): if not isinstance(stype, unicode):
raise ValueError("service type must be a unicode string.") raise ValueError("service type must be a unicode string.")
# if it was found, unref it and possibly remove it # if it was found, unref it and possibly remove it

View File

@ -8,20 +8,20 @@ import logging
import gobject import gobject
def compose_service_name(name, activity_id): def compose_service_name(name, activity_id):
if type(name) == type(""): if isinstance(name, str):
name = unicode(name) name = unicode(name)
if not name: if not name:
raise ValueError("name must be a valid string.") raise ValueError("name must be a valid string.")
if not activity_id: if not activity_id:
return name return name
if type(name) != type(u""): if not isinstance(name, unicode):
raise ValueError("name must be in unicode.") raise ValueError("name must be in unicode.")
composed = "%s [%s]" % (name, activity_id) composed = "%s [%s]" % (name, activity_id)
return composed return composed
def decompose_service_name(name): def decompose_service_name(name):
"""Break a service name into the name and activity ID, if we can.""" """Break a service name into the name and activity ID, if we can."""
if type(name) != type(u""): if not isinstance(name, unicode):
raise ValueError("name must be a valid unicode string.") raise ValueError("name must be a valid unicode string.")
name_len = len(name) name_len = len(name)
if name_len < util.ACTIVITY_ID_LEN + 5: if name_len < util.ACTIVITY_ID_LEN + 5:
@ -151,23 +151,23 @@ class Service(gobject.GObject):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
if not bus_name: if not bus_name:
raise ValueError("DBus bus name must be valid") raise ValueError("DBus bus name must be valid")
if not object_id or type(object_id) != type(1): if not object_id or not isinstance(object_id, int):
raise ValueError("object id must be a valid number") raise ValueError("object id must be a valid number")
# Validate immutable options # Validate immutable options
if name and type(name) != type(u""): if name and not isinstance(name, unicode):
raise ValueError("name must be unicode.") raise ValueError("name must be unicode.")
if not name or not len(name): if not name or not len(name):
raise ValueError("must specify a valid service name.") raise ValueError("must specify a valid service name.")
if stype and type(stype) != type(u""): if stype and not isinstance(stype, unicode):
raise ValueError("service type must be in unicode.") raise ValueError("service type must be in unicode.")
if not stype or not len(stype): if not stype or not len(stype):
raise ValueError("must specify a valid service type.") raise ValueError("must specify a valid service type.")
if not stype.endswith("._tcp") and not stype.endswith("._udp"): if not stype.endswith("._tcp") and not stype.endswith("._udp"):
raise ValueError("must specify a TCP or UDP service type.") raise ValueError("must specify a TCP or UDP service type.")
if type(domain) != type(u""): if not isinstance(domain, unicode):
raise ValueError("domain must be in unicode.") raise ValueError("domain must be in unicode.")
if domain and domain != "local": if domain and domain != "local":
raise ValueError("must use the 'local' domain (for now).") raise ValueError("must use the 'local' domain (for now).")
@ -271,9 +271,9 @@ class Service(gobject.GObject):
if sender is not None and self._local_publisher != sender: if sender is not None and self._local_publisher != sender:
raise ValueError("Service was not not registered by requesting process!") raise ValueError("Service was not not registered by requesting process!")
if type(key) != type(u""): if not isinstance(key, unicode):
raise ValueError("Key must be a unicode string.") raise ValueError("Key must be a unicode string.")
if type(value) != type(u"") and type(value) != type(True): if not isinstance(value, unicode) and not isinstance(value, bool):
raise ValueError("Key must be a unicode string or a boolean.") raise ValueError("Key must be a unicode string or a boolean.")
# Ignore setting the key to it's current value # Ignore setting the key to it's current value
@ -283,9 +283,9 @@ class Service(gobject.GObject):
# Blank value means remove key # Blank value means remove key
remove = False remove = False
if type(value) == type(u"") and len(value) == 0: if isinstance(value, unicode) and len(value) == 0:
remove = True remove = True
if type(value) == type(False) and value == False: if isinstance(value, bool) and value == False:
remove = True remove = True
if remove: if remove:
@ -294,7 +294,7 @@ class Service(gobject.GObject):
del self._properties[key] del self._properties[key]
else: else:
# Otherwise set it # Otherwise set it
if type(value) == type(True): if isinstance(value, bool):
value = "" value = ""
self._properties[key] = value self._properties[key] = value
@ -330,9 +330,9 @@ class Service(gobject.GObject):
continue continue
tmp_key = key tmp_key = key
tmp_val = value tmp_val = value
if type(tmp_key) != type(u""): if not isinstance(tmp_key, unicode):
tmp_key = unicode(tmp_key) tmp_key = unicode(tmp_key)
if type(tmp_val) != type(u""): if not isinstance(tmp_val, unicode):
tmp_val = unicode(tmp_val) tmp_val = unicode(tmp_val)
self._properties[tmp_key] = tmp_val self._properties[tmp_key] = tmp_val
@ -361,7 +361,7 @@ class Service(gobject.GObject):
return self._port return self._port
def set_port(self, port): def set_port(self, port):
if type(port) != type(1) or (port <= 1024 and port > 65536): if not isinstance(port, int) or (port <= 1024 and port > 65536):
raise ValueError("must specify a valid port number between 1024 and 65536.") raise ValueError("must specify a valid port number between 1024 and 65536.")
self._port = port self._port = port
@ -369,7 +369,7 @@ class Service(gobject.GObject):
return self._source_address return self._source_address
def set_source_address(self, address): def set_source_address(self, address):
if not address or type(address) != type(u""): if not address or not isinstance(address, unicode):
raise ValueError("address must be unicode") raise ValueError("address must be unicode")
self._source_address = address self._source_address = address
@ -377,7 +377,7 @@ class Service(gobject.GObject):
return self._address return self._address
def set_address(self, address): def set_address(self, address):
if not address or type(address) != type(u""): if not address or not isinstance(address, unicode):
raise ValueError("address must be a unicode string") raise ValueError("address must be a unicode string")
self._address = address self._address = address
self._properties['address'] = address self._properties['address'] = address

View File

@ -374,7 +374,7 @@ class SVGelement:
f.write('\n'+'\t'*(level+2)+line) f.write('\n'+'\t'*(level+2)+line)
f.write('\n'+'\t'*(level+1)+']]>\n') f.write('\n'+'\t'*(level+1)+']]>\n')
if self.text: if self.text:
if type(self.text)==type(''): #If the text is only text if isinstance(self.text, str): #If the text is only text
f.write(_escape(str(self.text))) f.write(_escape(str(self.text)))
else: #If the text is a spannedtext class else: #If the text is a spannedtext class
f.write(str(self.text)) f.write(str(self.text))

View File

@ -86,21 +86,21 @@ class SegmentBase(object):
self._addr = None self._addr = None
# Sanity checks on the message attributes # Sanity checks on the message attributes
if not segno or type(segno) != type(1): if not segno or not isinstance(segno, int):
raise ValueError("Segment number must be in integer.") raise ValueError("Segment number must be in integer.")
if segno < 1 or segno > 65535: if segno < 1 or segno > 65535:
raise ValueError("Segment number must be between 1 and 65535 inclusive.") raise ValueError("Segment number must be between 1 and 65535 inclusive.")
if not total_segs or type(total_segs) != type(1): if not total_segs or not isinstance(total_segs, int):
raise ValueError("Message segment total must be an integer.") raise ValueError("Message segment total must be an integer.")
if total_segs < 1 or total_segs > 65535: if total_segs < 1 or total_segs > 65535:
raise ValueError("Message must have between 1 and 65535 segments inclusive.") raise ValueError("Message must have between 1 and 65535 segments inclusive.")
if segno > total_segs: if segno > total_segs:
raise ValueError("Segment number cannot be larger than message segment total.") raise ValueError("Segment number cannot be larger than message segment total.")
if not msg_seq_num or type(msg_seq_num) != type(1): if not msg_seq_num or not isinstance(msg_seq_num, int):
raise ValueError("Message sequnce number must be an integer.") raise ValueError("Message sequnce number must be an integer.")
if msg_seq_num < 1 or msg_seq_num > 65535: if msg_seq_num < 1 or msg_seq_num > 65535:
raise ValueError("Message sequence number must be between 1 and 65535 inclusive.") raise ValueError("Message sequence number must be between 1 and 65535 inclusive.")
if not master_sha or type(master_sha) != type("") or len(master_sha) != 20: if not master_sha or not isinstance(master_sha, str) or len(master_sha) != 20:
raise ValueError("Message SHA1 checksum invalid.") raise ValueError("Message SHA1 checksum invalid.")
self._segno = segno self._segno = segno
@ -109,9 +109,9 @@ class SegmentBase(object):
self._master_sha = master_sha self._master_sha = master_sha
def _validate_address(addr): def _validate_address(addr):
if not addr or type(addr) != type(()): if not addr or not isinstance(addr, tuple):
raise ValueError("Address must be a tuple.") raise ValueError("Address must be a tuple.")
if len(addr) != 2 or type(addr[0]) != type("") or type(addr[1]) != type(1): if len(addr) != 2 or not isinstance(addr[0], str) or not isinstance(addr[1], int):
raise ValueError("Address format was invalid.") raise ValueError("Address format was invalid.")
if addr[1] < 1 or addr[1] > 65535: if addr[1] < 1 or addr[1] > 65535:
raise ValueError("Address port was invalid.") raise ValueError("Address port was invalid.")
@ -265,15 +265,15 @@ class RetransmitSegment(SegmentBase):
def _verify_data(rt_msg_seq_num, rt_master_sha, rt_segment_number): def _verify_data(rt_msg_seq_num, rt_master_sha, rt_segment_number):
# Sanity checks on the message attributes # Sanity checks on the message attributes
if not rt_segment_number or type(rt_segment_number) != type(1): if not rt_segment_number or not isinstance(rt_segment_number, int):
raise ValueError("RT Segment number must be in integer.") raise ValueError("RT Segment number must be in integer.")
if rt_segment_number < 1 or rt_segment_number > 65535: if rt_segment_number < 1 or rt_segment_number > 65535:
raise ValueError("RT Segment number must be between 1 and 65535 inclusive.") raise ValueError("RT Segment number must be between 1 and 65535 inclusive.")
if not rt_msg_seq_num or type(rt_msg_seq_num) != type(1): if not rt_msg_seq_num or not isinstance(rt_msg_seq_num, int):
raise ValueError("RT Message sequnce number must be an integer.") raise ValueError("RT Message sequnce number must be an integer.")
if rt_msg_seq_num < 1 or rt_msg_seq_num > 65535: if rt_msg_seq_num < 1 or rt_msg_seq_num > 65535:
raise ValueError("RT Message sequence number must be between 1 and 65535 inclusive.") raise ValueError("RT Message sequence number must be between 1 and 65535 inclusive.")
if not rt_master_sha or type(rt_master_sha) != type("") or len(rt_master_sha) != 20: if not rt_master_sha or not isinstance(rt_master_sha, str) or len(rt_master_sha) != 20:
raise ValueError("RT Message SHA1 checksum invalid.") raise ValueError("RT Message SHA1 checksum invalid.")
_verify_data = staticmethod(_verify_data) _verify_data = staticmethod(_verify_data)
@ -349,13 +349,13 @@ class AckSegment(SegmentBase):
def _verify_data(ack_msg_seq_num, ack_master_sha, ack_addr): def _verify_data(ack_msg_seq_num, ack_master_sha, ack_addr):
# Sanity checks on the message attributes # Sanity checks on the message attributes
if not ack_msg_seq_num or type(ack_msg_seq_num) != type(1): if not ack_msg_seq_num or not isinstance(ack_msg_seq_num, int):
raise ValueError("Ack message sequnce number must be an integer.") raise ValueError("Ack message sequnce number must be an integer.")
if ack_msg_seq_num < 1 or ack_msg_seq_num > 65535: if ack_msg_seq_num < 1 or ack_msg_seq_num > 65535:
raise ValueError("Ack message sequence number must be between 1 and 65535 inclusive.") raise ValueError("Ack message sequence number must be between 1 and 65535 inclusive.")
if not ack_master_sha or type(ack_master_sha) != type("") or len(ack_master_sha) != 20: if not ack_master_sha or not isinstance(ack_master_sha, str) or len(ack_master_sha) != 20:
raise ValueError("Ack message SHA1 checksum invalid.") raise ValueError("Ack message SHA1 checksum invalid.")
if type(ack_addr) != type(""): if not isinstance(ack_addr, str):
raise ValueError("Ack message invalid address type.") raise ValueError("Ack message invalid address type.")
try: try:
foo = socket.inet_aton(ack_addr) foo = socket.inet_aton(ack_addr)
@ -865,7 +865,7 @@ class MostlyReliablePipe(object):
"""Debugging function to randomly drop incoming packets. """Debugging function to randomly drop incoming packets.
The prob argument should be an integer between 1 and 10 to drop, The prob argument should be an integer between 1 and 10 to drop,
or 0 to drop none. Higher numbers drop more packets.""" or 0 to drop none. Higher numbers drop more packets."""
if type(prob) != type(1): if not isinstance(prob, int):
raise ValueError("Drop probability must be an integer.") raise ValueError("Drop probability must be an integer.")
if prob < 1 or prob > 10: if prob < 1 or prob > 10:
raise ValueError("Drop probability must be between 1 and 10 inclusive.") raise ValueError("Drop probability must be between 1 and 10 inclusive.")

View File

@ -43,7 +43,7 @@ def is_hex(s):
def validate_activity_id(actid): def validate_activity_id(actid):
"""Validate an activity ID.""" """Validate an activity ID."""
if type(actid) != type("") and type(actid) != type(u""): if not isinstance(actid, str) and not isinstance(actid, unicode):
return False return False
if len(actid) != ACTIVITY_ID_LEN: if len(actid) != ACTIVITY_ID_LEN:
return False return False