-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-132461: Fix crash in OrderedDict.setdefault when key has unstable hash #132462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,12 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import abc | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import builtins | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import contextlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import gc | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import pickle | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||
from random import randrange, shuffle | ||||||||||||||||||||||||||||||||||||||||||||||||||||
from random import randrange, shuffle, randint | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import struct | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import unittest | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -729,6 +730,22 @@ def test_merge_operator(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||
with self.assertRaises(ValueError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
a |= "BAD" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_unstable_hash_should_not_abort_issue132461(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# OrderedDict should raise, not abort, when used with unstable-hash keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||
large_num = 2**64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
class WeirdBase(abc.ABCMeta): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
def __hash__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return randint(0, large_num) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
class weird_bytes(bytes, metaclass=WeirdBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
obj = self.OrderedDict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
with self.assertRaises(Exception): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
for x in range(100): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
obj.setdefault(weird_bytes, None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+735
to
+747
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We can also remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. part of test
error
fixed part of test
Very strange There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, we should only catch |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
@support.cpython_only | ||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_ordered_dict_items_result_gc(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# bpo-42536: OrderedDict.items's tuple-reuse speed trick breaks the GC's | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to return a new one on each call, otherwise this test will sometimes be flaky, when random will produce the same number twice.