Repository navigation
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
Description
| BPO | 1692335 |
|---|---|
| Nosy | @gvanrossum, @loewis, @brettcannon, @birkenfeld, @facundobatista, @jaraco, @abalkin, @pitrou, @vstinner, @taleinat, @avassalotti, @benjaminp, @orivej, @ambv, @slallum |
| Files |
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:
assignee = None
closed_at = <Date 2012-07-29.12:24:55.210>
created_at = <Date 2007-04-01.13:46:15.000>
labels = ['interpreter-core', 'type-bug']
title = 'Fix exception pickling: Move initial args assignment to BaseException.__new__'
updated_at = <Date 2018-10-11.14:43:02.459>
user = 'https://bugs.python.org/zseil'bugs.python.org fields:
activity = <Date 2018-10-11.14:43:02.459>
actor = 'orivej'
assignee = 'sbt'
closed = True
closed_date = <Date 2012-07-29.12:24:55.210>
closer = 'sbt'
components = ['Interpreter Core']
creation = <Date 2007-04-01.13:46:15.000>
creator = 'zseil'
dependencies = []
files = ['7915', '7916', '7917', '7918', '7919', '7920', '7921', '9198', '17509', '26537', '26550']
hgrepos = []
issue_num = 1692335
keywords = ['patch']
message_count = 64.0
messages = ['52348', '52349', '52350', '52351', '52352', '52353', '52355', '52356', '52357', '52359', '52360', '52361', '52362', '52363', '55165', '55958', '56054', '60067', '60163', '61967', '61997', '61999', '62025', '70453', '75955', '76020', '76026', '76028', '102995', '106811', '108954', '108960', '117139', '117145', '117149', '133581', '151039', '151068', '166332', '166477', '166523', '166526', '166527', '166552', '166555', '166557', '166558', '166572', '166581', '166590', '166592', '166598', '166601', '166605', '166640', '166665', '166717', '166750', '166791', '183153', '310939', '310951', '310955', '310958']
nosy_count = 27.0
nosy_names = ['gvanrossum', 'loewis', 'nnorwitz', 'brett.cannon', 'georg.brandl', 'facundobatista', 'jafo', 'ehuss', 'tseaver', 'jaraco', 'belopolsky', 'zseil', 'fmitha', 'pitrou', 'vstinner', 'taleinat', 'alexandre.vassalotti', 'benjamin.peterson', 'jarpa', 'bpb', 'orivej', 'zbysz', 'kylev', 'lukasz.langa', 'python-dev', 'sbt', 'slallum']
pr_nums = []
priority = 'critical'
resolution = 'fixed'
stage = 'commit review'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue1692335'
versions = ['Python 2.7']Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
Activity
zseil commentedon Apr 1, 2007
Pickling exceptions fails when an Exception class
requires an argument in the constructor, but doesn't
call its base class' constructor. See this mail
for details:
http://mail.python.org/pipermail/python-dev/2007-April/072416.html
This patch simply moves initial args assignment to
BaseException.__new__. This should fix most of the
problems, because it is very unlikely that an
exception overwrites the __new__ method; exceptions
used to be old style classes, which don't support
the __new__ special method.
The args attribute is still overwritten in all the
__init__ methods, so there shouldn't be any backward
compatibility problems.
zseil commentedon Apr 1, 2007
File Added: exc_args_25.diff
zseil commentedon Apr 1, 2007
I'm attaching a test that Eric Huss sent in private
mail.
The test fails, because old exception pickles don't
have args for the reconstructor, but their __init__()
gets called anyway because they are new style classes
now.
The problem is in cPickle.InstanceNew() and
pickle.Unpickler._instantiate(). Those methods behave
differently depending on the type of the object
instantiated; they avoid the __init__() call when type
is an old style class.
There is nothing that can be done in the exception
classes to fix this issue; the fix would need to be
in the pickle and cPickle module.
File Added: test_exception_pickle.py
ehuss commentedon Jun 15, 2007
I have stumbled across another scenario where unpickling fails. If your exception takes arguments, but you call Exception.__init__ with a different number of arguments, it will fail. As in:
ehuss commentedon Jun 27, 2007
Added patch bpo-1744398 as an alternate solution.
birkenfeld commentedon Aug 11, 2007
Attaching a new patch that fixes the
scenario by keeping the original args to __new__ as an exception attribute.
File Added: exception-pickling.diff
nnorwitz commentedon Aug 11, 2007
self->args needs to be initialized to NULL because if the alloc of args failed, self->args would be uninitialized and deallocated. (I realize the alloc of an empty tuple will realistically never fail currently.)
I'm not sure if this could cause any new problems because of the behavior change, but the code itself looked fine to me. Hopefully someone with more knowledge of exceptions could take a look at the idea.
ehuss commentedon Aug 12, 2007
Georg's new patch looks good to me, it seems to pass all the scenarios that I know of. You can close out my patch if you check this in. Thanks for looking into this!
birkenfeld commentedon Aug 12, 2007
The only question left is what to do if something reassigns exceptioninstance.args.
zseil commentedon Aug 12, 2007
There is also the problem that Jim Fulton mentioned
in bug bpo-1742889; if the user modifies the 'message'
attribute of an exception (or any other attribute
that is not stored in the __dict__), it won't get
serialized on pickling.
Attaching a new patch that fixes this by using the
__getstate__() pickling hook. It uses tp_members
and tp_getset members of the type object to find
all the attributes that need to be serialized.
The 2.5 patch also contains a fix for migrating
old exception pickles to Python 2.5. For that
I had to change cPickle and pickle. It would
be nice if Jim could comment if this change is
needed, since ZODB is probably the biggest user
of these modules.
The downside is that this patch is fairly big.
File Added: exception_pickling_25.diff
zseil commentedon Aug 12, 2007
File Added: exception_pickling_26.diff
birkenfeld commentedon Aug 12, 2007
I wouldn't care too much about .message; it was new in 2.5 and is already deprecated in 2.6.
zseil commentedon Aug 12, 2007
It's not just the message attribute; the problem is
that the current code expects that the exception
won't be modified between creation and unpickling.
For example:
64 remaining items
sbt commentedon Jul 29, 2012
I guess so (although the change could arguably be back ported).
Pickling of Exception classes is still somewhat dodgy because an example like
is still not picklable.
Any further improvements can be discussed in a separate issue, so I will close.
birkenfeld commentedon Jul 29, 2012
Please reopen if you think it should be backported.
python-dev commentedon Feb 27, 2013
New changeset 2c9f7ed28384 by R David Murray in branch '3.2':
bpo-17296: backport fix for bpo-1692335, naive exception pickling.
http://hg.python.org/cpython/rev/2c9f7ed28384
New changeset 67c27421b00b by R David Murray in branch '3.3':
Null merge for bpo-1692335 backport.
http://hg.python.org/cpython/rev/67c27421b00b
New changeset 94f107752e83 by R David Murray in branch 'default':
Null merge for bpo-1692335 backport.
http://hg.python.org/cpython/rev/94f107752e83
slallum commentedon Jan 28, 2018
Perhaps this is a problem for a different issue, but pickling custom exceptions fails when the exception gets more than one argument:
this code produces the following error:
jaraco commentedon Jan 28, 2018
@slallum, That does seem to be a problem, though I do observe that the issue reported by bdb with CalledProcessError is no longer an issue:
Looking into how CalledProcessError is defined (
cpython/Lib/subprocess.py
Line 60 in 79db11c
Hmm. It seems it does it by not calling the superclass __init__. Indeed, following that model it seems to work:
slallum commentedon Jan 28, 2018
@jason.coombs as far as I can tell, this only works in python3, but not in python2, where it still produces the same error.
gvanrossum commentedon Jan 28, 2018
Please don't discuss on closed issues. Open a new issue.
Support pickling of SlackException
Support pickling of SlackException
Support pickling of SlackException