From fd54e68e6d6a7567b6d0b7ca44e5841a210ed019 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Sat, 11 Oct 2008 10:06:49 +0000 Subject: [PATCH] [thorstenp] some fixes with type() -> isinstance() --- src/common/xmpp/client.py | 4 ++-- src/common/xmpp/debug.py | 6 +++--- src/common/xmpp/dispatcher.py | 2 +- src/common/xmpp/dispatcher_nb.py | 2 +- src/common/xmpp/features.py | 4 ++-- src/common/xmpp/protocol.py | 10 +++++----- src/common/xmpp/simplexml.py | 4 ++-- src/common/xmpp/transports.py | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/common/xmpp/client.py b/src/common/xmpp/client.py index a34901034..4a1c8b7e8 100644 --- a/src/common/xmpp/client.py +++ b/src/common/xmpp/client.py @@ -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 @@ -321,4 +321,4 @@ class Component(CommonClient): except Exception: self.DEBUG(self.DBG,"Failed to authenticate %s"%name,'error') -# vim: se ts=3: \ No newline at end of file +# vim: se ts=3: diff --git a/src/common/xmpp/debug.py b/src/common/xmpp/debug.py index edf91805a..3450e3966 100644 --- a/src/common/xmpp/debug.py +++ b/src/common/xmpp/debug.py @@ -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: diff --git a/src/common/xmpp/dispatcher.py b/src/common/xmpp/dispatcher.py index a54f6101b..f88493ff1 100644 --- a/src/common/xmpp/dispatcher.py +++ b/src/common/xmpp/dispatcher.py @@ -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 diff --git a/src/common/xmpp/dispatcher_nb.py b/src/common/xmpp/dispatcher_nb.py index 84986c22b..a17e6aa26 100644 --- a/src/common/xmpp/dispatcher_nb.py +++ b/src/common/xmpp/dispatcher_nb.py @@ -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 diff --git a/src/common/xmpp/features.py b/src/common/xmpp/features.py index b3e8d4080..7ca7b4f18 100644 --- a/src/common/xmpp/features.py +++ b/src/common/xmpp/features.py @@ -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 @@ -183,4 +183,4 @@ def delPrivacyList(disp,listname): resp=disp.SendAndWaitForResponse(Iq('set',NS_PRIVACY,payload=[Node('list',{'name':listname})])) if isResultNode(resp): return 1 -# vim: se ts=3: \ No newline at end of file +# vim: se ts=3: diff --git a/src/common/xmpp/protocol.py b/src/common/xmpp/protocol.py index 853efd3e6..356d22a75 100644 --- a/src/common/xmpp/protocol.py +++ b/src/common/xmpp/protocol.py @@ -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') diff --git a/src/common/xmpp/simplexml.py b/src/common/xmpp/simplexml.py index 5a78ace59..46b386c8a 100644 --- a/src/common/xmpp/simplexml.py +++ b/src/common/xmpp/simplexml.py @@ -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): diff --git a/src/common/xmpp/transports.py b/src/common/xmpp/transports.py index dacc58483..738e12f8c 100644 --- a/src/common/xmpp/transports.py +++ b/src/common/xmpp/transports.py @@ -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.