Skip to content

Commit dce92da

Browse files
committed
add Py_AtExit, PyWeakref_NewRef, PyWeakref_GetObject
1 parent cd1579a commit dce92da

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ Py_ssize_t get_tp_dictoffset(PyTypeObject* obj) {
334334
return obj->tp_dictoffset;
335335
}
336336

337+
/** to be used from Java code only; reads native 'tp_weaklistoffset' field */
338+
Py_ssize_t get_tp_weaklistoffset(PyTypeObject* obj) {
339+
return obj->tp_weaklistoffset;
340+
}
341+
337342
/** to be used from Java code only; reads native 'tp_itemsize' field */
338343
Py_ssize_t get_tp_itemsize(PyTypeObject* obj) {
339344
return obj->tp_itemsize;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ int PyOS_InterruptOccurred(void) {
5656
PyErr_SetString(PyExc_SystemError, "'PyOS_InterruptOccurred' not implemented");
5757
return -1;
5858
}
59+
60+
typedef void (*py_atexit_fun_t)(void (*func)(void));
61+
UPCALL_TYPED_ID(Py_AtExit, py_atexit_fun_t);
62+
int Py_AtExit(void (*func)(void)) {
63+
_jls_Py_AtExit(func);
64+
return 0;
65+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ PyTypeObject _PyWeakref_CallableProxyType = PY_TRUFFLE_TYPE("weakcallableproxy",
4747
void PyObject_ClearWeakRefs(PyObject *object) {
4848
// TODO: implement
4949
}
50+
51+
void *PY_WEAKREF_MODULE;
52+
__attribute__((constructor (__COUNTER__)))
53+
static void initialize_upcall_functions() {
54+
PY_WEAKREF_MODULE = (void*)polyglot_eval("python", "import _weakref\n_weakref");
55+
}
56+
57+
PyObject *PyWeakref_NewRef(PyObject *object, PyObject *callback) {
58+
if (callback == NULL) {
59+
return UPCALL_O(PY_WEAKREF_MODULE, polyglot_from_string("ReferenceType", SRC_CS), native_to_java(object));
60+
} else {
61+
return UPCALL_O(PY_WEAKREF_MODULE, polyglot_from_string("ReferenceType", SRC_CS), native_to_java(object), native_to_java(callback));
62+
}
63+
}
64+
65+
PyObject *PyWeakref_GetObject(PyObject *ref) {
66+
return UPCALL_O(native_to_java(ref), polyglot_from_string("__call__", SRC_CS));
67+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
import com.oracle.graal.python.util.BufferFormat;
253253
import com.oracle.graal.python.util.OverflowException;
254254
import com.oracle.graal.python.util.PythonUtils;
255+
import com.oracle.graal.python.util.ShutdownHook;
255256
import com.oracle.truffle.api.CompilerDirectives;
256257
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
257258
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -4176,4 +4177,25 @@ int doGeneric(Object ptrObject) {
41764177
return 0;
41774178
}
41784179
}
4180+
4181+
// directly called without landing function
4182+
@Builtin(name = "Py_AtExit", minNumOfPositionalArgs = 1)
4183+
@GenerateNodeFactory
4184+
abstract static class PyAtExit extends PythonUnaryBuiltinNode {
4185+
4186+
@Specialization
4187+
@TruffleBoundary
4188+
int doGeneric(Object funcPtr) {
4189+
getContext().registerAtexitHook(new ShutdownHook(){
4190+
public void call(@SuppressWarnings("unused") PythonContext context) {
4191+
try {
4192+
InteropLibrary.getUncached().execute(funcPtr);
4193+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
4194+
// ignored
4195+
}
4196+
}
4197+
});
4198+
return 0;
4199+
}
4200+
}
41794201
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public enum NativeCAPISymbol implements NativeCExtSymbol {
117117
FUN_GET_TP_FLAGS("get_tp_flags"),
118118
FUN_GET_TP_SUBCLASSES("get_tp_subclasses"),
119119
FUN_GET_TP_DICTOFFSET("get_tp_dictoffset"),
120+
FUN_GET_TP_WEAKLISTOFFSET("get_tp_weaklistoffset"),
120121
FUN_GET_TP_BASICSIZE("get_tp_basicsize"),
121122
FUN_GET_TP_ITEMSIZE("get_tp_itemsize"),
122123
FUN_GET_TP_AS_BUFFER("get_tp_as_buffer"),

0 commit comments

Comments
 (0)