From: Rob Browning Date: Sat, 28 Dec 2019 20:39:44 +0000 (-0600) Subject: Add compat.reraise to handle python 3 syntax breakage X-Git-Tag: 0.31~195 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=20dc106a111db4822ccac34688fa8eb5ae2bfc48 Add compat.reraise to handle python 3 syntax breakage 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 Tested-by: Rob Browning --- diff --git a/lib/bup/compat.py b/lib/bup/compat.py index f5f9dc0..397920a 100644 --- a/lib/bup/compat.py +++ b/lib/bup/compat.py @@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function 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. @@ -35,6 +35,9 @@ if py3: """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 @@ -74,6 +77,9 @@ else: # Python 2 from pipes import quote from os import environ + + from bup.py2raise import reraise + 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 index 0000000..0d7332a --- /dev/null +++ b/lib/bup/py2raise.py @@ -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]