[thorstenp] some fixes with type() -> isinstance()

This commit is contained in:
Yann Leboulanger 2008-10-11 10:06:49 +00:00
parent 992e90208e
commit fd54e68e6d
8 changed files with 17 additions and 17 deletions

View File

@ -104,7 +104,7 @@ class CommonClient:
# Who initiated this client
# Used to register the EventDispatcher
self._caller=caller
if debug and type(debug)!=list: debug=['always', 'nodebuilder']
if debug and not isinstance(debug, list): debug=['always', 'nodebuilder']
self._DEBUG=Debug.Debug(debug)
self.DEBUG=self._DEBUG.Show
self.debug_flags=self._DEBUG.debug_flags

View File

@ -202,7 +202,7 @@ class Debug:
mod_name ))
self.show(' flags defined: %s' % ','.join( self.active ))
if type(flag_show) in (type(''), type(None)):
if type(flag_show) in (str, type(None)):
self.flag_show = flag_show
else:
msg2 = '%s' % type(flag_show )
@ -329,7 +329,7 @@ class Debug:
This code organises lst and remves dupes
"""
if type( items ) != type( [] ) and type( items ) != type( () ):
if not isinstance(items, (list, tuple)):
return [ items ]
r = []
for l in items:
@ -346,7 +346,7 @@ class Debug:
def _append_unique_str( self, lst, item ):
"""filter out any dupes."""
if type(item) != type(''):
if not isinstance(item, str):
msg2 = '%s' % item
raise 'Invalid item type (should be string)',msg2
if item not in lst:

View File

@ -354,7 +354,7 @@ class Dispatcher(PlugIn):
def send(self,stanza):
""" Serialise stanza and put it on the wire. Assign an unique ID to it before send.
Returns assigned ID."""
if type(stanza) in [type(''), type(u'')]: return self._owner_send(stanza)
if isinstance(stanza, basestring): return self._owner_send(stanza)
if not isinstance(stanza,Protocol): _ID=None
elif not stanza.getID():
global ID

View File

@ -404,7 +404,7 @@ class Dispatcher(PlugIn):
def send(self, stanza, is_message = False, now = False):
''' Serialise stanza and put it on the wire. Assign an unique ID to it before send.
Returns assigned ID.'''
if type(stanza) in (type(''), type(u'')):
if isinstance(stanza, basestring):
return self._owner.Connection.send(stanza, now = now)
if not isinstance(stanza, Protocol):
_ID=None

View File

@ -114,7 +114,7 @@ def register(disp,host,info):
attributes lastErrNode, lastErr and lastErrCode.
"""
iq=Iq('set',NS_REGISTER,to=host)
if type(info)!=type({}): info=info.asDict()
if not isinstance(info, dict): info=info.asDict()
for i in info.keys(): iq.setTag('query').setTagData(i,info[i])
resp=disp.SendAndWaitForResponse(iq)
if isResultNode(resp): return 1

View File

@ -389,7 +389,7 @@ class Protocol(Node):
if code:
if str(code) in _errorcodes.keys(): error=ErrorNode(_errorcodes[str(code)],text=error)
else: error=ErrorNode(ERR_UNDEFINED_CONDITION,code=code,typ='cancel',text=error)
elif type(error) in [type(''),type(u'')]: error=ErrorNode(error)
elif isinstance(error, basestring): error=ErrorNode(error)
self.setType('error')
self.addChild(node=error)
def setTimestamp(self,val=None):
@ -638,7 +638,7 @@ class DataField(Node):
"""
Node.__init__(self,'field',node=node)
if name: self.setVar(name)
if type(value) in [list,tuple]: self.setValues(value)
if isinstance(value, (list, tuple)): self.setValues(value)
elif value: self.setValue(value)
if typ: self.setType(typ)
elif not typ and not node: self.setType('text-single')
@ -689,7 +689,7 @@ class DataField(Node):
for opt in lst: self.addOption(opt)
def addOption(self,opt):
""" Add one more label-option pair to this field."""
if type(opt) in [str,unicode]: self.addChild('option').setTagData('value',opt)
if isinstance(opt, basestring): self.addChild('option').setTagData('value',opt)
else: self.addChild('option',{'label':opt[0]}).setTagData('value',opt[1])
def getType(self):
""" Get type of this field. """
@ -737,7 +737,7 @@ class DataForm(Node):
for name in data.keys(): newdata.append(DataField(name,data[name]))
data=newdata
for child in data:
if type(child) in [type(''),type(u'')]: self.addInstructions(child)
if isinstance(child, basestring): self.addInstructions(child)
elif child.__class__.__name__=='DataField': self.kids.append(child)
else: self.kids.append(DataField(node=child))
def getType(self):
@ -775,7 +775,7 @@ class DataForm(Node):
for field in self.getTags('field'):
name=field.getAttr('var')
typ=field.getType()
if type(typ) in [type(''),type(u'')] and typ.endswith('-multi'):
if isinstance(typ, basestring) and typ.endswith('-multi'):
val=[]
for i in field.getTags('value'): val.append(i.getData())
else: val=field.getTagData('value')

View File

@ -30,7 +30,7 @@ def ustr(what):
if isinstance(what, unicode): return what
try: r=what.__str__()
except AttributeError: r=str(what)
if type(r)!=type(u''): return unicode(r,ENCODING)
if not isinstance(r, unicode): return unicode(r,ENCODING)
return r
class Node(object):
@ -252,7 +252,7 @@ class Node(object):
def setPayload(self,payload,add=0):
""" Sets node payload according to the list specified. WARNING: completely replaces all node's
previous content. If you wish just to add child or CDATA - use addData or addChild methods. """
if type(payload) in (type(''),type(u'')): payload=[payload]
if isinstance(payload, basestring): payload=[payload]
if add: self.kids+=payload
else: self.kids=payload
def setTag(self, name, attrs={}, namespace=None):

View File

@ -139,7 +139,7 @@ class TCPsocket(PlugIn):
""" Writes raw outgoing data. Blocks until done.
If supplied data is unicode string, encodes it to utf-8 before send."""
if isinstance(raw_data, unicode): raw_data = raw_data.encode('utf-8')
elif type(raw_data)!=type(str): raw_data = ustr(raw_data).encode('utf-8')
elif not isinstance(raw_data, str): raw_data = ustr(raw_data).encode('utf-8')
try:
self._send(raw_data)
# Avoid printing messages that are empty keepalive packets.