Skip to content

Commit 76a8307

Browse files
committed
Do not expose native members like 'tp_alloc'.
1 parent d28c9ff commit 76a8307

File tree

5 files changed

+60
-47
lines changed

5 files changed

+60
-47
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToJavaNodeFactory.ToJavaCachedNodeGen;
7575
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
7676
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PythonObjectNativeWrapper;
77+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
7778
import com.oracle.graal.python.builtins.objects.floats.PFloat;
7879
import com.oracle.graal.python.builtins.objects.function.PFunction;
7980
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -83,6 +84,7 @@
8384
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
8485
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
8586
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
87+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
8688
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
8789
import com.oracle.graal.python.nodes.PGuards;
8890
import com.oracle.graal.python.nodes.PNodeWithContext;
@@ -117,6 +119,7 @@
117119
import com.oracle.graal.python.runtime.exception.PException;
118120
import com.oracle.graal.python.runtime.exception.PythonErrorType;
119121
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
122+
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
120123
import com.oracle.truffle.api.Assumption;
121124
import com.oracle.truffle.api.CompilerDirectives;
122125
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -1893,4 +1896,51 @@ boolean doSingleContext(PythonAbstractNativeObject left, PythonAbstractNativeObj
18931896
return pointerCompareNode.execute(SpecialMethodNames.__EQ__, left, right);
18941897
}
18951898
}
1899+
1900+
/**
1901+
* Use this node to lookup a native type member like {@code tp_alloc}.<br>
1902+
* <p>
1903+
* This node basically implements the native member inheritance that is done by
1904+
* {@code inherit_special} or other code in {@code PyType_Ready}.
1905+
* </p>
1906+
* <p>
1907+
* Since it may be that a managed types needs to emulate such members but there is no
1908+
* corresponding Python attribute (e.g. {@code tp_alloc}), such members are stored as hidden
1909+
* keys on the managed type. However, the MRO may contain native types and in this case, we need
1910+
* to access the native member.
1911+
* </p>
1912+
*/
1913+
@GenerateUncached
1914+
public abstract static class LookupNativeMemberInMRONode extends Node {
1915+
1916+
public abstract Object execute(PythonAbstractClass cls, String nativeMemberName, Object managedMemberName);
1917+
1918+
@Specialization
1919+
Object doSingleContext(PythonAbstractClass cls, String nativeMemberName, Object managedMemberName,
1920+
@Cached GetMroStorageNode getMroNode,
1921+
@Cached SequenceStorageNodes.LenNode lenNode,
1922+
@Cached SequenceStorageNodes.GetItemDynamicNode getItemNode,
1923+
@Cached("createForceType()") ReadAttributeFromObjectNode readAttrNode,
1924+
@Cached GetTypeMemberNode getTypeMemberNode) {
1925+
1926+
MroSequenceStorage mroStorage = getMroNode.execute(cls);
1927+
int n = lenNode.execute(mroStorage);
1928+
1929+
for (int i = 0; i < n; i++) {
1930+
PythonAbstractClass mroCls = (PythonAbstractClass) getItemNode.execute(mroStorage, i);
1931+
Object result = PNone.NO_VALUE;
1932+
if (PGuards.isManagedClass(mroCls)) {
1933+
result = readAttrNode.execute(mroCls, managedMemberName);
1934+
} else {
1935+
assert PGuards.isNativeClass(mroCls) : "invalid class inheritance structure; expected native class";
1936+
result = getTypeMemberNode.execute(mroCls, nativeMemberName);
1937+
}
1938+
if (result != PNone.NO_VALUE) {
1939+
return result;
1940+
}
1941+
}
1942+
1943+
return PNone.NO_VALUE;
1944+
}
1945+
}
18961946
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__ITEMSIZE__;
5555
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__WEAKLISTOFFSET__;
5656
import static com.oracle.graal.python.nodes.SpecialMethodNames.RICHCMP;
57-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ALLOC__;
5857
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
5958
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
6059
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
@@ -335,9 +334,9 @@ Object doTpBase(PythonManagedClass object, @SuppressWarnings("unused") String ke
335334

336335
@Specialization(guards = "eq(TP_ALLOC, key)")
337336
Object doTpAlloc(PythonManagedClass object, @SuppressWarnings("unused") String key,
338-
@Cached LookupAttributeInMRONode.Dynamic getAllocNode,
337+
@Cached CExtNodes.LookupNativeMemberInMRONode lookupNativeMemberNode,
339338
@Shared("toSulongNode") @Cached CExtNodes.ToSulongNode toSulongNode) {
340-
Object result = getAllocNode.execute(object, __ALLOC__);
339+
Object result = lookupNativeMemberNode.execute(object, NativeMemberNames.TP_ALLOC, TypeBuiltins.TYPE_ALLOC);
341340
return toSulongNode.execute(result);
342341
}
343342

@@ -896,13 +895,8 @@ long doTpBasicsize(PythonAbstractClass object, @SuppressWarnings("unused") Strin
896895
@Specialization(guards = "eq(TP_ALLOC, key)")
897896
Object doTpAlloc(PythonAbstractClass object, @SuppressWarnings("unused") String key, Object allocFunc,
898897
@Cached WriteAttributeToObjectNode writeAttrNode,
899-
@Cached IsBuiltinClassProfile profile,
900898
@Cached CExtNodes.AsPythonObjectNode asPythonObjectNode) {
901-
if (profile.profileClass(object, PythonBuiltinClassType.PythonClass)) {
902-
writeAttrNode.execute(object, TypeBuiltins.TYPE_ALLOC, asPythonObjectNode.execute(allocFunc));
903-
} else {
904-
writeAttrNode.execute(object, SpecialMethodNames.__ALLOC__, asPythonObjectNode.execute(allocFunc));
905-
}
899+
writeAttrNode.execute(object, TypeBuiltins.TYPE_ALLOC, asPythonObjectNode.execute(allocFunc));
906900
return allocFunc;
907901
}
908902

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public abstract class NativeCAPISymbols {
6464
public static final String FUN_GET_TP_BASES = "get_tp_bases";
6565
public static final String FUN_GET_TP_NAME = "get_tp_name";
6666
public static final String FUN_GET_TP_MRO = "get_tp_mro";
67+
public static final String FUN_GET_TP_ALLOC = "get_tp_alloc";
6768
public static final String FUN_GET_TP_SUBCLASSES = "get_tp_subclasses";
6869
public static final String FUN_GET_TP_DICTOFFSET = "get_tp_dictoffset";
6970
public static final String FUN_GET_TP_BASICSIZE = "get_tp_basicsize";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
136136
import com.oracle.graal.python.runtime.sequence.storage.ListSequenceStorage;
137137
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
138+
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
138139
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
139140
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
140141
import com.oracle.graal.python.runtime.sequence.storage.RangeSequenceStorage;
@@ -774,6 +775,11 @@ protected Object doObject(ObjectSequenceStorage storage, int idx) {
774775
return storage.getItemNormalized(idx);
775776
}
776777

778+
@Specialization
779+
protected Object doMro(MroSequenceStorage storage, int idx) {
780+
return storage.getItemNormalized(idx);
781+
}
782+
777783
@Specialization(guards = "isObject(getElementType, storage)", limit = "1")
778784
protected Object doNativeObject(NativeSequenceStorage storage, int idx,
779785
@CachedLibrary("storage.getPtr()") InteropLibrary lib,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeBuiltins.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -889,42 +889,4 @@ Object flags(PythonAbstractClass self) {
889889
return getFlagsNode.execute(self);
890890
}
891891
}
892-
893-
@Builtin(name = __ALLOC__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
894-
abstract static class AllocNode extends AbstractSlotNode {
895-
896-
@Specialization(guards = "isNoValue(value)")
897-
Object getAlloc(PythonManagedClass cls, @SuppressWarnings("unused") PNone value,
898-
@Cached("create()") IsBuiltinClassProfile profile,
899-
@Cached("create()") ReadAttributeFromObjectNode getName) {
900-
// recursion anchor; since the metaclass of 'type' is 'type'
901-
if (profile.profileClass(cls, PythonBuiltinClassType.PythonClass)) {
902-
return getName.execute(cls, TYPE_ALLOC);
903-
}
904-
return getName.execute(cls, __ALLOC__);
905-
}
906-
907-
@Specialization(guards = "!isNoValue(value)")
908-
Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarnings("unused") Object value) {
909-
throw raise(PythonErrorType.RuntimeError, "can't set attributes of built-in/extension 'type'");
910-
}
911-
912-
@Specialization(guards = {"!isNoValue(value)", "!isPythonBuiltinClass(cls)"})
913-
Object setName(PythonClass cls, Object value,
914-
@Cached WriteAttributeToObjectNode setName) {
915-
return setName.execute(cls, __ALLOC__, value);
916-
}
917-
918-
@Specialization(guards = "isNoValue(value)")
919-
Object getNative(PythonNativeClass cls, @SuppressWarnings("unused") PNone value,
920-
@Cached("create()") GetTypeMemberNode getTpAllocNode) {
921-
return getTpAllocNode.execute(cls, NativeMemberNames.TP_ALLOC);
922-
}
923-
924-
@Specialization(guards = "!isNoValue(value)")
925-
Object setNative(@SuppressWarnings("unused") PythonAbstractNativeObject cls, @SuppressWarnings("unused") Object value) {
926-
throw raise(PythonErrorType.RuntimeError, "can't set attributes of native type");
927-
}
928-
}
929-
930892
}

0 commit comments

Comments
 (0)