Skip to content

Commit 15ea67f

Browse files
steve-stimfel
authored andcommitted
HPyType_FromSpec: support static legacy metaclasses, skip the tests on CPython ABI
1 parent d9fd329 commit 15ea67f

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyNodes.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@
6363
import com.oracle.graal.python.PythonLanguage;
6464
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6565
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins;
66+
import com.oracle.graal.python.builtins.modules.cext.PythonCextListBuiltins;
6667
import com.oracle.graal.python.builtins.objects.PNone;
6768
import com.oracle.graal.python.builtins.objects.PythonAbstractObject.PInteropSubscriptNode;
69+
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
6870
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
6971
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
7072
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.CreateMethodNode;
7173
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FromCharPointerNode;
74+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetTypeMemberNode;
7275
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.SubRefCntNode;
7376
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.PExternalFunctionWrapper;
77+
import com.oracle.graal.python.builtins.objects.cext.capi.NativeMember;
7478
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.AsNativePrimitiveNode;
7579
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.ConvertPIntToPrimitiveNode;
7680
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.ImportCExtSymbolNode;
@@ -138,6 +142,7 @@
138142
import com.oracle.graal.python.nodes.util.CannotCastException;
139143
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
140144
import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
145+
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
141146
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
142147
import com.oracle.graal.python.runtime.PythonContext;
143148
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
@@ -1976,6 +1981,7 @@ Object doGeneric(GraalHPyContext context, Object typeSpec, Object typeSpecParamA
19761981
@Cached PythonObjectFactory factory,
19771982
@Cached PCallHPyFunction callHelperFunctionNode,
19781983
@Cached PCallHPyFunction callMallocNode,
1984+
@Cached GetTypeMemberNode getMetaSizeNode,
19791985
@Cached ReadAttributeFromObjectNode readAttributeFromObjectNode,
19801986
@Cached WriteAttributeToObjectNode writeAttributeToObjectNode,
19811987
@Cached PyObjectCallMethodObjArgs callCreateTypeNode,
@@ -2020,18 +2026,29 @@ Object doGeneric(GraalHPyContext context, Object typeSpec, Object typeSpecParamA
20202026
}
20212027

20222028
// create the type object
2023-
PythonClass metatype = (PythonClass) getMetatype(context, typeSpecParamArray, ptrLib, castToJavaIntNode, callHelperFunctionNode, hPyAsPythonObjectNode);
2029+
Object metatype = getMetatype(context, typeSpecParamArray, ptrLib, castToJavaIntNode, callHelperFunctionNode, hPyAsPythonObjectNode);
20242030
PythonModule pythonCextModule = PythonContext.get(this).lookupBuiltinModule(PythonCextBuiltins.PYTHON_CEXT);
20252031
PythonClass newType = (PythonClass) callCreateTypeNode.execute(null, pythonCextModule, "PyTruffle_CreateType",
20262032
names[1], bases, namespace, metatype != null ? metatype : PythonBuiltinClassType.PythonClass);
20272033
// allocate additional memory for the metatype and set it
2028-
if (metatype != null) {
2034+
long metaBasicSize = 0;
2035+
Object destroyFunc = null;
2036+
if (metatype instanceof PythonClass) {
20292037
// get basicsize of metatype and allocate it into
20302038
// GraalHPyDef.OBJECT_HPY_NATIVE_SPACE
2031-
long basicSize = metatype.basicSize;
2032-
Object dataPtr = callMallocNode.call(context, GraalHPyNativeSymbol.GRAAL_HPY_CALLOC, basicSize, 1L);
2033-
writeAttributeToObjectNode.execute(newType, GraalHPyDef.OBJECT_HPY_NATIVE_SPACE, dataPtr);
2034-
Object destroyFunc = metatype.hpyDestroyFunc;
2039+
PythonClass metaclass = (PythonClass) metatype;
2040+
metaBasicSize = metaclass.basicSize;
2041+
destroyFunc = metaclass.hpyDestroyFunc;
2042+
} else if (metatype instanceof PythonNativeClass) {
2043+
// This path is implemented only for completeness,
2044+
// but is not expected to happen often, hence the
2045+
// uncached nodes, no profiling and potential leak
2046+
Object sizeObj = getMetaSizeNode.execute(metatype, NativeMember.TP_BASICSIZE);
2047+
metaBasicSize = CastToJavaLongExactNode.getUncached().execute(sizeObj);
2048+
}
2049+
Object dataPtr = callMallocNode.call(context, GraalHPyNativeSymbol.GRAAL_HPY_CALLOC, metaBasicSize, 1L);
2050+
writeAttributeToObjectNode.execute(newType, GraalHPyDef.OBJECT_HPY_NATIVE_SPACE, dataPtr);
2051+
if (destroyFunc != null) {
20352052
context.createHandleReference(newType, dataPtr, destroyFunc != PNone.NO_VALUE ? destroyFunc : null);
20362053
}
20372054

graalpython/lib-graalpython/modules/hpy/test/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def skip_nfi(hpy_abi):
9696
pytest.skip()
9797

9898

99+
@pytest.fixture()
100+
def skip_cpython_abi(hpy_abi):
101+
# skip all tests in this class for CPython ABI mode
102+
if hpy_abi == 'cpython':
103+
pytest.skip()
104+
105+
99106
@pytest.fixture(scope="session")
100107
def fatal_exit_code(request):
101108
import sys

graalpython/lib-graalpython/modules/hpy/test/test_hpytype_legacy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def _metaclass_test(self, metatype_setup_code):
303303
mod.set_member(d)
304304
assert d.member == 123614
305305

306+
@pytest.mark.usefixtures('skip_cpython_abi')
306307
def test_metatype_as_legacy_static_type(self):
307308
self._metaclass_test("""
308309
static PyTypeObject DummyMetaType = {
@@ -324,6 +325,7 @@ def test_metatype_as_legacy_static_type(self):
324325
# Ideally this test should be in the super class and parametrized by
325326
# @IS_LEGACY for both the metatype and the class itself, but we cannot
326327
# do pure HPy types in this scenario - see the comment above.
328+
@pytest.mark.usefixtures('skip_cpython_abi')
327329
def test_metatype_as_legacy_hpy_type(self):
328330
self._metaclass_test("""
329331
static HPyType_Spec DummyMeta_spec = {

0 commit comments

Comments
 (0)