[issue27635] pickle documentation says that unpickling may not call __new__
New submission from July Tikhonov: 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'
---------- assignee: docs@python components: Documentation messages: 271465 nosy: docs@python, july priority: normal severity: normal status: open title: pickle documentation says that unpickling may not call __new__ versions: Python 3.4, Python 3.5, Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27635> _______________________________________
Changes by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- nosy: +alexandre.vassalotti, serhiy.storchaka stage: -> needs patch type: -> behavior versions: +Python 3.7 -Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27635> _______________________________________
Change by Furkan Onder <furkanonder@protonmail.com>: ---------- keywords: +patch nosy: +furkanonder nosy_count: 4.0 -> 5.0 pull_requests: +18626 stage: needs patch -> patch review pull_request: https://github.com/python/cpython/pull/19269 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Furkan Onder <furkanonder@protonmail.com> added the comment: There is still an mistake in the document. I sent a pr to fix it. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/482259d0dcf27714a84cf56b93977320bea... ---------- nosy: +miss-islington _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +18922 pull_request: https://github.com/python/cpython/pull/19585 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +18923 pull_request: https://github.com/python/cpython/pull/19586 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/0abb548cc7b239fbe426ca9e00968130e53... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/020f2aaaea95aef6f54ab31488926ed7601... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Furkan Onder <furkanonder@protonmail.com> added the comment: The problem is fixed, issue can be closed. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Irit Katriel <iritkatriel@yahoo.com> added the comment: Ping (to close the issue). ---------- nosy: +iritkatriel _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
Change by Alexandre Vassalotti <alexandre@peadrop.com>: ---------- stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27635> _______________________________________
participants (6)
-
Alexandre Vassalotti
-
Furkan Onder
-
Irit Katriel
-
July Tikhonov
-
miss-islington
-
Serhiy Storchaka