Skip to content

Commit d51ef00

Browse files
committed
Refactor tp slots and native symbol comparisons
1 parent 6655d80 commit d51ef00

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,18 @@ public static boolean isIdenticalToSymbol(Object obj, NativeCAPISymbol symbol) {
671671
}
672672
}
673673

674+
public static boolean isIdenticalToSymbol(long ptr, NativeCAPISymbol symbol) {
675+
CompilerAsserts.neverPartOfCompilation();
676+
Object nativeSymbol = getNativeSymbol(null, symbol);
677+
InteropLibrary lib = InteropLibrary.getUncached(nativeSymbol);
678+
lib.toNative(nativeSymbol);
679+
try {
680+
return lib.asPointer(nativeSymbol) == ptr;
681+
} catch (UnsupportedMessageException e) {
682+
throw new RuntimeException(e);
683+
}
684+
}
685+
674686
public static Object getNativeSymbol(Node caller, NativeCAPISymbol symbol) {
675687
Object[] nativeSymbolCache = getSymbolCache(caller);
676688
Object result = nativeSymbolCache[symbol.ordinal()];

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.type;
4242

43-
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_PYOBJECT_HASH_NOT_IMPLEMENTED;
4443
import static com.oracle.graal.python.nodes.SpecialMethodNames.T___ABS__;
4544
import static com.oracle.graal.python.nodes.SpecialMethodNames.T___ADD__;
4645
import static com.oracle.graal.python.nodes.SpecialMethodNames.T___AND__;
@@ -1232,19 +1231,19 @@ public static TpSlots fromNative(PythonAbstractNativeObject pythonClass, PythonC
12321231
Object executable = ctx.getCApiContext().getClosureExecutable(fieldPointer);
12331232
if (executable instanceof TpSlotWrapper execWrapper) {
12341233
existingSlotWrapper = execWrapper;
1235-
} else if (executable == null && CApiContext.isIdenticalToSymbol(field, FUN_PYOBJECT_HASH_NOT_IMPLEMENTED)) {
1236-
builder.set(def, PyObjectHashNotImplemented.INSTANCE);
1237-
continue;
12381234
} else if (executable != null) {
12391235
// This can happen for legacy slots where the delegate would be a PFunction
12401236
LOGGER.fine(() -> String.format("Unexpected executable for slot pointer: %s", executable));
1241-
} else if (def == TpSlotMeta.TP_ITERNEXT) {
1237+
} else if (def == TpSlotMeta.TP_HASH) {
12421238
// If the slot is not tp_iternext, but the value is
1243-
// _PyObject_NextNotImplemented, we still assign it to the slot as wrapped
1244-
// native executable
1245-
Object symbol = CApiContext.getNativeSymbol(null, NativeCAPISymbol.FUN_PY_OBJECT_NEXT_NOT_IMPLEMENTED);
1246-
InteropLibrary symbolLibrary = InteropLibrary.getUncached(symbol);
1247-
if (fieldPointer == symbolLibrary.asPointer(symbol)) {
1239+
// PyObject_HashNotImplemented, we still assign it to the slot as wrapped
1240+
// native executable later on
1241+
if (CApiContext.isIdenticalToSymbol(fieldPointer, NativeCAPISymbol.FUN_PYOBJECT_HASH_NOT_IMPLEMENTED)) {
1242+
builder.set(def, TpSlotIterNext.NEXT_NOT_IMPLEMENTED);
1243+
continue;
1244+
}
1245+
} else if (def == TpSlotMeta.TP_ITERNEXT) {
1246+
if (CApiContext.isIdenticalToSymbol(fieldPointer, NativeCAPISymbol.FUN_PY_OBJECT_NEXT_NOT_IMPLEMENTED)) {
12481247
builder.set(def, TpSlotIterNext.NEXT_NOT_IMPLEMENTED);
12491248
continue;
12501249
}

0 commit comments

Comments
 (0)