commit: dd3d5a4138662836243e1686167708f98fe2bf0b
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Tue Oct 6 14:02:42 2015 +0000
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Tue Oct 6 14:02:42 2015 +0000
URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=dd3d5a41
lint: clean up bare exception handling
It's a bad idea to use a bare except clause as you end up including
things like SystemExit, KeyboardInterrupt, and GeneratorExit, none
of which we actually want to catch. Some of the cases in the code
were explicitly catching & passing SystemExit back up which proves
this point.
catalyst/base/stagebase.py | 2 +-
catalyst/lock.py | 26 ++++----------------------
catalyst/main.py | 2 +-
catalyst/support.py | 15 +++++----------
targets/stage1/build.py | 2 +-
5 files changed, 12 insertions(+), 35 deletions(-)
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index 409fcab..e393c5b 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -1015,7 +1015,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
target is fully initialized
"""
self.snapshot_lock_object.unlock()
- except:
+ except Exception:
pass
if ouch:
"""
diff --git a/catalyst/lock.py b/catalyst/lock.py
index 01b1aa8..d6653f7 100644
--- a/catalyst/lock.py
+++ b/catalyst/lock.py
@@ -130,8 +130,6 @@ class LockDir(object):
try:
if os.stat(self.lockfile).st_gid !=
self.gid:
os.chown(self.lockfile,os.getuid(),self.gid)
- except SystemExit, e:
- raise
except OSError, e:
if e[0] == 2: #XXX: No such file or
directory
return
self.fcntl_locking(locktype)
@@ -185,7 +183,7 @@ class LockDir(object):
try:
os.close(self.myfd)
self.myfd=None
- except:
+ except Exception:
pass
return False
@@ -194,8 +192,6 @@ class LockDir(object):
self.myfd = os.open(self.lockfile,
os.O_WRONLY,0660)
unlinkfile = 1
self.locking_method(self.myfd,fcntl.LOCK_UN)
- except SystemExit, e:
- raise e
except Exception, e:
#if self.myfd is not None:
#print "fcntl_unlock() trying to
close", self.myfd
@@ -213,7 +209,7 @@ class LockDir(object):
InUse=False
try:
self.locking_method(self.myfd,fcntl.LOCK_EX|fcntl.LOCK_NB)
- except:
+ except Exception:
print "Read lock may be
in effect. skipping lockfile delete..."
InUse=True
# We won the lock, so
there isn't competition for it.
@@ -227,8 +223,6 @@ class LockDir(object):
self.myfd=None
# if "DEBUG" in self.settings:
# print "Unlinked
lockfile..."
- except SystemExit, e:
- raise e
except Exception, e:
# We really don't care... Someone else
has the lock.
# So it is their problem now.
@@ -273,8 +267,6 @@ class LockDir(object):
print_traceback=True)
try:
os.link(self.myhardlock, self.lockfile)
- except SystemExit:
- raise
except Exception:
# if "DEBUG" in self.settings:
# print "lockfile(): Hardlink: Link
failed."
@@ -305,9 +297,7 @@ class LockDir(object):
os.unlink(self.myhardlock)
if os.path.exists(self.lockfile):
os.unlink(self.lockfile)
- except SystemExit:
- raise
- except:
+ except Exception:
writemsg("Something strange happened to our hardlink
locks.\n")
def add_hardlock_file_to_cleanup(self):
@@ -335,9 +325,7 @@ class LockDir(object):
try:
myhls = os.stat(link)
mylfs = os.stat(lock)
- except SystemExit:
- raise
- except:
+ except Exception:
myhls = None
mylfs = None
@@ -406,8 +394,6 @@ class LockDir(object):
# We're
sweeping through, unlinking everyone's locks.
os.unlink(filename)
results.append("Unlinked: " + filename)
- except SystemExit:
- raise
except Exception:
pass
try:
@@ -415,16 +401,12 @@ class LockDir(object):
results.append("Unlinked: " + x)
os.unlink(mylockname)
results.append("Unlinked: " +
mylockname)
- except SystemExit:
- raise
except Exception:
pass
else:
try:
os.unlink(mylockname)
results.append("Unlinked: " +
mylockname)
- except SystemExit:
- raise
except Exception:
pass
return results
diff --git a/catalyst/main.py b/catalyst/main.py
index 04f689e..4e83414 100644
--- a/catalyst/main.py
+++ b/catalyst/main.py
@@ -95,7 +95,7 @@ def parse_config(myconfig):
myconfig = catalyst.config.ConfigParser(config_file)
myconf.update(myconfig.get_values())
- except:
+ except Exception:
print "!!! catalyst: Unable to parse configuration file,
"+myconfig
sys.exit(1)
diff --git a/catalyst/support.py b/catalyst/support.py
index 90c59eb..f184ed7 100644
--- a/catalyst/support.py
+++ b/catalyst/support.py
@@ -22,9 +22,7 @@ DESIRED_RLIMIT = 0
try:
import resource
max_fd_limit=resource.getrlimit(resource.RLIMIT_NOFILE)[DESIRED_RLIMIT]
-except SystemExit, e:
- raise
-except:
+except Exception:
# hokay, no resource module.
max_fd_limit=256
@@ -48,7 +46,7 @@ def read_from_clst(path):
myline = ''
try:
myf = open(path, "r")
- except:
+ except Exception:
return -1
#raise CatalystError("Could not open file " + path)
for line in myf.readlines():
@@ -136,10 +134,7 @@ def cmd(mycmd, myexc="", env=None, debug=False,
fail_func=None):
if debug:
print "***** cmd(); args =", args
- try:
- proc = Popen(args, env=env)
- except:
- raise
+ proc = Popen(args, env=env)
if proc.wait() != 0:
if fail_func:
print "CMD(), NON-Zero command return. Running
fail_func()"
@@ -243,7 +238,7 @@ def read_makeconf(mymakeconffile):
try:
import portage.util
return
portage.util.getconfig(mymakeconffile, tolerant=1, allow_sourcing=True)
- except:
+ except Exception:
try:
import portage_util
return
portage_util.getconfig(mymakeconffile, tolerant=1, allow_sourcing=True)
@@ -252,7 +247,7 @@ def read_makeconf(mymakeconffile):
mylines=myf.readlines()
myf.close()
return parse_makeconf(mylines)
- except:
+ except Exception:
raise CatalystError("Could not parse make.conf file " +
mymakeconffile, print_traceback=True)
else:
diff --git a/targets/stage1/build.py b/targets/stage1/build.py
index be1bc4d..fa4fd13 100755
--- a/targets/stage1/build.py
+++ b/targets/stage1/build.py
@@ -33,7 +33,7 @@ for idx in range(0, len(pkgs)):
buildpkgs[bidx] = pkgs[idx]
if buildpkgs[bidx][0:1] == "*":
buildpkgs[bidx] = buildpkgs[bidx][1:]
- except:
+ except Exception:
pass
for b in buildpkgs: