From 3392c54dd0d775f5f0c2b79f6db3388a64011e38 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger <asterix@lagaule.org> Date: Wed, 3 Dec 2008 21:37:05 +0000 Subject: [PATCH] [thorstenp] fix empty exception clauses --- src/common/connection_handlers.py | 2 +- src/common/rst_xhtml_generator.py | 2 +- src/common/xmpp/auth.py | 6 ++++-- src/common/xmpp/features.py | 6 ++++-- src/common/xmpp/filetransfer.py | 14 +++++++++---- src/common/xmpp/protocol.py | 24 +++++++++++++++-------- src/common/xmpp/session.py | 6 ++++-- src/common/xmpp/simplexml.py | 27 ++++++++++++++++---------- src/common/xmpp/transports.py | 18 +++++++++++------ src/common/xmpp/transports_nb.py | 18 +++++++++++------ src/common/zeroconf/client_zeroconf.py | 6 +++--- src/config.py | 2 +- src/gtkgui_helpers.py | 2 +- src/htmltextview.py | 3 ++- 14 files changed, 88 insertions(+), 48 deletions(-) diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index 5c97ba87a5..24d3e4072c 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -2333,7 +2333,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, for jid in raw_roster: try: j = helpers.parse_jid(jid) - except: + except Exception: print >> sys.stderr, _('JID %s is not RFC compliant. It will not be added to your roster. Use roster management tools such as http://jru.jabberstudio.org/ to remove it') % jid else: infos = raw_roster[jid] diff --git a/src/common/rst_xhtml_generator.py b/src/common/rst_xhtml_generator.py index 315b011adb..c65387d152 100644 --- a/src/common/rst_xhtml_generator.py +++ b/src/common/rst_xhtml_generator.py @@ -27,7 +27,7 @@ try: from docutils.parsers.rst import roles from docutils import nodes,utils from docutils.parsers.rst.roles import set_classes -except: +except ImportError: print "Requires docutils 0.4 for set_classes to be available" def create_xhtml(text): return None diff --git a/src/common/xmpp/auth.py b/src/common/xmpp/auth.py index 1c7721cb88..733346123d 100644 --- a/src/common/xmpp/auth.py +++ b/src/common/xmpp/auth.py @@ -161,8 +161,10 @@ class SASL(PlugIn): if challenge.getNamespace()!=NS_SASL: return if challenge.getName()=='failure': self.startsasl='failure' - try: reason=challenge.getChildren()[0] - except: reason=challenge + try: + reason=challenge.getChildren()[0] + except Exception: + reason=challenge self.DEBUG('Failed SASL authentification: %s'%reason,'error') raise NodeProcessed elif challenge.getName()=='success': diff --git a/src/common/xmpp/features.py b/src/common/xmpp/features.py index 277e7c8d38..4cee5d6f6e 100644 --- a/src/common/xmpp/features.py +++ b/src/common/xmpp/features.py @@ -149,7 +149,8 @@ def getPrivacyLists(disp): if list_.getName()=='list': dict_['lists'].append(list_.getAttr('name')) else: dict_[list_.getName()]=list_.getAttr('name') return dict_ - except: pass + except Exception: + pass def getPrivacyList(disp,listname): """ Requests specific privacy list listname. Returns list of XML nodes (rules) @@ -157,7 +158,8 @@ def getPrivacyList(disp,listname): try: resp=disp.SendAndWaitForResponse(Iq('get',NS_PRIVACY,payload=[Node('list',{'name':listname})])) if isResultNode(resp): return resp.getQueryPayload()[0] - except: pass + except Exception: + pass def setActivePrivacyList(disp,listname=None,typ='active'): """ Switches privacy list 'listname' to specified type. diff --git a/src/common/xmpp/filetransfer.py b/src/common/xmpp/filetransfer.py index 6624a11437..6afefe3e34 100644 --- a/src/common/xmpp/filetransfer.py +++ b/src/common/xmpp/filetransfer.py @@ -69,8 +69,10 @@ class IBB(PlugIn): err=None sid,blocksize=stanza.getTagAttr('open','sid'),stanza.getTagAttr('open','block-size') self.DEBUG('StreamOpenHandler called sid->%s blocksize->%s'%(sid,blocksize),'info') - try: blocksize=int(blocksize) - except: err=ERR_BAD_REQUEST + try: + blocksize=int(blocksize) + except Exception: + err=ERR_BAD_REQUEST if not sid or not blocksize: err=ERR_BAD_REQUEST elif sid in self._streams.keys(): err=ERR_UNEXPECTED_REQUEST if err: rep=Error(stanza,err) @@ -137,8 +139,12 @@ class IBB(PlugIn): """ sid,seq,data=stanza.getTagAttr('data','sid'),stanza.getTagAttr('data','seq'),stanza.getTagData('data') self.DEBUG('ReceiveHandler called sid->%s seq->%s'%(sid,seq),'info') - try: seq=int(seq); data=base64.decodestring(data) - except: seq=''; data='' + try: + seq=int(seq) + data=base64.decodestring(data) + except Exception: + seq='' + data='' err=None if not sid in self._streams.keys(): err=ERR_ITEM_NOT_FOUND else: diff --git a/src/common/xmpp/protocol.py b/src/common/xmpp/protocol.py index 5f553941b5..eb9c3136f2 100644 --- a/src/common/xmpp/protocol.py +++ b/src/common/xmpp/protocol.py @@ -326,22 +326,30 @@ class Protocol(Node): self.timestamp=None for d in self.getTags('delay',namespace=NS_DELAY2): try: - if d.getAttr('stamp')<self.getTimestamp2(): self.setTimestamp(d.getAttr('stamp')) - except: pass + if d.getAttr('stamp') < self.getTimestamp2(): + self.setTimestamp(d.getAttr('stamp')) + except Exception: + pass if not self.timestamp: for x in self.getTags('x',namespace=NS_DELAY): try: - if x.getAttr('stamp')<self.getTimestamp(): self.setTimestamp(x.getAttr('stamp')) - except: pass + if x.getAttr('stamp') < self.getTimestamp(): + self.setTimestamp(x.getAttr('stamp')) + except Exception: + pass if timestamp is not None: self.setTimestamp(timestamp) # To auto-timestamp stanza just pass timestamp='' def getTo(self): """ Return value of the 'to' attribute. """ - try: return self['to'] - except: return None + try: + return self['to'] + except KeyError: + return None def getFrom(self): """ Return value of the 'from' attribute. """ - try: return self['from'] - except: return None + try: + return self['from'] + except KeyError: + return None def getTimestamp(self): """ Return the timestamp in the 'yyyymmddThhmmss' format. """ if self.timestamp: return self.timestamp diff --git a/src/common/xmpp/session.py b/src/common/xmpp/session.py index 0e77020787..aa19d1d62a 100644 --- a/src/common/xmpp/session.py +++ b/src/common/xmpp/session.py @@ -127,8 +127,10 @@ class Session: """ Reads all pending incoming data. Raises IOError on disconnection. Blocks until at least one byte is read.""" - try: received = self._recv(10240) - except: received = '' + try: + received = self._recv(10240) + except socket.error: + received = '' if len(received): # length of 0 means disconnect self.DEBUG(repr(self.fileno())+' '+received,'got') diff --git a/src/common/xmpp/simplexml.py b/src/common/xmpp/simplexml.py index fc735ffab5..7e10185f67 100644 --- a/src/common/xmpp/simplexml.py +++ b/src/common/xmpp/simplexml.py @@ -168,8 +168,7 @@ class Node(object): return self.attrs def getAttr(self, key): """ Returns value of specified attribute. """ - try: return self.attrs[key] - except: return None + return self.attrs.get(key) def getChildren(self): """ Returns all node's child nodes as list. """ return self.kids @@ -203,12 +202,16 @@ class Node(object): return self.getTags(name, attrs, namespace, one=1) def getTagAttr(self,tag,attr): """ Returns attribute value of the child with specified name (or None if no such attribute).""" - try: return self.getTag(tag).attrs[attr] - except: return None + try: + return self.getTag(tag).attrs[attr] + except: + return None def getTagData(self,tag): """ Returns cocatenated CDATA of the child with specified name.""" - try: return self.getTag(tag).getData() - except: return None + try: + return self.getTag(tag).getData() + except Exception: + return None def getTags(self, name, attrs={}, namespace=None, one=0): """ Filters all child nodes using specified arguments as filter. Returns the list of nodes found. """ @@ -264,13 +267,17 @@ class Node(object): def setTagAttr(self,tag,attr,val): """ Creates new node (if not already present) with name "tag" and sets it's attribute "attr" to value "val". """ - try: self.getTag(tag).attrs[attr]=val - except: self.addChild(tag,attrs={attr:val}) + try: + self.getTag(tag).attrs[attr]=val + except Exception: + self.addChild(tag,attrs={attr:val}) def setTagData(self,tag,val,attrs={}): """ Creates new node (if not already present) with name "tag" and (optionally) attributes "attrs" and sets it's CDATA to string "val". """ - try: self.getTag(tag,attrs).setData(ustr(val)) - except: self.addChild(tag,attrs,payload=[ustr(val)]) + try: + self.getTag(tag,attrs).setData(ustr(val)) + except Exception: + self.addChild(tag,attrs,payload=[ustr(val)]) def has_attr(self,key): """ Checks if node have attribute "key".""" return key in self.attrs diff --git a/src/common/xmpp/transports.py b/src/common/xmpp/transports.py index ad7e68f0f6..9fd83884ad 100644 --- a/src/common/xmpp/transports.py +++ b/src/common/xmpp/transports.py @@ -100,8 +100,10 @@ class TCPsocket(PlugIn): self._recv=self._sock.recv self.DEBUG("Successfully connected to remote host %s"%repr(server),'start') return 'ok' - except: continue - except: pass + except Exception: + continue + except Exception: + pass def plugout(self): """ Disconnect from the remote server and unregister self.disconnected method from @@ -112,12 +114,16 @@ class TCPsocket(PlugIn): def receive(self): """ Reads all pending incoming data. Calls owner's disconnected() method if appropriate.""" - try: received = self._recv(1024000) - except: received = '' + try: + received = self._recv(1024000) + except socket.error: + received = '' while temp_failure_retry(select.select,[self._sock],[],[],0)[0]: - try: add = self._recv(1024000) - except: add='' + try: + add = self._recv(1024000) + except socket.error: + add='' received +=add if not add: break diff --git a/src/common/xmpp/transports_nb.py b/src/common/xmpp/transports_nb.py index df456c11ec..5414fc956f 100644 --- a/src/common/xmpp/transports_nb.py +++ b/src/common/xmpp/transports_nb.py @@ -102,7 +102,8 @@ class SSLWrapper: if self.exc_args[0] > 0: errno = self.exc_args[0] strerror = self.exc_args[1] - except: pass + except Exception: + pass self.parent.__init__(self, errno, strerror) @@ -112,7 +113,8 @@ class SSLWrapper: if len(ppeer) == 2 and isinstance(ppeer[0], basestring) \ and isinstance(ppeer[1], int): self.peer = ppeer - except: pass + except Exception: + pass def __str__(self): s = str(self.__class__) @@ -356,8 +358,10 @@ class NonBlockingTcp(PlugIn, IdleObject): def pollend(self, retry=False): if not self.printed_error: self.printed_error = True - try: self._do_receive(errors_only=True) - except: log.error("pollend: Got exception from _do_receive:", exc_info=True) + try: + self._do_receive(errors_only=True) + except Exception: + log.error("pollend: Got exception from _do_receive:", exc_info=True) conn_failure_cb = self.on_connect_failure self.disconnect() if conn_failure_cb: @@ -381,8 +385,10 @@ class NonBlockingTcp(PlugIn, IdleObject): except socket.error, e: if e[0] != errno.ENOTCONN: log.error("Error shutting down socket for %s:", self.getName(), exc_info=True) - try: sock.close() - except: log.error("Error closing socket for %s:", self.getName(), exc_info=True) + try: + sock.close() + except Exception: + log.error("Error closing socket for %s:", self.getName(), exc_info=True) # socket descriptor cannot be (un)plugged anymore self.fd = -1 if self.on_disconnect: diff --git a/src/common/zeroconf/client_zeroconf.py b/src/common/zeroconf/client_zeroconf.py index 0b842e239e..6952636c50 100644 --- a/src/common/zeroconf/client_zeroconf.py +++ b/src/common/zeroconf/client_zeroconf.py @@ -105,7 +105,7 @@ class ZeroconfListener(IdleObject): self.started = False try: self._serv.close() - except: + except socket.error: pass self.conn_holder.kill_all_connections() @@ -343,7 +343,7 @@ class P2PConnection(IdleObject, PlugIn): self._sock = socket.socket(*ai[:3]) self._sock.setblocking(False) self._server = ai[4] - except: + except socket.error: if sys.exc_value[0] != errno.EINPROGRESS: # for all errors, we try other addresses self.connect_to_next_ip() @@ -491,7 +491,7 @@ class P2PConnection(IdleObject, PlugIn): try: self._sock.shutdown(socket.SHUT_RDWR) self._sock.close() - except: + except socket.error: # socket is already closed pass self.fd = -1 diff --git a/src/config.py b/src/config.py index c072c74198..494b573d86 100644 --- a/src/config.py +++ b/src/config.py @@ -640,7 +640,7 @@ class PreferencesWindow: tv = gtk.TextView() try: gtkspell.Spell(tv, lang) - except: + except Exception: dialogs.ErrorDialog( _('Dictionary for lang %s not available') % lang, _('You have to install %s dictionary to use spellchecking, or ' diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py index 6fadb53b1c..d86014bdc5 100644 --- a/src/gtkgui_helpers.py +++ b/src/gtkgui_helpers.py @@ -795,7 +795,7 @@ default_name = ''): # Save image try: pixbuf.save(file_path, type_) - except: + except Exception: if os.path.exists(file_path): os.remove(file_path) new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg' diff --git a/src/htmltextview.py b/src/htmltextview.py index b6395b2ae1..ce7f72db58 100644 --- a/src/htmltextview.py +++ b/src/htmltextview.py @@ -1046,7 +1046,8 @@ if __name__ == '__main__': change_cursor = tag elif tag == tag_table.lookup('focus-out-line'): over_line = True - except: pass + except Exception: + pass #if line_tooltip.timeout != 0: # Check if we should hide the line tooltip -- GitLab