*.py: Hack around Python exception-catching syntax mismatch.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 21 Oct 2019 16:53:43 +0000 (17:53 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:21 +0000 (12:44 +0100)
There's no common syntax to capture the current exception value between
Python 2.5 and 3: the former wants

try: ...
except EXCCLS, VAR: ...

while the latter wants

try: ...
except EXCCLS as VAR: ...

Give up and dig the exception value out of `sys.exc_info()' instead.

catacomb/pwsafe.py
pock
pwsafe

index 7740212..7a5cda0 100644 (file)
@@ -35,6 +35,11 @@ from cStringIO import StringIO as _StringIO
 import catacomb as _C
 
 ###--------------------------------------------------------------------------
+### Python version portability.
+
+def _excval(): return _SYS.exc_info()[1]
+
+###--------------------------------------------------------------------------
 ### Text encoding utilities.
 
 def _literalp(s):
@@ -501,7 +506,7 @@ else:
 
     def _open(me, file, writep):
       try: me._db = _G.open(file, writep and 'w' or 'r')
-      except _G.error, e: raise StorageBackendRefusal(e)
+      except _G.error: raise StorageBackendRefusal(_excval())
 
     def _create(me, file):
       me._db = _G.open(file, 'n', 0600)
@@ -564,8 +569,8 @@ else:
         ver = me._query_scalar(
           "SELECT value FROM meta WHERE name = '$version'",
           "version check")
-      except (_Q.DatabaseError, _Q.OperationalError), e:
-        raise StorageBackendRefusal(e)
+      except (_Q.DatabaseError, _Q.OperationalError):
+        raise StorageBackendRefusal(_excval())
       if ver is None: raise ValueError('database broken (missing $version)')
       elif ver < me.VERSION: me._upgrade(ver)
       elif ver > me.VERSION: raise ValueError \
@@ -926,8 +931,8 @@ class DirectoryStorageBackend (PlainTextBackend):
   def _get_passwd(me, label):
     try:
       f = open(me._pwfile(label), 'rb')
-    except (OSError, IOError), e:
-      if e.errno == _E.ENOENT: raise KeyError(label)
+    except (OSError, IOError):
+      if _excval().errno == _E.ENOENT: raise KeyError(label)
       else: raise
     with f: return f.read()
   def _put_passwd(me, label, payload):
@@ -939,8 +944,8 @@ class DirectoryStorageBackend (PlainTextBackend):
   def _del_passwd(me, label):
     try:
       _OS.remove(me._pwfile(label))
-    except (OSError, IOError), e:
-      if e.errno == _E.ENOENT: raise KeyError(label)
+    except (OSError, IOError):
+      if _excval().errno == _E.ENOENT: raise KeyError(label)
       else: raise
   def _iter_passwds(me):
     pw = _OS.path.join(me._dir, 'pw')
diff --git a/pock b/pock
index ff62d8b..2df5fd2 100644 (file)
--- a/pock
+++ b/pock
@@ -38,6 +38,10 @@ import catacomb as C
 ###--------------------------------------------------------------------------
 ### Utilities.
 
+def _excval():
+  """Return the most recent exception object."""
+  return SYS.exc_info()[1]
+
 class ExpectedError (Exception):
   """
   I represent an expected error, which should be reported in a friendly way.
@@ -715,8 +719,9 @@ class PrimeProof (object):
         if step is not None:
           me.addstep(step)
           lastp = step.p
-      except ExpectedError, e:
-        raise ExpectedError('%s:%d: %s' % (file.name, lno, e.message))
+      except ExpectedError:
+        raise ExpectedError('%s:%d: %s' %
+                            (file.name, lno, _excval().message))
     return lastp
 
 ###--------------------------------------------------------------------------
@@ -1059,7 +1064,7 @@ pock [-qv] [-s SIEVEBITS] CMD ARGS...
 if __name__ == '__main__':
   prog = OS.path.basename(argv[0])
   try: __main__()
-  except ExpectedError, e: exit('%s: %s' % (prog, e.message))
-  except IOError, e: exit('%s: %s' % (prog, e))
+  except ExpectedError: exit('%s: %s' % (prog, _excval().message))
+  except IOError: exit('%s: %s' % (prog, _excval()))
 
 ###----- That's all, folks --------------------------------------------------
diff --git a/pwsafe b/pwsafe
index 3e981cd..6a3dfae 100644 (file)
--- a/pwsafe
+++ b/pwsafe
@@ -39,6 +39,11 @@ import catacomb as C
 from catacomb.pwsafe import *
 
 ###--------------------------------------------------------------------------
+### Python version portability.
+
+def _excval(): return SYS.exc_info()[1]
+
+###--------------------------------------------------------------------------
 ### Utilities.
 
 ## The program name.
@@ -95,7 +100,7 @@ def cmd_find(av):
   if len(av) != 1: return 1
   with PW(file) as pw:
     try: print pw[av[0]]
-    except KeyError, exc: die("Password `%s' not found" % exc.args[0])
+    except KeyError: die("Password `%s' not found" % _excval().args[0])
 
 def cmd_store(av):
   if len(av) < 1 or len(av) > 2: return 1
@@ -145,7 +150,7 @@ def cmd_del(av):
   with PW(file, writep = True) as pw:
     tag = av[0]
     try: del pw[tag]
-    except KeyError, exc: die("Password `%s' not found" % exc.args[0])
+    except KeyError: die("Password `%s' not found" % _excval().args[0])
 
 def cmd_xfer(av):