From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <gentoo-catalyst+bounces-3280-garchives=archives.gentoo.org@lists.gentoo.org> Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id D9B1B13888F for <garchives@archives.gentoo.org>; Tue, 6 Oct 2015 15:05:51 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0737C21C041; Tue, 6 Oct 2015 15:05:37 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 4991821C010 for <gentoo-catalyst@lists.gentoo.org>; Tue, 6 Oct 2015 15:05:36 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 78321340AF1 for <gentoo-catalyst@lists.gentoo.org>; Tue, 6 Oct 2015 15:05:35 +0000 (UTC) From: Mike Frysinger <vapier@gentoo.org> To: gentoo-catalyst@lists.gentoo.org Subject: [gentoo-catalyst] [PATCH 05/13] lint: clean up bare exception handling Date: Tue, 6 Oct 2015 11:05:21 -0400 Message-Id: <1444143929-26705-5-git-send-email-vapier@gentoo.org> X-Mailer: git-send-email 2.5.2 In-Reply-To: <1444143929-26705-1-git-send-email-vapier@gentoo.org> References: <1444143929-26705-1-git-send-email-vapier@gentoo.org> Precedence: bulk List-Post: <mailto:gentoo-catalyst@lists.gentoo.org> List-Help: <mailto:gentoo-catalyst+help@lists.gentoo.org> List-Unsubscribe: <mailto:gentoo-catalyst+unsubscribe@lists.gentoo.org> List-Subscribe: <mailto:gentoo-catalyst+subscribe@lists.gentoo.org> List-Id: Gentoo Linux mail <gentoo-catalyst.gentoo.org> X-BeenThere: gentoo-catalyst@lists.gentoo.org Reply-to: gentoo-catalyst@lists.gentoo.org X-Archives-Salt: 5cf6ed82-92a9-4d52-a2e4-51a207bf710f X-Archives-Hash: a5e9a4330dd9383ae247b02f29fe3ee7 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: -- 2.5.2