classification
Title: pickle documentation says that unpickling may not call __new__
Type: behavior Stage: patch review
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: alexandre.vassalotti, docs@python, furkanonder, july, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-07-27 17:07 by july, last changed 2020-04-21 23:12 by furkanonder.

Pull Requests
URL Status Linked Edit
PR 19269 merged furkanonder, 2020-04-01 00:17
PR 19585 merged miss-islington, 2020-04-18 18:09
PR 19586 merged miss-islington, 2020-04-18 18:09
Messages (6)
msg271465 - (view) Author: July Tikhonov (july) * Date: 2016-07-27 17:07
A note just below object.__setstate__() documentation
https://docs.python.org/3.6/library/pickle.html#object.__setstate__
says that
"""
… the type should implement __getnewargs__() or __getnewargs_ex__() to establish such an invariant; otherwise, neither __new__() nor __init__() will be called.
"""

I believe that note about not calling __new__() was relevant in python2. I could not find case in python3 in which __new__() would not be called. And __init__() is not called anyway, as far as I understand (unless explicitly by __setstate__() or something).

Python 3.6.0a3+ (default:da9898e7e90d, Jul 27 2016, 19:51:12) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
...   def __getstate__(self): return {'foo' : self.foo}
...   def __setstate__(self, state): self.foo = state['foo']
...   def __new__(cls):
...     print('__new__ is called'); return super().__new__(cls)
...   def __init__(self):
...     print('__init__ is called'); self.foo = None; super().__init__()
... 
>>> c = C(); c.foo = 'bar'
__new__ is called
__init__ is called
>>> import pickle
>>> c2 = pickle.loads(pickle.dumps(c))
__new__ is called
>>> c2.foo
'bar'
msg365442 - (view) Author: Furkan Onder (furkanonder) * Date: 2020-04-01 00:21
There is still an mistake in the document. I sent a pr to fix it.
msg366730 - (view) Author: miss-islington (miss-islington) Date: 2020-04-18 18:09
New changeset 482259d0dcf27714a84cf56b93977320bea7e093 by Furkan Önder in branch 'master':
bpo-27635: Fix pickle documentation about `__new__` not being called. (GH-19269)
https://github.com/python/cpython/commit/482259d0dcf27714a84cf56b93977320bea7e093
msg366731 - (view) Author: miss-islington (miss-islington) Date: 2020-04-18 18:14
New changeset 0abb548cc7b239fbe426ca9e00968130e53ffc98 by Miss Islington (bot) in branch '3.7':
bpo-27635: Fix pickle documentation about `__new__` not being called. (GH-19269)
https://github.com/python/cpython/commit/0abb548cc7b239fbe426ca9e00968130e53ffc98
msg366732 - (view) Author: miss-islington (miss-islington) Date: 2020-04-18 18:14
New changeset 020f2aaaea95aef6f54ab31488926ed76017e41a by Miss Islington (bot) in branch '3.8':
bpo-27635: Fix pickle documentation about `__new__` not being called. (GH-19269)
https://github.com/python/cpython/commit/020f2aaaea95aef6f54ab31488926ed76017e41a
msg366950 - (view) Author: Furkan Onder (furkanonder) * Date: 2020-04-21 23:12
The problem is fixed, issue can be closed.
History
Date User Action Args
2020-04-21 23:12:57furkanondersetmessages: + msg366950
2020-04-18 18:14:58miss-islingtonsetmessages: + msg366732
2020-04-18 18:14:44miss-islingtonsetmessages: + msg366731
2020-04-18 18:09:30miss-islingtonsetpull_requests: + pull_request18923
2020-04-18 18:09:24miss-islingtonsetpull_requests: + pull_request18922
2020-04-18 18:09:13miss-islingtonsetnosy: + miss-islington
messages: + msg366730
2020-04-01 00:21:22furkanondersetmessages: + msg365442
2020-04-01 00:17:13furkanondersetkeywords: + patch
nosy: + furkanonder

pull_requests: + pull_request18626
stage: needs patch -> patch review
2017-02-18 20:47:59serhiy.storchakasetnosy: + alexandre.vassalotti, serhiy.storchaka
stage: needs patch
type: behavior

versions: + Python 3.7, - Python 3.4
2017-02-18 20:47:07serhiy.storchakalinkissue29597 superseder
2016-07-27 17:07:25julycreate