Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,37 @@ def test_module_dict():
del foo.f
assert foo.__dict__ == {}


def test_module_dict_del_non_str_key_equal_to_str_key():
import types

class Key:
def __init__(self, value):
self.value = value
def __hash__(self):
return hash(self.value)
def __eq__(self, other):
return self.value == other

m = types.ModuleType("m")
m.x = 1
m.y = 2
d = vars(m)
assert d[Key("y")] == 2
assert d.pop(Key("y")) == 2
assert "y" not in d
assert d.pop(Key("y"), None) is None
del d[Key("x")]
assert "x" not in d
assert_raises(KeyError, d.__delitem__, Key("missing"))

def f(w):
ns = locals()
del ns[Key("w")]
return ns
assert "w" not in f(1)


def test_hashcode_str_subclass():
class subclass(str):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,17 +603,32 @@ static Object domStringKey(Frame frame, Node inliningTarget, DynamicObjectStorag
@Cached PyUnicodeCheckExactNode isBuiltinString,
@Cached CastBuiltinStringToTruffleStringNode castStr,
@Exclusive @Cached PyObjectHashNode hashNode,
@Cached PyObjectRichCompareBool eqNode,
@Cached DynamicObject.GetKeyArrayNode getKeyArrayNode,
@Cached DynamicObject.GetNode getNode,
@Cached DynamicObject.PutNode putNode,
@Exclusive @Cached InlinedBranchProfile invalidateLengthProfile,
@Cached DynamicObject.SetShapeFlagsNode setShapeFlagsNode) {
if (!isBuiltinString.execute(inliningTarget, keyObj)) {
// Just for the potential side effects
hashNode.execute(frame, inliningTarget, keyObj);
return needsValue ? null : false;
}
TruffleString key = castStr.execute(inliningTarget, keyObj);
DynamicObject store = self.store;
TruffleString key;
if (isBuiltinString.execute(inliningTarget, keyObj)) {
key = castStr.execute(inliningTarget, keyObj);
} else {
// Like DynamicObjectStorage.GetItemNoStringKeyNode: the key can be equal to one of
// the string keys
key = null;
long hash = hashNode.execute(frame, inliningTarget, keyObj);
for (Object currentKey : getKeyArrayNode.execute(store)) {
if (currentKey instanceof TruffleString currentString && hashNode.execute(frame, inliningTarget, currentString) == hash &&
eqNode.executeEq(frame, inliningTarget, keyObj, currentString)) {
key = currentString;
break;
}
}
if (key == null) {
return needsValue ? null : false;
}
}
if (needsValue) {
Object val = getNode.execute(store, key, PNone.NO_VALUE);
if (val == PNone.NO_VALUE) {
Expand Down