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: [doc] Add section on pickling to exceptions documentation
Type: enhancement Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: bquinlan, docs@python, gregory.p.smith, iritkatriel, raabf
Priority: normal Keywords:

Created on 2019-06-14 23:30 by bquinlan, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
nopickle.py bquinlan, 2019-06-14 23:30
Messages (4)
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'
msg409872 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-06 16:27
This is the same as issue32696.

I'm turning this into a documentation issue.
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81468
2022-01-06 16:27:39iritkatrielsetassignee: docs@python
type: enhancement

components: + Documentation, - Library (Lib)
title: picke cannot dump Exception subclasses with different super() args -> [doc] Add section on pickling to exceptions documentation
nosy: + docs@python, iritkatriel
versions: + Python 3.10, Python 3.11, - Python 3.7, Python 3.8
messages: + msg409872
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