Skip to content

Commit 7409467

Browse files
committed
Minor refactorings
1 parent d51ef00 commit 7409467

File tree

8 files changed

+40
-91
lines changed

8 files changed

+40
-91
lines changed

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/SlotsProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static TpSlotData createHashNotImplemented(TypeElement enclosingType) {
8383

8484
public String builderCall() {
8585
if (isHashNotImplemented()) {
86-
return ".set(TpSlots.TpSlotMeta.TP_HASH, com.oracle.graal.python.builtins.objects.type.slots.PyObjectHashNotImplemented.INSTANCE)";
86+
return ".set(TpSlots.TpSlotMeta.TP_HASH, com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun.HASH_NOT_IMPLEMENTED)";
8787
}
8888
return String.format(".set(TpSlots.TpSlotMeta.%s, %s.INSTANCE)", //
8989
slot.value().name().toUpperCase(Locale.ROOT), //

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/BaseSetBuiltins.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
import com.oracle.graal.python.builtins.objects.type.TpSlots;
7979
import com.oracle.graal.python.builtins.objects.type.TpSlots.GetObjectSlotsNode;
8080
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
81-
import com.oracle.graal.python.builtins.objects.type.slots.PyObjectHashNotImplemented;
8281
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotBinaryOp.BinaryOpBuiltinNode;
82+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun;
8383
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotLen.LenBuiltinNode;
8484
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotRichCompare;
8585
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotSqContains.SqContainsBuiltinNode;
@@ -503,7 +503,7 @@ static Object doPSet(Node inliningTarget, PSet key,
503503
// come from the tp_hash implementation or from the tp_richcompare also called during
504504
// the search
505505
TpSlots slots = getSlotsNode.execute(inliningTarget, key);
506-
if (slots.tp_hash() == null || slots.tp_hash() == PyObjectHashNotImplemented.INSTANCE) {
506+
if (slots.tp_hash() == null || slots.tp_hash() == TpSlotHashFun.HASH_NOT_IMPLEMENTED) {
507507
return PFactory.createFrozenSet(PythonLanguage.get(inliningTarget), copyNode.execute(inliningTarget, key.getDictStorage()));
508508
} else {
509509
return key;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TpSlots.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@
167167
import com.oracle.graal.python.builtins.objects.type.TpSlotsFactory.GetObjectSlotsNodeGen;
168168
import com.oracle.graal.python.builtins.objects.type.TpSlotsFactory.GetTpSlotsNodeGen;
169169
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetSubclassesAsArrayNode;
170-
import com.oracle.graal.python.builtins.objects.type.slots.PyObjectHashNotImplemented;
171170
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot;
172171
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotBuiltin;
173172
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotCExtNative;
@@ -186,6 +185,7 @@
186185
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotDescrSet.TpSlotDescrSetPython;
187186
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotGetAttr.TpSlotGetAttrBuiltin;
188187
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotGetAttr.TpSlotGetAttrPython;
188+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun;
189189
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun.TpSlotHashBuiltin;
190190
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotInquiry.TpSlotInquiryBuiltin;
191191
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotIterNext;
@@ -1239,7 +1239,7 @@ public static TpSlots fromNative(PythonAbstractNativeObject pythonClass, PythonC
12391239
// PyObject_HashNotImplemented, we still assign it to the slot as wrapped
12401240
// native executable later on
12411241
if (CApiContext.isIdenticalToSymbol(fieldPointer, NativeCAPISymbol.FUN_PYOBJECT_HASH_NOT_IMPLEMENTED)) {
1242-
builder.set(def, TpSlotIterNext.NEXT_NOT_IMPLEMENTED);
1242+
builder.set(def, TpSlotHashFun.HASH_NOT_IMPLEMENTED);
12431243
continue;
12441244
}
12451245
} else if (def == TpSlotMeta.TP_ITERNEXT) {
@@ -1534,7 +1534,7 @@ private static Builder updateSlots(PythonAbstractClass klass, Builder slots, Set
15341534
// TODO: special cases:
15351535
// PyCFunction_Type && tp_new (looks like just optimization)
15361536
} else if (descr == PNone.NONE && slot == TpSlotMeta.TP_HASH) {
1537-
specific = PyObjectHashNotImplemented.INSTANCE;
1537+
specific = TpSlotHashFun.HASH_NOT_IMPLEMENTED;
15381538
} else {
15391539
useGeneric = true;
15401540
generic = defs[i].functionFactory;
@@ -1621,7 +1621,7 @@ public static void addOperatorsToBuiltin(Map<TruffleString, BoundBuiltinCallable
16211621
for (var slotDefGroup : SLOTDEFS.entrySet()) {
16221622
TpSlotMeta slotMeta = slotDefGroup.getKey();
16231623
TpSlot slotValue = slotMeta.getter.get(slots);
1624-
if (slotMeta == TpSlotMeta.TP_HASH && slotValue == PyObjectHashNotImplemented.INSTANCE) {
1624+
if (slotMeta == TpSlotMeta.TP_HASH && slotValue == TpSlotHashFun.HASH_NOT_IMPLEMENTED) {
16251625
DynamicObjectLibrary.getUncached().put(pythonBuiltinClass, T___HASH__, PNone.NONE);
16261626
continue;
16271627
}
@@ -1664,7 +1664,7 @@ public void addOperators(PythonClass type) {
16641664
continue;
16651665
}
16661666
Object wrapperDescriptor = null;
1667-
if (value == PyObjectHashNotImplemented.INSTANCE) {
1667+
if (value == TpSlotHashFun.HASH_NOT_IMPLEMENTED) {
16681668
wrapperDescriptor = PNone.NO_VALUE;
16691669
} else if (value instanceof TpSlotBuiltin<?> builtinSlot) {
16701670
wrapperDescriptor = builtinSlot.createBuiltin(context, type, tpSlotDef.name, tpSlotDef.wrapper);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.IsSameTypeNodeGen;
178178
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.IsTypeNodeGen;
179179
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.SetTypeFlagsNodeGen;
180-
import com.oracle.graal.python.builtins.objects.type.slots.PyObjectHashNotImplemented;
180+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun;
181181
import com.oracle.graal.python.lib.PyObjectSizeNode;
182182
import com.oracle.graal.python.lib.PyUnicodeCheckNode;
183183
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -2027,7 +2027,7 @@ protected PythonClass makeType(VirtualFrame frame, PDict namespaceOrig, TruffleS
20272027
if (inheritedSlots.get(TpSlotMeta.TP_HASH) == null) {
20282028
Object dunderHash = getItemNamespace.execute(inliningTarget, namespace.getDictStorage(), T___HASH__);
20292029
if (dunderHash == null) {
2030-
inheritedSlots.set(TpSlotMeta.TP_HASH, PyObjectHashNotImplemented.INSTANCE);
2030+
inheritedSlots.set(TpSlotMeta.TP_HASH, TpSlotHashFun.HASH_NOT_IMPLEMENTED);
20312031
newType.setAttribute(T___HASH__, PNone.NONE);
20322032
}
20332033
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/slots/PyObjectHashNotImplemented.java

-79
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/slots/TpSlot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static Object toNative(TpSlotMeta slotMeta, TpSlot slot, Object defaultVa
110110
}
111111

112112
private static Object getNativeWrapper(TpSlotMeta slotMeta, TpSlotManaged slot) {
113-
if (slot == PyObjectHashNotImplemented.INSTANCE) {
113+
if (slot == TpSlotHashFun.HASH_NOT_IMPLEMENTED) {
114114
// If there are more such cases, we should add generic mapping mechanism
115115
// This translation other way around is also done in TpSlots.fromNative
116116
// We must not cache this in the singleton slot object, it would hold onto and leak

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/slots/TpSlotHashFun.java

+28
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@
5252
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNode;
5353
import com.oracle.graal.python.builtins.objects.function.PArguments;
5454
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins;
55+
import com.oracle.graal.python.builtins.objects.type.TpSlots.TpSlotMeta;
5556
import com.oracle.graal.python.builtins.objects.type.slots.PythonDispatchers.UnaryPythonSlotDispatcherNode;
5657
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotBuiltinBase;
5758
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotCExtNative;
59+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotManaged;
5860
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotPythonSingle;
61+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun.PyObjectHashNotImplemented.HashNotImplementedNode;
62+
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFunFactory.PyObjectHashNotImplementedFactory.HashNotImplementedNodeFactory;
5963
import com.oracle.graal.python.lib.PyLongAsLongAndOverflowNode;
6064
import com.oracle.graal.python.lib.PyLongCheckNode;
6165
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -74,6 +78,7 @@
7478
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7579
import com.oracle.truffle.api.dsl.GenerateCached;
7680
import com.oracle.truffle.api.dsl.GenerateInline;
81+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7782
import com.oracle.truffle.api.dsl.GenerateUncached;
7883
import com.oracle.truffle.api.dsl.NodeFactory;
7984
import com.oracle.truffle.api.dsl.Specialization;
@@ -83,9 +88,32 @@
8388
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
8489

8590
public abstract class TpSlotHashFun {
91+
public static final TpSlotManaged HASH_NOT_IMPLEMENTED = new PyObjectHashNotImplemented();
92+
8693
private TpSlotHashFun() {
8794
}
8895

96+
/**
97+
* Mirror of the {@code PyObject_HashNotImplemented} slot on the managed side. We translate this
98+
* slot singleton instance to a pointer to the {@code PyObject_HashNotImplemented} C function in
99+
* {@link TpSlot#toNative(TpSlotMeta, TpSlot, Object)} and vice versa in
100+
* {@code TpSlot#fromNative(PythonContext, Object, InteropLibrary)}.
101+
*/
102+
public static class PyObjectHashNotImplemented extends TpSlotHashBuiltin<HashNotImplementedNode> {
103+
private PyObjectHashNotImplemented() {
104+
super(HashNotImplementedNodeFactory.getInstance());
105+
}
106+
107+
@GenerateNodeFactory
108+
public abstract static class HashNotImplementedNode extends HashBuiltinNode {
109+
@Specialization
110+
static long doIt(@SuppressWarnings("unused") Object obj,
111+
@Bind Node nodeForRaise) {
112+
throw PRaiseNode.raiseStatic(nodeForRaise, PythonBuiltinClassType.TypeError, ErrorMessages.UNHASHABLE_TYPE_P, obj);
113+
}
114+
}
115+
}
116+
89117
public abstract static class TpSlotHashBuiltin<T extends HashBuiltinNode>
90118
extends TpSlotBuiltinBase<T> {
91119
static final BuiltinSlotWrapperSignature SIGNATURE = BuiltinSlotWrapperSignature.UNARY;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectRichCompareBool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
* referential equality first (when doing equality comparison) before calling the special method.
7171
* This makes a difference for objects that report they are unequal to themselves (i.e.
7272
* {@code NaN}). Since we do not maintain identity for unboxed float objects, we cannot fully match
73-
* the CPython behavior - we treat all NaNs as equal.
73+
* the CPython behavior - we treat all NaNs with exactly the same bits as equal.
7474
*/
7575
@GenerateInline
7676
@GenerateCached(false)

0 commit comments

Comments
 (0)