From 2ab3b6844941590c99983f0276560ef86fedd6c8 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 25 Jul 2024 14:41:37 +0200 Subject: [PATCH 3/9] Add call to `JsProxy_GetMethod` to help remove temporary `_PyObject_GetMethod` is a special attribute lookup function that won't call the `__get__` descriptor on a method to avoid creating a temporary PyMethodObject. We also want to optimize away a temporary JsProxy in a special way. In order to do this, we patch the behavior of `_PyObject_GetMethod` to use `JsProxy_GetMethod`. In order to avoid linker errors when used with just Python and not with libpyodide.a, we declare a dummy JsProxy_GetMethod with weak linkage that does nothing. When linked to libpyodide.a it will get overridden by the real function. Otherwise, this patch does nothing. See the definition of `JsProxy_GetMethod` in `jsproxy.c` and particularly `JsMethodCallSingleton` for how this is used. --- Objects/object.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Objects/object.c b/Objects/object.c index 8dc3ab9643c..49fb1da1073 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1562,6 +1562,18 @@ _PyObject_NextNotImplemented(PyObject *self) } + +PyObject* __attribute__((weak)) +JsProxy_GetAttr(PyObject* self, PyObject* attr) { + return NULL; +} + +PyObject* __attribute__((weak)) +JsProxy_GetMethod(PyObject* obj, PyObject* name) { + return NULL; +} + + /* Specialized version of _PyObject_GenericGetAttrWithDict specifically for the LOAD_METHOD opcode. @@ -1586,6 +1598,11 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) } } + if (tp->tp_getattro == JsProxy_GetAttr) { + *method = JsProxy_GetMethod(obj, name); + return 0; + } + if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { *method = PyObject_GetAttr(obj, name); return 0; -- 2.43.0