This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pickle does not serialize Exception __cause__ field
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alexandre.vassalotti, brett.cannon, diekhans, eric.snow, iritkatriel, kakshay, ncoghlan, serhiy.storchaka, tjb900
Priority: normal Keywords:

Created on 2017-02-06 18:36 by diekhans, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
cause_pickle.py diekhans, 2017-02-06 18:36 bug demog program
Messages (6)
msg287163 - (view) Author: Mark Diekhans (diekhans) Date: 2017-02-06 18:36
python3 pickle does not serialize the __cause__ field, as shown by the attached demo program.
msg287304 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-08 11:55
True. Attributes __context__, __cause__ and __traceback__ are not pickled. The traceback objects are even not pickleable.

What is worse, some other non-special attributes are lost during pickling. For example name and path attributes of ImportError.

>>> try: import foo
... except Exception as ex: exc = ex
... 
>>> exc.name
'foo'
>>> exc.__reduce__()
(<class 'ModuleNotFoundError'>, ("No module named 'foo'",), {})

Or the value attribute of StopIteration if it was not passed to the constructor.

>>> exc = StopIteration()
>>> exc.value = 42
>>> exc.__reduce__()
(<class 'StopIteration'>, (), {})
msg335686 - (view) Author: Kumar Akshay (kakshay) * Date: 2019-02-16 14:15
Hey, can I work on this?
msg396603 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-27 21:41
I get different output for Serhiy's first example now, but the same for the second:

>>> try: import foo
... except Exception as ex: exc = ex
... 
>>> exc.name
'foo'
>>> exc.__reduce__()
(<class 'ModuleNotFoundError'>, ("No module named 'foo'",), {'name': 'foo'})
>>> exc = StopIteration()
>>> exc.value = 42
>>> exc.__reduce__()
(<class 'StopIteration'>, ())
>>>
msg396604 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-27 21:41
See also issue43460, issue32696, issue30005.
msg409871 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-06 16:21
> Attributes __context__, __cause__ and __traceback__ are not pickled. The traceback objects are even not pickleable.


It's not guaranteed that the __cause__ and __context__ are picklable either.

Should we try to pickle them, or should we document this and suggest traceback.TracebackException for storing or transmitting exception information?
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73652
2022-01-06 16:21:58iritkatrielsetmessages: + msg409871
2021-06-27 21:41:37iritkatrielsetmessages: + msg396604
2021-06-27 21:41:18iritkatrielsetnosy: + iritkatriel

messages: + msg396603
versions: + Python 3.11, - Python 3.6
2019-02-16 14:15:36kakshaysetnosy: + kakshay
messages: + msg335686
2019-02-14 05:29:16tjb900setnosy: + tjb900
2017-02-08 11:55:09serhiy.storchakasetnosy: + brett.cannon, ncoghlan, eric.snow
messages: + msg287304
2017-02-07 02:47:24xiang.zhangsetnosy: + serhiy.storchaka
2017-02-06 19:22:50diekhanssetnosy: + alexandre.vassalotti
2017-02-06 18:36:36diekhanscreate