Repository navigation
pickling and repr of exceptions with kwargs #71202
Copy link
Copy link
Open
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
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' >>>
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 13, 2016 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".
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.
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__.- added3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesand removed3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life
on Jan 6, 2022 - changed the title
[-]subprocess.CalledProcessError's repr changes based on kwargs, and doesn't unpickle[/-][+]pickling and repr of exceptions with kwargs[/+]on Jan 6, 2022
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsNo status
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: