]> arthur.barton.de Git - bup.git/commitdiff
Add compat.reraise to handle python 3 syntax breakage
authorRob Browning <rlb@defaultvalue.org>
Sat, 28 Dec 2019 20:39:44 +0000 (14:39 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 19 Jan 2020 17:46:10 +0000 (11:46 -0600)
Add a exception reraise function to compat that will allow us to
rewrite invocations like this:

    except Exception as e:
        raise ClientError, e, sys.exc_info()[2]

as this:

    except Exception as e:
        reraise(ClientError(e))

since python 3 now provides a with_traceback() method that we can (and
must) use instead.

Put the python 2 specific formulation (shown above) in a separate
py2raise module that we can conditionally import because python 3
decided to make the python 2 code produce a syntax error.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/compat.py
lib/bup/py2raise.py [new file with mode: 0644]

index f5f9dc0ffa3801632d74dbca79e3bfa67104a4a9..397920a3d333ee8d8b27effab7e90abdd84c074e 100644 (file)
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function
 from array import array
 from binascii import hexlify
 from traceback import print_exception
 from array import array
 from binascii import hexlify
 from traceback import print_exception
-import sys
+import os, sys
 
 # Please see CODINGSTYLE for important exception handling guidelines
 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
 
 # Please see CODINGSTYLE for important exception handling guidelines
 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
@@ -35,6 +35,9 @@ if py3:
         """Return hex string (not bytes as with hexlify) representation of b."""
         return b.hex()
 
         """Return hex string (not bytes as with hexlify) representation of b."""
         return b.hex()
 
+    def reraise(ex):
+        raise ex.with_traceback(sys.exc_info()[2])
+
     def add_ex_tb(ex):
         """Do nothing (already handled by Python 3 infrastructure)."""
         return ex
     def add_ex_tb(ex):
         """Do nothing (already handled by Python 3 infrastructure)."""
         return ex
@@ -74,6 +77,9 @@ else:  # Python 2
 
     from pipes import quote
     from os import environ
 
     from pipes import quote
     from os import environ
+
+    from bup.py2raise import reraise
+
     range = xrange
     str_type = basestring
     int_types = (int, long)
     range = xrange
     str_type = basestring
     int_types = (int, long)
diff --git a/lib/bup/py2raise.py b/lib/bup/py2raise.py
new file mode 100644 (file)
index 0000000..0d7332a
--- /dev/null
@@ -0,0 +1,6 @@
+
+# This file exists because the raise syntax is completely incompatible
+# with Python 3.
+
+def reraise(ex):
+    raise ex, None, sys.exc_info()[2]