Skip to content

Commit dc9169f

Browse files
committed
Make PyObject_GenericGetDict work on managed objects
1 parent 631eade commit dc9169f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5528,6 +5528,10 @@ _PyObject_FreeInstanceAttributes(PyObject *self)
55285528
PyObject *
55295529
PyObject_GenericGetDict(PyObject *obj, void *context)
55305530
{
5531+
// GraalPy change: upcall for managed
5532+
if (points_to_py_handle_space(obj)) {
5533+
return GraalPyTruffleObject_GenericGetDict(obj);
5534+
}
55315535
PyObject *dict;
55325536
// GraalPy change: we don't have inlined values in managed dict
55335537
PyObject **dictptr = _PyObject_GetDictPtr(obj);

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_object.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,26 @@ def __hash__(self):
15951595
cmpfunc=unhandled_error_compare
15961596
)
15971597

1598+
object_with_attributes = MyObject()
1599+
object_with_attributes.foo = 1
1600+
1601+
test_PyObject_GenericGetDict = CPyExtFunction(
1602+
lambda args: args[0].__dict__,
1603+
lambda: (
1604+
(TestObjectFunctions.object_with_attributes,),
1605+
),
1606+
code='''
1607+
static PyObject* wrap_PyObject_GenericGetDict(PyObject* obj) {
1608+
return PyObject_GenericGetDict(obj, NULL);
1609+
}
1610+
''',
1611+
arguments=["PyObject* obj"],
1612+
resultspec="O",
1613+
argspec="O",
1614+
callfunction="wrap_PyObject_GenericGetDict",
1615+
cmpfunc=unhandled_error_compare
1616+
)
1617+
15981618

15991619
class TestPickleNative:
16001620
def test_pickle_native(self):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextObjectBuiltins.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import com.oracle.graal.python.nodes.call.CallNode;
127127
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
128128
import com.oracle.graal.python.nodes.object.GetClassNode;
129+
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
129130
import com.oracle.graal.python.nodes.util.CannotCastException;
130131
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
131132
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
@@ -739,4 +740,14 @@ static Object dir(Object object,
739740
return dir.execute(null, inliningTarget, object);
740741
}
741742
}
743+
744+
@CApiBuiltin(ret = PyObjectTransfer, args = {PyObject}, call = Ignored)
745+
abstract static class PyTruffleObject_GenericGetDict extends CApiUnaryBuiltinNode {
746+
@Specialization
747+
static Object getDict(Object object,
748+
@Bind("this") Node inliningTarget,
749+
@Cached GetOrCreateDictNode getDict) {
750+
return getDict.execute(inliningTarget, object);
751+
}
752+
}
742753
}

0 commit comments

Comments
 (0)