[python-bugzilla] [PATCH 2/4] Make exception handling forward compatible with python3

abn at redhat.com abn at redhat.com
Thu Oct 17 14:50:42 UTC 2013


From: Arun Babu Neelicattu <abn at redhat.com>

---
 bin/bugzilla           | 16 ++++++++++------
 bugzilla/base.py       | 10 +++++++---
 tests/__init__.py      |  3 ++-
 tests/misc.py          |  3 ++-
 tests/rw_functional.py |  3 ++-
 5 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/bin/bugzilla b/bin/bugzilla
index 980970d..ec9ba77 100755
--- a/bin/bugzilla
+++ b/bin/bugzilla
@@ -64,12 +64,13 @@ def open_without_clobber(name, *args):
     while fd is None:
         try:
             fd = os.open(name, os.O_CREAT | os.O_EXCL, 0666)
-        except OSError, e:
-            if e.errno == os.errno.EEXIST:
+        except OSError:
+            err = sys.exc_info()[1]
+            if err.errno == os.errno.EEXIST:
                 name = "%s.%i" % (orig_name, count)
                 count += 1
             else:
-                raise IOError(e.errno, e.strerror, e.filename)
+                raise IOError(err.errno, err.strerror, err.filename)
     fobj = open(name, *args)
     if fd != fobj.fileno():
         os.close(fd)
@@ -1166,15 +1167,18 @@ if __name__ == '__main__':
         log.debug("", exc_info=True)
         print("\nExited at user request.")
         sys.exit(1)
-    except socket.error, e:
+    except socket.error:
+        e = sys.exc_info()[1]
         log.debug("", exc_info=True)
         print("\nConnection lost/failed: %s" % str(e))
         sys.exit(2)
-    except (xmlrpclib.Fault, urllib2.HTTPError), e:
+    except (xmlrpclib.Fault, urllib2.HTTPError):
+        e = sys.exc_info()[1]
         log.debug("", exc_info=True)
         print("\nServer error: %s" % str(e))
         sys.exit(3)
-    except xmlrpclib.ProtocolError, e:
+    except xmlrpclib.ProtocolError:
+        e = sys.exc_info()[1]
         log.debug("", exc_info=True)
         print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))
 
diff --git a/bugzilla/base.py b/bugzilla/base.py
index 9c25fbb..878edaf 100644
--- a/bugzilla/base.py
+++ b/bugzilla/base.py
@@ -12,6 +12,7 @@
 import cookielib
 import os
 import StringIO
+import sys
 import urllib2
 import urlparse
 import xmlrpclib
@@ -36,7 +37,8 @@ def _detect_filetype(fname):
             import magic
             mimemagic = magic.open(magic.MAGIC_MIME_TYPE)
             mimemagic.load()
-        except ImportError, e:
+        except ImportError:
+            e = sys.exc_info()[1]
             log.debug("Could not load python-magic: %s", e)
             mimemagic = False
     if mimemagic is False:
@@ -47,7 +49,8 @@ def _detect_filetype(fname):
 
     try:
         return mimemagic.file(fname)
-    except Exception, e:
+    except Exception:
+        e = sys.exc_info()[1]
         log.debug("Could not detect content_type: %s", e)
     return None
 
@@ -206,7 +209,8 @@ class _CURLTransport(xmlrpclib.Transport):
                               "SIGINT, raising")
                     m.remove_handle(self.c)
                     raise KeyboardInterrupt
-        except pycurl.error, e:
+        except pycurl.error:
+            e = sys.exc_info()[1]
             raise xmlrpclib.ProtocolError(url, e[0], e[1], None)
 
         b.seek(0)
diff --git a/tests/__init__.py b/tests/__init__.py
index 8d32b39..4bf7541 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -77,7 +77,8 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False,
             print()
 
             mainout = bugzillascript.main(bzinstance)
-        except SystemExit, sys_e:
+        except SystemExit:
+            sys_e = sys.exc_info()[1]
             ret = sys_e.code
 
         outt = ""
diff --git a/tests/misc.py b/tests/misc.py
index 865dd8e..6f800ec 100644
--- a/tests/misc.py
+++ b/tests/misc.py
@@ -36,7 +36,8 @@ class MiscCLI(unittest.TestCase):
 
             from logilab.common.optik_ext import ManHelpFormatter
             ignore = ManHelpFormatter
-        except Exception, e:
+        except Exception:
+            e = sys.exc_info()[1]
             print("Skipping man page test: %s" % e)
             return
 
diff --git a/tests/rw_functional.py b/tests/rw_functional.py
index a50f736..ce06265 100644
--- a/tests/rw_functional.py
+++ b/tests/rw_functional.py
@@ -698,7 +698,8 @@ class RHPartnerTest(BaseTest):
             bz.cookiefile = None
             raise AssertionError("Setting cookiefile for active connection "
                                  "should fail.")
-        except RuntimeError, e:
+        except RuntimeError:
+            e = sys.exc_info()[1]
             self.assertTrue("disconnect()" in str(e))
 
         bz.disconnect()
-- 
1.8.3.1



More information about the python-bugzilla mailing list