Title: dict_set_fromkeys() calculates size of dictionary improperly · Issue #132762 · python/cpython · GitHub
Open Graph Title: dict_set_fromkeys() calculates size of dictionary improperly · Issue #132762 · python/cpython
X Title: dict_set_fromkeys() calculates size of dictionary improperly · Issue #132762 · python/cpython
Description: Bug report Bug description: The function dict_set_fromkeys() in the file dictobject.c adds elements of an iterable to an existing dictionary. The size of the expanded dictionary is estimated as PySet_GET_SIZE(iterable) and the size of th...
Open Graph Description: Bug report Bug description: The function dict_set_fromkeys() in the file dictobject.c adds elements of an iterable to an existing dictionary. The size of the expanded dictionary is estimated as PyS...
X Description: Bug report Bug description: The function dict_set_fromkeys() in the file dictobject.c adds elements of an iterable to an existing dictionary. The size of the expanded dictionary is estimated as PyS...
Opengraph URL: https://github.com/python/cpython/issues/132762
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"dict_set_fromkeys() calculates size of dictionary improperly","articleBody":"# Bug report\n\n### Bug description:\n\nThe function dict_set_fromkeys() in the file dictobject.c adds elements of an iterable to an existing dictionary. The size of the expanded dictionary is estimated as PySet_GET_SIZE(iterable) and the size of the existing dictionary is not considered.\n\nThis is unlogical.\n\nA more resonable estimation is \"mp-\u003ema_used + PySet_GET_SIZE(iterable)\". What turns this into a bug is the fact that a large dictionary plus a small iterable can cause the function to loop forever.\n\nThe following code is adopted from the official tests, file test_dict.py. Official binaries for Python 3.13.3 compiled with MSVC will loop forever when running this modified test (the official test has range(10)). Other platforms may be affected as well.\n\n```python\nclass baddict3(dict):\n def __new__(cls):\n return d\nd = {i : i for i in range(17)}\nres = d.copy()\nres.update(a=None, b=None, c=None)\nprint(res)\nq = baddict3.fromkeys({\"a\", \"b\", \"c\"})\nprint(q)\n```\nA minor error is in the function calculate_log2_keysize(minsize) which has three branches for calculating its result. However, when minsize \u003c 10 the results of the three branches can differ. Since _BitScanReverse64() is Windows-only other platforms might be unaffected.\n\nA final unrelated note. PEP 11 – CPython platform support states that aarch64-unknown-linux-gnu and x86_64-unknown-linux-gnu are Tier 1 AND Tier 2.\n\n### CPython versions tested on:\n\n3.13\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-133627\n* gh-133685\n* gh-133686\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ThomasBr0","@type":"Person","name":"ThomasBr0"},"datePublished":"2025-04-21T00:17:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/132762/cpython/issues/132762"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:822becca-9354-9eec-eb9f-2e632486fa8f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A47E:1F08C5:4EC6F6:68212F:6A53E9C8 |
| html-safe-nonce | f318ae38450fe419e612dedbcaadeac1baa2c1bc7e133456d4d59ebb0dc3e666 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDdFOjFGMDhDNTo0RUM2RjY6NjgyMTJGOjZBNTNFOUM4IiwidmlzaXRvcl9pZCI6IjY4MDk5OTg5OTU1NDY2OTYxMzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b99b0e00dc2f370a8abaed269b8966bd51666b20341f7668adf636200a7ee7b7 |
| hovercard-subject-tag | issue:3007309237 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/132762/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b65c3b8de85818f84f675e9f07f4f8a39931ecad8a5d1ccd7dc5bb19e20911b8/python/cpython/issues/132762 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b65c3b8de85818f84f675e9f07f4f8a39931ecad8a5d1ccd7dc5bb19e20911b8/python/cpython/issues/132762 |
| og:image:alt | Bug report Bug description: The function dict_set_fromkeys() in the file dictobject.c adds elements of an iterable to an existing dictionary. The size of the expanded dictionary is estimated as PyS... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ThomasBr0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width