changeset 16688:cfb6682961b8

cleanup: replace naked excepts with more specific ones
author Brodie Rao <brodie@sf.io>
date Sat, 12 May 2012 16:02:45 +0200
parents e34106fa0dc3
children f366d4c2ff34
files contrib/setup3k.py hgext/convert/common.py hgext/convert/cvsps.py hgext/convert/monotone.py hgext/convert/subversion.py hgext/gpg.py hgext/hgcia.py hgext/inotify/server.py hgext/mq.py hgext/patchbomb.py hgext/zeroconf/__init__.py mercurial/keepalive.py mercurial/sshrepo.py mercurial/util.py setup.py tests/hghave tests/run-tests.py tests/test-check-code-hg.t tests/test-filecache.py
diffstat 19 files changed, 38 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/setup3k.py	Sat May 12 16:00:58 2012 +0200
+++ b/contrib/setup3k.py	Sat May 12 16:02:45 2012 +0200
@@ -26,22 +26,22 @@
 try:
     import hashlib
     sha = hashlib.sha1()
-except:
+except ImportError:
     try:
         import sha
-    except:
+    except ImportError:
         raise SystemExit(
             "Couldn't import standard hashlib (incomplete Python install).")
 
 try:
     import zlib
-except:
+except ImportError:
     raise SystemExit(
         "Couldn't import standard zlib (incomplete Python install).")
 
 try:
     import bz2
-except:
+except ImportError:
     raise SystemExit(
         "Couldn't import standard bz2 (incomplete Python install).")
 
--- a/hgext/convert/common.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/convert/common.py	Sat May 12 16:02:45 2012 +0200
@@ -116,10 +116,10 @@
             return s.encode("utf-8")
         try:
             return s.decode(encoding).encode("utf-8")
-        except:
+        except UnicodeError:
             try:
                 return s.decode("latin-1").encode("utf-8")
-            except:
+            except UnicodeError:
                 return s.decode(encoding, "replace").encode("utf-8")
 
     def getchangedfiles(self, rev, i):
@@ -333,7 +333,7 @@
         argmax = 4096
         try:
             argmax = os.sysconf("SC_ARG_MAX")
-        except:
+        except (AttributeError, ValueError):
             pass
 
         # Windows shells impose their own limits on command line length,
--- a/hgext/convert/cvsps.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/convert/cvsps.py	Sat May 12 16:02:45 2012 +0200
@@ -706,11 +706,11 @@
         if mergeto:
             m = mergeto.search(c.comment)
             if m:
-                try:
+                if m.groups():
                     m = m.group(1)
                     if m == 'HEAD':
                         m = None
-                except:
+                else:
                     m = None   # if no group found then merge to HEAD
                 if m in branches and c.branch != m:
                     # insert empty changeset for merge
--- a/hgext/convert/monotone.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/convert/monotone.py	Sat May 12 16:02:45 2012 +0200
@@ -30,7 +30,7 @@
                 f = file(path, 'rb')
                 header = f.read(16)
                 f.close()
-            except:
+            except IOError:
                 header = ''
             if header != 'SQLite format 3\x00':
                 raise norepo
--- a/hgext/convert/subversion.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/convert/subversion.py	Sat May 12 16:02:45 2012 +0200
@@ -139,7 +139,7 @@
                                    ' hg executable is in PATH'))
             try:
                 orig_paths, revnum, author, date, message = entry
-            except:
+            except (TypeError, ValueError):
                 if entry is None:
                     break
                 raise util.Abort(_("log stream exception '%s'") % entry)
--- a/hgext/gpg.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/gpg.py	Sat May 12 16:02:45 2012 +0200
@@ -43,7 +43,7 @@
                 try:
                     if f:
                         os.unlink(f)
-                except:
+                except OSError:
                     pass
         keys = []
         key, fingerprint = None, None
--- a/hgext/hgcia.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/hgcia.py	Sat May 12 16:02:45 2012 +0200
@@ -46,17 +46,14 @@
 from mercurial import cmdutil, patch, templater, util, mail
 import email.Parser
 
-import xmlrpclib
+import socket, xmlrpclib
 from xml.sax import saxutils
 
 socket_timeout = 30 # seconds
-try:
+if util.safehasattr(socket, 'setdefaulttimeout'):
     # set a timeout for the socket so you don't have to wait so looooong
     # when cia.vc is having problems. requires python >= 2.3:
-    import socket
     socket.setdefaulttimeout(socket_timeout)
-except:
-    pass
 
 HGCIA_VERSION = '0.1'
 HGCIA_URL = 'http://hg.kublai.com/mercurial/hgcia'
--- a/hgext/inotify/server.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/inotify/server.py	Sat May 12 16:02:45 2012 +0200
@@ -355,7 +355,7 @@
                 except (OSError, socket.error), inst:
                     try:
                         os.unlink(self.realsockpath)
-                    except:
+                    except OSError:
                         pass
                     os.rmdir(tempdir)
                     if inst.errno == errno.EEXIST:
@@ -416,7 +416,7 @@
                 # try to send back our version to the client
                 # this way, the client too is informed of the mismatch
                 sock.sendall(chr(common.version))
-            except:
+            except socket.error:
                 pass
             return
 
--- a/hgext/mq.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/mq.py	Sat May 12 16:02:45 2012 +0200
@@ -1084,7 +1084,7 @@
                 patchpath = self.join(patchfn)
                 try:
                     os.unlink(patchpath)
-                except:
+                except OSError:
                     self.ui.warn(_('error unlinking %s\n') % patchpath)
                 raise
             self.removeundo(repo)
--- a/hgext/patchbomb.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/patchbomb.py	Sat May 12 16:02:45 2012 +0200
@@ -304,7 +304,7 @@
         finally:
             try:
                 os.unlink(tmpfn)
-            except:
+            except OSError:
                 pass
             os.rmdir(tmpdir)
 
--- a/hgext/zeroconf/__init__.py	Sat May 12 16:00:58 2012 +0200
+++ b/hgext/zeroconf/__init__.py	Sat May 12 16:02:45 2012 +0200
@@ -44,7 +44,7 @@
         s.connect(('1.0.0.1', 0))
         ip = s.getsockname()[0]
         return ip
-    except:
+    except socket.error:
         pass
 
     # Generic method, sometimes gives useless results
@@ -61,7 +61,7 @@
         s.connect(('1.0.0.1', 1))
         ip = s.getsockname()[0]
         return ip
-    except:
+    except socket.error:
         pass
 
     return dumbip
--- a/mercurial/keepalive.py	Sat May 12 16:00:58 2012 +0200
+++ b/mercurial/keepalive.py	Sat May 12 16:02:45 2012 +0200
@@ -758,7 +758,7 @@
     try:
         N = int(sys.argv[1])
         url = sys.argv[2]
-    except:
+    except (IndexError, ValueError):
         print "%s <integer> <url>" % sys.argv[0]
     else:
         test(url, N)
--- a/mercurial/sshrepo.py	Sat May 12 16:00:58 2012 +0200
+++ b/mercurial/sshrepo.py	Sat May 12 16:02:45 2012 +0200
@@ -29,6 +29,7 @@
     def __init__(self, ui, path, create=False):
         self._url = path
         self.ui = ui
+        self.pipeo = self.pipei = self.pipee = None
 
         u = util.url(path, parsequery=False, parsefragment=False)
         if u.scheme != 'ssh' or not u.host or u.path is None:
@@ -111,15 +112,17 @@
         raise exception
 
     def cleanup(self):
+        if self.pipeo is None:
+            return
+        self.pipeo.close()
+        self.pipei.close()
         try:
-            self.pipeo.close()
-            self.pipei.close()
             # read the error descriptor until EOF
             for l in self.pipee:
                 self.ui.status(_("remote: "), l)
-            self.pipee.close()
-        except:
+        except (IOError, ValueError):
             pass
+        self.pipee.close()
 
     __del__ = cleanup
 
--- a/mercurial/util.py	Sat May 12 16:00:58 2012 +0200
+++ b/mercurial/util.py	Sat May 12 16:02:45 2012 +0200
@@ -1079,7 +1079,7 @@
             try:
                 d["d"] = days
                 return parsedate(date, extendeddateformats, d)[0]
-            except:
+            except Abort:
                 pass
         d["d"] = "28"
         return parsedate(date, extendeddateformats, d)[0]
--- a/setup.py	Sat May 12 16:00:58 2012 +0200
+++ b/setup.py	Sat May 12 16:02:45 2012 +0200
@@ -23,16 +23,16 @@
 try:
     import hashlib
     sha = hashlib.sha1()
-except:
+except ImportError:
     try:
         import sha
-    except:
+    except ImportError:
         raise SystemExit(
             "Couldn't import standard hashlib (incomplete Python install).")
 
 try:
     import zlib
-except:
+except ImportError:
     raise SystemExit(
         "Couldn't import standard zlib (incomplete Python install).")
 
@@ -41,7 +41,7 @@
 try:
     isironpython = (platform.python_implementation()
                     .lower().find("ironpython") != -1)
-except:
+except AttributeError:
     pass
 
 if isironpython:
@@ -49,7 +49,7 @@
 else:
     try:
         import bz2
-    except:
+    except ImportError:
         raise SystemExit(
             "Couldn't import standard bz2 (incomplete Python install).")
 
--- a/tests/hghave	Sat May 12 16:00:58 2012 +0200
+++ b/tests/hghave	Sat May 12 16:02:45 2012 +0200
@@ -60,7 +60,7 @@
         os.close(fd)
         os.remove(path)
         return True
-    except:
+    except (IOError, OSError):
         return False
 
 def has_executablebit():
@@ -93,7 +93,7 @@
         try:
             s2 = os.stat(p2)
             return s2 == s1
-        except:
+        except OSError:
             return False
     finally:
         os.remove(path)
--- a/tests/run-tests.py	Sat May 12 16:00:58 2012 +0200
+++ b/tests/run-tests.py	Sat May 12 16:02:45 2012 +0200
@@ -860,7 +860,7 @@
         tf = open(testpath)
         firstline = tf.readline().rstrip()
         tf.close()
-    except:
+    except IOError:
         firstline = ''
     lctest = test.lower()
 
--- a/tests/test-check-code-hg.t	Sat May 12 16:00:58 2012 +0200
+++ b/tests/test-check-code-hg.t	Sat May 12 16:02:45 2012 +0200
@@ -17,27 +17,12 @@
   contrib/setup3k.py:0:
    >         except:
    warning: naked except clause
-  contrib/setup3k.py:0:
-   >     except:
-   warning: naked except clause
-  contrib/setup3k.py:0:
-   > except:
-   warning: naked except clause
-   warning: naked except clause
-   warning: naked except clause
   contrib/shrink-revlog.py:0:
    >         except:
    warning: naked except clause
   hgext/convert/bzr.py:0:
    >         except:
    warning: naked except clause
-  hgext/convert/common.py:0:
-   >             except:
-   warning: naked except clause
-  hgext/convert/common.py:0:
-   >         except:
-   warning: naked except clause
-   warning: naked except clause
   hgext/convert/convcmd.py:0:
    >         except:
    warning: naked except clause
@@ -51,9 +36,6 @@
    >                     ui.write('Parents: %s\n' %
    warning: unwrapped ui message
   hgext/convert/cvsps.py:0:
-   >                 except:
-   warning: naked except clause
-  hgext/convert/cvsps.py:0:
    >                 ui.write('Branchpoints: %s \n' % ', '.join(branchpoints))
    warning: unwrapped ui message
   hgext/convert/cvsps.py:0:
@@ -88,23 +70,11 @@
    >         except:
    warning: naked except clause
   hgext/convert/monotone.py:0:
-   >             except:
-   warning: naked except clause
-  hgext/convert/monotone.py:0:
    >         except:
    warning: naked except clause
   hgext/convert/subversion.py:0:
-   >             except:
-   warning: naked except clause
-  hgext/convert/subversion.py:0:
    >     except:
    warning: naked except clause
-  hgext/gpg.py:0:
-   >                 except:
-   warning: naked except clause
-  hgext/hgcia.py:0:
-   > except:
-   warning: naked except clause
   hgext/hgk.py:0:
    >         ui.write("parent %s\n" % p)
    warning: unwrapped ui message
@@ -126,19 +96,12 @@
   hgext/hgk.py:0:
    >     ui.write("tree %s\n" % short(ctx.changeset()[0]))
    warning: unwrapped ui message
-  hgext/inotify/server.py:0:
-   >                     except:
-   warning: naked except clause
-  hgext/inotify/server.py:0:
-   >             except:
-   warning: naked except clause
   hgext/keyword.py:0:
    >     ui.note("hg ci -m '%s'\n" % msg)
    warning: unwrapped ui message
   hgext/mq.py:0:
    >                 except:
    warning: naked except clause
-   warning: naked except clause
   hgext/mq.py:0:
    >             except:
    warning: naked except clause
@@ -152,18 +115,11 @@
    >         ui.write("mq:     %s\n" % ', '.join(m))
    warning: unwrapped ui message
   hgext/patchbomb.py:0:
-   >             except:
-   warning: naked except clause
-  hgext/patchbomb.py:0:
    >             ui.write('Subject: %s\n' % subj)
    warning: unwrapped ui message
   hgext/patchbomb.py:0:
    >         ui.write('From: %s\n' % sender)
    warning: unwrapped ui message
-  hgext/zeroconf/__init__.py:0:
-   >     except:
-   warning: naked except clause
-   warning: naked except clause
   mercurial/commands.py:0:
    >                 ui.note('branch %s\n' % data)
    warning: unwrapped ui message
@@ -270,9 +226,6 @@
   mercurial/keepalive.py:0:
    >         except:
    warning: naked except clause
-  mercurial/keepalive.py:0:
-   >     except:
-   warning: naked except clause
   mercurial/localrepo.py:0:
    >             except:
    warning: naked except clause
@@ -285,16 +238,10 @@
   mercurial/repair.py:0:
    >     except:
    warning: naked except clause
-  mercurial/sshrepo.py:0:
-   >         except:
-   warning: naked except clause
   mercurial/transaction.py:0:
    >             except:
    warning: naked except clause
   mercurial/util.py:0:
-   >             except:
-   warning: naked except clause
-  mercurial/util.py:0:
    >     except:
    warning: naked except clause
   mercurial/verify.py:0:
@@ -306,27 +253,12 @@
   setup.py:0:
    >         except:
    warning: naked except clause
-  setup.py:0:
-   >     except:
-   warning: naked except clause
-   warning: naked except clause
-  setup.py:0:
-   > except:
-   warning: naked except clause
-   warning: naked except clause
-   warning: naked except clause
   tests/autodiff.py:0:
    >         ui.write('data lost for: %s\n' % fn)
    warning: unwrapped ui message
-  tests/run-tests.py:0:
-   >     except:
-   warning: naked except clause
   tests/test-convert-mtn.t:0:
    >   > function get_passphrase(keypair_id)
    don't use 'function', use old style
-  tests/test-filecache.py:0:
-   >     except:
-   warning: naked except clause
   tests/test-import-git.t:0:
    >   > Mc\${NkU|\`?^000jF3jhEB
    ^ must be quoted
--- a/tests/test-filecache.py	Sat May 12 16:00:58 2012 +0200
+++ b/tests/test-filecache.py	Sat May 12 16:02:45 2012 +0200
@@ -78,7 +78,7 @@
 
     try:
         os.remove('x')
-    except:
+    except OSError:
         pass
 
     basic(fakerepo())