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: dict.fromkeys() assumes 'self' is empty
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, arigo, benjamin.peterson, ezio.melotti, jcea, python-dev
Priority: normal Keywords:

Created on 2012-10-28 08:04 by arigo, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
x.py arigo, 2012-10-28 08:04
Messages (6)
msg174009 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2012-10-28 08:04
The implementation of dict.fromkeys() assumes that the new dictionary is empty.  That's not the case if we tweak __new__.  Attached example shows 'dictresize(mp, 0)' being called with 'mp' being dictionary of 10 items.  This causes an infinite uninterruptible loop.
msg174129 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-10-29 16:44
I wonder what the semantics should be. I would like to just clear the dictionary that is returned from __new__ but you're also allowed to return custom classes from __new__ which we might not know how to clear.
msg174140 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2012-10-29 18:49
In case of doubt, see the source code of PyPy :-)

        w_dict = space.call_function(w_type)
        for w_key in space.listview(w_keys):
            space.setitem(w_dict, w_key, w_fill)

(Seriously more compact.)  No clear there...
msg174141 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2012-10-29 18:56
Joke apart, you could check and complain if the object returned is an instance of 'dict' and is not empty.  Otherwise just accept it.  I think complaining is better than picking either of the two behaviors in this case.
msg174160 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-10-29 23:05
That's still inconsistent, though, since we wouldn't complain if __new__ returns a user defined object.
msg174352 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-10-31 18:14
New changeset a965eac352ec by Benjamin Peterson in branch '3.2':
only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
http://hg.python.org/cpython/rev/a965eac352ec

New changeset b68da8f2ed95 by Benjamin Peterson in branch '3.3':
merge 3.2 (#16345)
http://hg.python.org/cpython/rev/b68da8f2ed95

New changeset 411d52192062 by Benjamin Peterson in branch 'default':
merge 3.3 (#16345)
http://hg.python.org/cpython/rev/411d52192062

New changeset 6a582cdfee41 by Benjamin Peterson in branch '2.7':
only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
http://hg.python.org/cpython/rev/6a582cdfee41
History
Date User Action Args
2022-04-11 14:57:37adminsetgithub: 60549
2012-10-31 18:14:19python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg174352

resolution: fixed
stage: needs patch -> resolved
2012-10-29 23:05:13benjamin.petersonsetmessages: + msg174160
2012-10-29 18:56:21arigosetmessages: + msg174141
2012-10-29 18:49:47arigosetmessages: + msg174140
2012-10-29 16:44:11benjamin.petersonsetmessages: + msg174129
2012-10-29 13:02:36jceasetnosy: + jcea
2012-10-28 08:16:42alexsetnosy: + alex
2012-10-28 08:10:42ezio.melottisetnosy: + benjamin.peterson, ezio.melotti
stage: needs patch
type: behavior

versions: - Python 3.1
2012-10-28 08:04:26arigocreate