Skip to content

Commit 7202b6c

Browse files
committed
Correctly implement registering of native subclasses.
1 parent fe71826 commit 7202b6c

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

graalpython/com.oracle.graal.python.cext/src/object.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,24 @@ Py_ssize_t PyObject_Size(PyObject *o) {
163163
}
164164

165165
static int add_subclass(PyTypeObject *base, PyTypeObject *type) {
166-
PyObject* result = UPCALL_CEXT_O("PyTruffle_Add_Subclass", native_to_java(base->tp_subclasses), native_to_java(PyLong_FromVoidPtr(type)), native_to_java((PyObject*)type));
167-
if (result == NULL) {
168-
return -1;
169-
}
170-
base->tp_subclasses = result;
171-
return 0;
166+
void* key = PyLong_FromVoidPtr((void *) type);
167+
if (key == NULL) {
168+
return -1;
169+
}
170+
if (polyglot_is_value(base)) {
171+
return polyglot_as_i32(polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Add_Subclass", native_to_java(base), native_to_java(key), native_to_java(type)));
172+
} else {
173+
PyObject *dict = base->tp_subclasses;
174+
if (dict == NULL) {
175+
base->tp_subclasses = dict = PyDict_New();
176+
if (dict == NULL) {
177+
return -1;
178+
}
179+
}
180+
// TODO value should be a weak reference !
181+
return PyDict_SetItem(base->tp_subclasses, key, type);
182+
}
183+
return -1;
172184
}
173185

174186
/* Special C landing functions that convert some arguments to primitives. */

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*/
3939
package com.oracle.graal.python.builtins.modules;
4040

41+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
42+
4143
import java.nio.ByteBuffer;
4244
import java.nio.CharBuffer;
4345
import java.nio.charset.CharacterCodingException;
@@ -1206,4 +1208,25 @@ Object get() {
12061208
return getContext().getCustomThreadState();
12071209
}
12081210
}
1211+
1212+
@Builtin(name = "PyTruffle_Add_Subclass", fixedNumOfArguments = 3)
1213+
@GenerateNodeFactory
1214+
abstract static class PyTruffle_Add_Subclass extends NativeBuiltin {
1215+
1216+
@Specialization
1217+
int doManagedSubclass(PythonClassNativeWrapper base, @SuppressWarnings("unused") Object key, PythonClassNativeWrapper value) {
1218+
addToSet((PythonClass) base.getPythonObject(), (PythonClass) value.getPythonObject());
1219+
return 0;
1220+
}
1221+
1222+
@Fallback
1223+
int doGeneric(@SuppressWarnings("unused") Object base, @SuppressWarnings("unused") Object key, @SuppressWarnings("unused") Object value) {
1224+
return raiseNative(-1, SystemError, "Builtin can only handle managed base class.");
1225+
}
1226+
1227+
@TruffleBoundary
1228+
private static void addToSet(PythonClass base, PythonClass value) {
1229+
base.getSubClasses().add(value);
1230+
}
1231+
}
12091232
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -738,14 +738,6 @@ def PyType_IsSubtype(a, b):
738738
return 1 if b in a.mro() else 0
739739

740740

741-
@may_raise
742-
def PyTruffle_Add_Subclass(bases_dict, key, cls):
743-
if not bases_dict:
744-
bases_dict = dict()
745-
bases_dict[key] = cls
746-
return bases_dict
747-
748-
749741
def PyTuple_New(size):
750742
return (None,) * size
751743

0 commit comments

Comments
 (0)