Issue 27015: pickling and repr of exceptions with kwargs - Python tracker

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
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Taywee, astrand, gregory.p.smith, ncoghlan, remi.lapeyre, serhiy.storchaka
Priority: normal Keywords: patch, patch, patch

Created on 2016-05-13 16:59 by Taywee, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 11580 open remi.lapeyre, 2019-01-16 14:17
Messages (4)
msg265482 - (view) Author: Taywee (Taywee) Date: 2016-05-13 16:59
When using kwargs to construct a CalledProcessError, the repr doesn't show those args, and using kwargs also breaks pickling:

>>> import pickle; from subprocess import CalledProcessError
>>> CalledProcessError(2, 'foo')
CalledProcessError(2, 'foo')
>>> CalledProcessError(2, 'foo').returncode
2
>>> CalledProcessError(2, 'foo').cmd
'foo'
>>> CalledProcessError(returncode=2, cmd='foo')
CalledProcessError()
>>> CalledProcessError(returncode=2, cmd='foo').returncode
2
>>> CalledProcessError(returncode=2, cmd='foo').cmd
'foo'
>>> pickle.loads(pickle.dumps(CalledProcessError(2, 'foo')))
CalledProcessError(2, 'foo')
>>> pickle.loads(pickle.dumps(CalledProcessError(returncode=2, cmd='foo')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 2 required positional arguments: 'returncode' and 'cmd'
>>>
msg265486 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-13 19:37
This is a problem not only with CalledProcessError, but with all custom exceptions with overridden __init__. BaseException.__new__ saves positional arguments as the "args" attribute, but ignores keyword arguments. repr() and pickle use "args".
msg333769 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-01-16 14:19
I tried to fix the issue, the attached PR solves the issue of saving the kwargs and unpickling the exception but I was not able to fix a regression I caused in test_memory_error_in_PyErr_PrintEx.
msg334626 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2019-01-31 13:33
Reviewing Rémi's page made me realise that a big part of the root cause here is pickle support in exceptions predating the introduction of `__getnewargs__` and `__getnewargs_ex__`.