Skip to content

Commit 5e112ae

Browse files
committed
Convert to 'Exception as' syntax
1 parent 937834c commit 5e112ae

5 files changed

Lines changed: 22 additions & 41 deletions

File tree

bin/bugzilla

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def open_without_clobber(name, *args):
7979
while fd is None:
8080
try:
8181
fd = os.open(name, os.O_CREAT | os.O_EXCL, 0o666)
82-
except OSError:
83-
err = sys.exc_info()[1]
82+
except OSError as err:
8483
if err.errno == os.errno.EEXIST:
8584
name = "%s.%i" % (orig_name, count)
8685
count += 1
@@ -1008,8 +1007,8 @@ def _handle_login(opt, parser, args, action, bz):
10081007
print("Logging into %s" % urlparse(bz.url)[1])
10091008
bz.interactive_login(
10101009
opt.username, opt.password)
1011-
except bugzilla.BugzillaError:
1012-
print(str(sys.exc_info()[1]))
1010+
except bugzilla.BugzillaError as e:
1011+
print(str(e))
10131012
sys.exit(1)
10141013

10151014
if opt.ensure_logged_in and not bz.logged_in:
@@ -1120,13 +1119,11 @@ if __name__ == '__main__':
11201119
log.debug("", exc_info=True)
11211120
print("\nExited at user request.")
11221121
sys.exit(1)
1123-
except (Fault, bugzilla.BugzillaError):
1124-
e = sys.exc_info()[1]
1122+
except (Fault, bugzilla.BugzillaError) as e:
11251123
log.debug("", exc_info=True)
11261124
print("\nServer error: %s" % str(e))
11271125
sys.exit(3)
1128-
except ProtocolError:
1129-
e = sys.exc_info()[1]
1126+
except ProtocolError as e:
11301127
log.debug("", exc_info=True)
11311128
print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))
11321129
# Detect redirect
@@ -1135,8 +1132,7 @@ if __name__ == '__main__':
11351132
print("\nServer was attempting a redirect. Try: "
11361133
" bugzilla --bugzilla %s ..." % redir)
11371134
sys.exit(4)
1138-
except requests.exceptions.SSLError:
1139-
e = sys.exc_info()[1]
1135+
except requests.exceptions.SSLError as e:
11401136
log.debug("", exc_info=True)
11411137

11421138
# Give SSL recommendations
@@ -1147,8 +1143,7 @@ if __name__ == '__main__':
11471143
sys.exit(4)
11481144
except (socket.error,
11491145
requests.exceptions.HTTPError,
1150-
requests.exceptions.ConnectionError):
1151-
e = sys.exc_info()[1]
1146+
requests.exceptions.ConnectionError) as e:
11521147
log.debug("", exc_info=True)
11531148
print("\nConnection lost/failed: %s" % str(e))
11541149
sys.exit(2)

bugzilla/base.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def _detect_filetype(fname):
5454
import magic
5555
mimemagic = magic.open(getattr(magic, "MAGIC_MIME_TYPE", 16))
5656
mimemagic.load()
57-
except ImportError:
58-
e = sys.exc_info()[1]
57+
except ImportError as e:
5958
log.debug("Could not load python-magic: %s", e)
6059
mimemagic = None
6160
if not mimemagic:
@@ -66,8 +65,7 @@ def _detect_filetype(fname):
6665

6766
try:
6867
return mimemagic.file(fname)
69-
except Exception:
70-
e = sys.exc_info()[1]
68+
except Exception as e:
7169
log.debug("Could not detect content_type: %s", e)
7270
return None
7371

@@ -611,8 +609,7 @@ def login(self, user=None, password=None):
611609
self.password = ''
612610
log.info("login successful for user=%s", self.user)
613611
return ret
614-
except Fault:
615-
e = sys.exc_info()[1]
612+
except Fault as e:
616613
raise BugzillaError("Login failed: %s" % str(e.faultString))
617614

618615
def interactive_login(self, user=None, password=None, force=False):
@@ -667,8 +664,7 @@ def logged_in(self):
667664
try:
668665
self._proxy.User.get({'ids': []})
669666
return True
670-
except Fault:
671-
e = sys.exc_info()[1]
667+
except Fault as e:
672668
if e.faultCode == 505 or e.faultCode == 32000:
673669
return False
674670
raise e
@@ -1215,8 +1211,7 @@ def query(self, query):
12151211
log.debug("Calling Bug.search with: %s", query)
12161212
try:
12171213
r = self._proxy.Bug.search(query)
1218-
except Fault:
1219-
e = sys.exc_info()[1]
1214+
except Fault as e:
12201215

12211216
# Try to give a hint in the error message if url_to_query
12221217
# isn't supported by this bugzilla instance

bugzilla/transport.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,13 @@ def _request_helper(self, url, request_body):
167167

168168
response.raise_for_status()
169169
return self.parse_response(response)
170-
except requests.RequestException:
171-
e = sys.exc_info()[1]
170+
except requests.RequestException as e:
172171
if not response:
173172
raise
174173
raise ProtocolError(
175174
url, response.status_code, str(e), response.headers)
176175
except Fault:
177-
raise sys.exc_info()[1]
176+
raise
178177
except Exception:
179178
# pylint: disable=W0201
180179
e = BugzillaError(str(sys.exc_info()[1]))

tests/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False,
101101
print()
102102

103103
mainout = bugzillascript.main(unittest_bz_instance=bzinstance)
104-
except SystemExit:
105-
sys_e = sys.exc_info()[1]
104+
except SystemExit as sys_e:
106105
ret = sys_e.code
107106

108107
outt = ""

tests/rw_functional.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,7 @@ def test11UserUpdate(self):
720720
bz.updateperms(email, "remove", [group])
721721
user.refresh()
722722
self.assertTrue(group not in user.groupnames)
723-
except:
724-
e = sys.exc_info()[1]
723+
except Exception as e:
725724
if have_admin:
726725
raise
727726
self.assertTrue("Sorry, you aren't a member" in str(e))
@@ -731,8 +730,7 @@ def test11UserUpdate(self):
731730
bz.updateperms(email, "add", group)
732731
user.refresh()
733732
self.assertTrue(group in user.groupnames)
734-
except:
735-
e = sys.exc_info()[1]
733+
except Exception as e:
736734
if have_admin:
737735
raise
738736
self.assertTrue("Sorry, you aren't a member" in str(e))
@@ -745,17 +743,15 @@ def test11UserUpdate(self):
745743
bz.updateperms(email, "set", newgroups)
746744
user.refresh()
747745
self.assertTrue(group not in user.groupnames)
748-
except:
749-
e = sys.exc_info()[1]
746+
except Exception as e:
750747
if have_admin:
751748
raise
752749
self.assertTrue("Sorry, you aren't a member" in str(e))
753750

754751
# Reset everything
755752
try:
756753
bz.updateperms(email, "set", origgroups)
757-
except:
758-
e = sys.exc_info()[1]
754+
except Exception as e:
759755
if have_admin:
760756
raise
761757
self.assertTrue("Sorry, you aren't a member" in str(e))
@@ -809,8 +805,7 @@ def compare(data, newid):
809805
print("Created product=%s component=%s" % (
810806
basedata["product"], basedata["component"]))
811807
compare(data, newid)
812-
except:
813-
e = sys.exc_info()[1]
808+
except Exception as e:
814809
if have_admin:
815810
raise
816811
self.assertTrue("Sorry, you aren't a member" in str(e))
@@ -829,8 +824,7 @@ def compare(data, newid):
829824
try:
830825
bz.editcomponent(data)
831826
compare(data, newid)
832-
except:
833-
e = sys.exc_info()[1]
827+
except Exception as e:
834828
if have_admin:
835829
raise
836830
self.assertTrue("Sorry, you aren't a member" in str(e))
@@ -843,8 +837,7 @@ def test12SetCookie(self):
843837
bz.cookiefile = None
844838
raise AssertionError("Setting cookiefile for active connection "
845839
"should fail.")
846-
except RuntimeError:
847-
e = sys.exc_info()[1]
840+
except RuntimeError as e:
848841
self.assertTrue("disconnect()" in str(e))
849842

850843
bz.disconnect()

0 commit comments

Comments
 (0)