changeset 32232:76f9a0009b4b

pycompat: extract helper to raise exception with traceback It uses "raise excobj, None, tb" form which I think is simpler and more useful than "raise exctype, args, tb".
author Yuya Nishihara <yuya@tcha.org>
date Thu, 20 Apr 2017 22:16:12 +0900
parents cf424dae5dc7
children e62cf13e0858
files mercurial/bundle2.py mercurial/pycompat.py
diffstat 2 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundle2.py	Mon May 01 17:23:48 2017 +0900
+++ b/mercurial/bundle2.py	Thu Apr 20 22:16:12 2017 +0900
@@ -1005,7 +1005,7 @@
             # backup exception data for later
             ui.debug('bundle2-input-stream-interrupt: encoding exception %s'
                      % exc)
-            exc_info = sys.exc_info()
+            tb = sys.exc_info()[2]
             msg = 'unexpected error: %s' % exc
             interpart = bundlepart('error:abort', [('message', msg)],
                                    mandatory=False)
@@ -1016,10 +1016,7 @@
             outdebug(ui, 'closing payload chunk')
             # abort current part payload
             yield _pack(_fpayloadsize, 0)
-            if pycompat.ispy3:
-                raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
-            else:
-                exec("""raise exc_info[0], exc_info[1], exc_info[2]""")
+            pycompat.raisewithtb(exc, tb)
         # end of payload
         outdebug(ui, 'closing payload chunk')
         yield _pack(_fpayloadsize, 0)
--- a/mercurial/pycompat.py	Mon May 01 17:23:48 2017 +0900
+++ b/mercurial/pycompat.py	Thu Apr 20 22:16:12 2017 +0900
@@ -164,6 +164,10 @@
             return s
         return s.decode(u'latin-1')
 
+    def raisewithtb(exc, tb):
+        """Raise exception with the given traceback"""
+        raise exc.with_traceback(tb)
+
     def _wrapattrfunc(f):
         @functools.wraps(f)
         def w(object, name, *args):
@@ -224,6 +228,10 @@
     sysbytes = identity
     sysstr = identity
 
+    # this can't be parsed on Python 3
+    exec('def raisewithtb(exc, tb):\n'
+         '    raise exc, None, tb\n')
+
     # Partial backport from os.py in Python 3, which only accepts bytes.
     # In Python 2, our paths should only ever be bytes, a unicode path
     # indicates a bug.