classification
Title: picke cannot dump Exception subclasses with different super() args
Type: Stage:
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bquinlan, gregory.p.smith, raabf
Priority: normal Keywords:

Created on 2019-06-14 23:30 by bquinlan, last changed 2019-07-26 16:39 by raabf.

Files
File name Uploaded Description Edit
nopickle.py bquinlan, 2019-06-14 23:30
Messages (3)
msg345647 - (view) Author: Brian Quinlan (bquinlan) * (Python committer) Date: 2019-06-14 23:30
$ ./python.exe nopickle.py
TypeError: __init__() missing 1 required positional argument: 'num'

The issue is that the arguments passed to Exception.__init__ (via `super()`) are collected into `args` and then serialized by pickle e.g.

>>> PickleBreaker(5).args
()
>>> PickleBreaker(5).__reduce_ex__(3)
(<class '__main__.PoolBreaker'>, (), {'num': 5})
>>> # The 1st index is the `args` tuple

Then, during load, the `args` tuple is used to initialize the Exception i.e. PickleBreaker(), which results in the `TypeError`

See https://github.com/python/cpython/blob/master/Modules/_pickle.c#L6769
msg345909 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-06-17 21:07
possibly related to https://bugs.python.org/issue32696 and https://bugs.python.org/issue1692335.
msg348506 - (view) Author: Fabian Raab (raabf) Date: 2019-07-26 16:39
It seems to that this problem is affecting __new__ methods independent of exceptions:

>>> class NewBreaker:
...     def __new__(cls, arg):
...             return super().__new__(cls)
...
>>> nb = NewBreaker(42)
>>> import pickle
>>> dumped = pickle.dumps(nb)
>>> pickle.loads(dumped)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __new__() missing 1 required positional argument: 'arg'
History
Date User Action Args
2019-07-26 16:39:50raabfsetmessages: + msg348506
2019-06-28 16:32:36raabfsetnosy: + raabf
2019-06-17 21:07:03gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg345909
2019-06-14 23:33:41bquinlanlinkissue37208 superseder
2019-06-14 23:31:56bquinlansettitle: picke cannot dump exceptions subclasses with different super() args -> picke cannot dump Exception subclasses with different super() args
2019-06-14 23:30:45bquinlancreate