Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit Exception chains are not pickled. #60

Open
bernardpazio opened this issue Jul 21, 2020 · 0 comments
Open

Implicit Exception chains are not pickled. #60

bernardpazio opened this issue Jul 21, 2020 · 0 comments

Comments

@bernardpazio
Copy link

Explicit exceptions those given with the from syntax when raising an exception are pickled, however ones which are called with out this are not.

excepted:

def chain_exception():
    try:
        raise Exception('Foo')
    except Exception as ex:
        try:
            raise Exception('Bar')
        except Exception as e:
            raise Exception('explict') from e

chain_exception()
Exception chain
Traceback (most recent call last):
  File "<ipython-input-14-53e847eef0b4>", line 5, in chain_exception
    raise Exception('Foo')
Exception: Foo

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-14-53e847eef0b4>", line 8, in chain_exception
    raise Exception('Bar')
Exception: Bar

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/bert/.conda/envs/dask-dev/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-14-53e847eef0b4>", line 12, in <module>
    chain_exception()
  File "<ipython-input-14-53e847eef0b4>", line 10, in chain_exception
    raise Exception('explict') from e
Exception: explict

after pickling:

import pickle
import tblib.pickling_support
tblib.pickling_support.install()

try:
    chain_exception()
except Exception as e:
    pickled_e = pickle.dumps(e, protocol=pickle.HIGHEST_PROTOCOL)

raise pickled_e
Exception chain
Traceback (most recent call last):
  File "<ipython-input-14-53e847eef0b4>", line 8, in chain_exception
    raise Exception('Bar')
Exception: Bar

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/bert/.conda/envs/dask-dev/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-269163ec6143>", line 8, in <module>
    raise pickle.loads(pickled_e)
  File "<ipython-input-15-269163ec6143>", line 2, in <module>
    chain_exception()
  File "<ipython-input-14-53e847eef0b4>", line 10, in chain_exception
    raise Exception('explict') from e
Exception: explict

I believe this is simply due to the fact that __cause__ attribute is pickled but the __context__ one is not. See PEP 3134 for further details on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant