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: pickling and repr of exceptions with kwargs
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
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: Nick 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__`.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71202
2022-01-06 13:25:55iritkatrielsetkeywords: patch, patch, patch
title: subprocess.CalledProcessError's repr changes based on kwargs, and doesn't unpickle -> pickling and repr of exceptions with kwargs
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8
2022-01-06 13:22:02iritkatriellinkissue37489 superseder
2019-01-31 13:33:08ncoghlansetkeywords: patch, patch, patch
nosy: + ncoghlan
messages: + msg334626

2019-01-31 11:30:22ncoghlansetpull_requests: - pull_request11261
2019-01-31 11:30:08ncoghlansetpull_requests: - pull_request11260
2019-01-16 14:19:09remi.lapeyresetmessages: + msg333769
versions: + Python 3.6, Python 3.7, Python 3.8
2019-01-16 14:17:14remi.lapeyresetkeywords: + patch
stage: patch review
pull_requests: + pull_request11262
2019-01-16 14:17:05remi.lapeyresetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11261
2019-01-16 14:16:55remi.lapeyresetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11260
2019-01-11 16:20:11remi.lapeyresetnosy: + remi.lapeyre
2016-05-13 19:37:28serhiy.storchakasetnosy: + gregory.p.smith, serhiy.storchaka, astrand
messages: + msg265486
2016-05-13 16:59:09Tayweecreate