Skip to content

Commit 67346ff

Browse files
committed
Fix IsHashable node hash value type check (must be an int), fix DictBuiltins setdefault
- unittest cleanup
1 parent c747dbe commit 67346ff

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_dict.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ def test_dictview_mixed_set_operations():
348348
def test_setdefault():
349349
# dict.setdefault()
350350
d = {}
351-
none = d.setdefault('key0')
352351
assert d.setdefault('key0') is None
353352
d.setdefault('key0', [])
354353
assert d.setdefault('key0') is None

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.oracle.truffle.api.dsl.Fallback;
6969
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7070
import com.oracle.truffle.api.dsl.Specialization;
71+
import com.oracle.truffle.api.profiles.ConditionProfile;
7172

7273
@CoreFunctions(extendClasses = PDict.class)
7374
public final class DictBuiltins extends PythonBuiltins {
@@ -129,20 +130,16 @@ public Object setDefault(PDict dict, Object key, @SuppressWarnings("unused") Obj
129130
return getItemNode.execute(dict.getDictStorage(), key);
130131
}
131132

132-
@Specialization(guards = "!containsKey(dict.getDictStorage(), key)")
133-
public Object setDefault(PDict dict, Object key, @SuppressWarnings("unused") PNone defaultValue,
134-
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode) {
135-
136-
setItemNode.execute(dict, dict.getDictStorage(), key, PNone.NONE);
137-
return PNone.NONE;
138-
}
139-
140133
@Specialization(guards = "!containsKey(dict.getDictStorage(), key)")
141134
public Object setDefault(PDict dict, Object key, Object defaultValue,
142-
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode) {
143-
144-
setItemNode.execute(dict, dict.getDictStorage(), key, defaultValue);
145-
return defaultValue;
135+
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode,
136+
@Cached("createBinaryProfile()") ConditionProfile defaultValProfile) {
137+
Object value = defaultValue;
138+
if (defaultValProfile.profile(defaultValue == PNone.NO_VALUE)) {
139+
value = PNone.NONE;
140+
}
141+
setItemNode.execute(dict, dict.getDictStorage(), key, value);
142+
return value;
146143
}
147144
}
148145

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/datamodel/IsHashableNode.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@
3838
*/
3939
package com.oracle.graal.python.nodes.datamodel;
4040

41+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
42+
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
43+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4144
import com.oracle.graal.python.nodes.PGuards;
4245
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
4346
import com.oracle.graal.python.runtime.exception.PythonErrorType;
4447
import com.oracle.truffle.api.dsl.Cached;
4548
import com.oracle.truffle.api.dsl.Specialization;
46-
import com.oracle.truffle.api.profiles.ConditionProfile;
4749

4850
public abstract class IsHashableNode extends PDataModelEmulationNode {
51+
protected PythonClass getBuiltinIntType() {
52+
return getCore().lookupType(PythonBuiltinClassType.PInt);
53+
}
54+
4955
protected boolean isDouble(Object object) {
5056
return object instanceof Double || PGuards.isPFloat(object);
5157
}
@@ -68,9 +74,10 @@ protected boolean isHashableGeneric(@SuppressWarnings("unused") Object object) {
6874
@Specialization
6975
protected boolean isHashableGeneric(Object object,
7076
@Cached("create(__HASH__)") LookupAndCallUnaryNode lookupHashAttributeNode,
71-
@Cached("createBinaryProfile()") ConditionProfile isIntHashProfile) {
77+
@Cached("create()") BuiltinFunctions.IsInstanceNode isInstanceNode,
78+
@Cached("getBuiltinIntType()") PythonClass IntType) {
7279
Object hashValue = lookupHashAttributeNode.executeObject(object);
73-
if (isIntHashProfile.profile(hashValue instanceof Integer)) {
80+
if (isInstanceNode.executeWith(hashValue, IntType)) {
7481
return true;
7582
}
7683
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");

0 commit comments

Comments
 (0)