cloudpipe/cloudpickle

Chained exception lost in round trip

Open

#248 opened on Feb 12, 2019

View on GitHub
 (15 comments) (2 reactions) (0 assignees)Python (1,407 stars) (152 forks)batch import
bughelp wanted

Description

It seems that a round trip through cloudpickle results in chained exceptions being dropped. Here's an example below.

In [1]: import cloudpickle                                                      

In [2]: cloudpickle.__version__                                                 
Out[2]: '0.7.0'

In [3]: def raise_twice(): 
   ...:     try: 
   ...:         raise RuntimeError("Oops 1") 
   ...:     except Exception as ex: 
   ...:         raise RuntimeError("Oops 2") from ex 
   ...:                                                                         

In [4]: try: 
   ...:     raise_twice() 
   ...: except Exception as e: 
   ...:     ex = e 
   ...:                                                                         

In [5]: raise ex                                                                
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-520fb2c4a416> in raise_twice()
      2     try:
----> 3         raise RuntimeError("Oops 1")
      4     except Exception as ex:

RuntimeError: Oops 1

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

RuntimeError                              Traceback (most recent call last)
<ipython-input-5-9408e39981f1> in <module>
----> 1 raise ex

<ipython-input-4-c1e246dfdfa0> in <module>
      1 try:
----> 2     raise_twice()
      3 except Exception as e:
      4     ex = e
      5 

<ipython-input-3-520fb2c4a416> in raise_twice()
      3         raise RuntimeError("Oops 1")
      4     except Exception as ex:
----> 5         raise RuntimeError("Oops 2") from ex
      6 

RuntimeError: Oops 2

In [6]: raise cloudpickle.loads(cloudpickle.dumps(ex))                          
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-306e510f2ec1> in <module>
----> 1 raise cloudpickle.loads(cloudpickle.dumps(ex))

RuntimeError: Oops 2

xref: https://github.com/dask/dask/issues/4384

cc @stuarteberg

Contributor guide