raise pending_ex
If an exception is thrown from an exception handler, the pending
-exception should be "chained" to the new exception as the context.
-This can be accomplished via ``chain_ex()``::
+exception should be the `"context"
+<https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement>`_
+of the new exception This can be accomplished via
+``add_ex_ctx()``::
try:
...
...
except ... as ex2:
add_ex_tb(ex2)
- raise chain_ex(ex2, ex)
+ raise add_ex_ctx(ex2, ex)
See the end of ``lib/bup/compat.py`` for a functional example.
import sys
# Please see CODINGSTYLE for important exception handling guidelines
-# and the rationale behind add_ex_tb(), chain_ex(), etc.
+# and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
py_maj = sys.version_info[0]
py3 = py_maj >= 3
"""Do nothing (already handled by Python 3 infrastructure)."""
return ex
- def chain_ex(ex, context_ex):
+ def add_ex_ctx(ex, context_ex):
"""Do nothing (already handled by Python 3 infrastructure)."""
return ex
ex.__traceback__ = sys.exc_info()[2]
return ex
- def chain_ex(ex, context_ex):
- """Chain context_ex to ex as the __context__ (unless it already has
- one). Return ex.
+ def add_ex_ctx(ex, context_ex):
+ """Make context_ex the __context__ of ex (unless it already has one).
+ Return ex.
"""
if context_ex:
try:
raise Exception('second')
except Exception as ex2:
- raise chain_ex(add_ex_tb(ex2), ex)
+ raise add_ex_ctx(add_ex_tb(ex2), ex)
wrap_main(outer)
from bup import compat, helpers
-from bup.compat import add_ex_tb, chain_ex, wrap_main
+from bup.compat import add_ex_tb, add_ex_ctx, wrap_main
from bup.helpers import atoi, columnate, debug1, log, tty_width
if split[0]:
pending.setdefault(fd, []).extend(split)
except BaseException as ex:
- pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+ pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
try:
# Try to finish each of the streams
for fd, pending_items in compat.items(pending):
try:
print_clean_line(dest, pending_items, width)
except (EnvironmentError, EOFError) as ex:
- pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+ pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
except BaseException as ex:
- pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+ pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
if pending_ex:
raise pending_ex
os.kill(p.pid, signal.SIGTERM)
p.wait()
except BaseException as kill_ex:
- raise chain_ex(add_ex_tb(kill_ex), ex)
+ raise add_ex_ctx(add_ex_tb(kill_ex), ex)
raise ex
wrap_main(lambda : run_subcmd(subcmd))