Patchwork [4,of,4] pycompat: extract helper to raise exception with traceback

login
register
mail settings
Submitter Yuya Nishihara
Date May 3, 2017, 1:50 a.m.
Message ID <aeafe29a89e55b2f038f.1493776224@mimosa>
Download mbox | patch
Permalink /patch/20388/
State Accepted
Headers show

Comments

Yuya Nishihara - May 3, 2017, 1:50 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1492694172 -32400
#      Thu Apr 20 22:16:12 2017 +0900
# Node ID aeafe29a89e55b2f038fed1314abd9d65a58cd1f
# Parent  0bce0c9c98ac1140fbee92912cb18aaee86a7c61
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".
Augie Fackler - May 6, 2017, 5 p.m.
On Wed, May 03, 2017 at 10:50:24AM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1492694172 -32400
> #      Thu Apr 20 22:16:12 2017 +0900
> # Node ID aeafe29a89e55b2f038fed1314abd9d65a58cd1f
> # Parent  0bce0c9c98ac1140fbee92912cb18aaee86a7c61
> pycompat: extract helper to raise exception with traceback

queued, thanks

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -1005,7 +1005,7 @@  class bundlepart(object):
             # 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 @@  class bundlepart(object):
             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)
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -164,6 +164,10 @@  if ispy3:
             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 @@  else:
     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.