Skip to content

Commit a255a16

Browse files
committed
Cleanup the API of Write/Read attribute nodes
1 parent 9b11c77 commit a255a16

20 files changed

+170
-210
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ private void initializeImportlib() {
949949
LOGGER.log(Level.FINE, () -> "initializing zipimport failed");
950950
} else {
951951
LOGGER.log(Level.FINE, () -> "# installing zipimport hook");
952-
Object zipimport = null;
952+
PythonModule zipimport = null;
953953
try {
954954
zipimport = AbstractImportNode.importModule(toTruffleStringUncached("zipimport"));
955955
} catch (PException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public BuiltinDescr(Supplier<PythonBuiltinBaseNode> nodeSupplier, Class<?> nodeC
191191

192192
@TruffleBoundary
193193
static PTuple codecsInfo(PythonModule self, TruffleString encoding, PythonContext context, PythonObjectFactory factory) {
194-
PythonModule codecsModule = (PythonModule) AbstractImportNode.importModule(T_CODECS);
194+
PythonModule codecsModule = AbstractImportNode.importModule(T_CODECS);
195195
CodecsTruffleModuleBuiltins codecsTruffleBuiltins = (CodecsTruffleModuleBuiltins) self.getBuiltins();
196196
if (self.getAttribute(T_TRUFFLE_CODEC) instanceof PNone) {
197197
initCodecClasses(self, codecsModule, context, factory);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ static void doNativeClass(PythonNativeClass object, TruffleString key, Object va
10101010
@Specialization(guards = {"!isPythonBuiltinClass(object)"})
10111011
static void doObject(PythonObject object, TruffleString key, Object value,
10121012
@Exclusive @Cached(inline = false) WriteAttributeToDynamicObjectNode writeAttrToDynamicObjectNode) {
1013-
writeAttrToDynamicObjectNode.execute(object.getStorage(), key, value);
1013+
writeAttrToDynamicObjectNode.execute(object, key, value);
10141014
}
10151015
}
10161016

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,20 @@ static Object set(PythonAbstractObject object, Object bufferProcs,
830830
@CApiBuiltin(ret = Void, args = {PyTypeObject, PyObject}, call = Ignored)
831831
abstract static class Py_set_PyTypeObject_tp_dict extends CApiBinaryBuiltinNode {
832832

833+
private static TruffleString castKey(Node inliningTarget, CastToTruffleStringNode castNode, Object value) {
834+
try {
835+
return castNode.execute(inliningTarget, value);
836+
} catch (CannotCastException ex) {
837+
throw CompilerDirectives.shouldNotReachHere(ex);
838+
}
839+
}
840+
833841
@Specialization
834842
static Object doTpDict(PythonManagedClass object, Object value,
835843
@Bind("this") Node inliningTarget,
836844
@Cached GetDictIfExistsNode getDict,
837845
@Cached SetDictNode setDict,
846+
@Cached CastToTruffleStringNode castNode,
838847
@Cached WriteAttributeToObjectNode writeAttrNode,
839848
@Cached HashingStorageGetIterator getIterator,
840849
@Cached HashingStorageIteratorNext itNext,
@@ -845,7 +854,7 @@ static Object doTpDict(PythonManagedClass object, Object value,
845854
HashingStorage storage = dict.getDictStorage();
846855
HashingStorageIterator it = getIterator.execute(inliningTarget, storage);
847856
while (itNext.execute(inliningTarget, storage, it)) {
848-
writeAttrNode.execute(object, itKey.execute(inliningTarget, storage, it), itValue.execute(inliningTarget, storage, it));
857+
writeAttrNode.execute(object, castKey(inliningTarget, castNode, itKey.execute(inliningTarget, storage, it)), itValue.execute(inliningTarget, storage, it));
849858
}
850859
PDict existing = getDict.execute(object);
851860
if (existing != null) {

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___NAME__;
5757
import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY;
5858
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
59+
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
60+
import static com.oracle.truffle.api.CompilerDirectives.transferToInterpreter;
5961

6062
import com.oracle.graal.python.PythonLanguage;
6163
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApi7BuiltinNode;
@@ -96,6 +98,8 @@
9698
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
9799
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
98100
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
101+
import com.oracle.graal.python.nodes.util.CannotCastException;
102+
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
99103
import com.oracle.graal.python.runtime.PythonContext;
100104
import com.oracle.graal.python.runtime.PythonOptions;
101105
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -130,8 +134,16 @@ public final class PythonCextTypeBuiltins {
130134
abstract static class _PyType_Lookup extends CApiBinaryBuiltinNode {
131135
@Specialization
132136
Object doGeneric(Object type, Object name,
137+
@Bind("this") Node inliningTarget,
138+
@Cached CastToTruffleStringNode castToTruffleStringNode,
133139
@Cached LookupAttributeInMRONode.Dynamic lookupAttributeInMRONode) {
134-
Object result = lookupAttributeInMRONode.execute(type, name);
140+
TruffleString key;
141+
try {
142+
key = castToTruffleStringNode.execute(inliningTarget, castToTruffleStringNode);
143+
} catch (CannotCastException e) {
144+
throw shouldNotReachHere();
145+
}
146+
Object result = lookupAttributeInMRONode.execute(type, key);
135147
if (result == PNone.NO_VALUE) {
136148
return getNativeNull();
137149
}
@@ -303,7 +315,11 @@ abstract static class PyTruffleType_AddSlot extends CApi7BuiltinNode {
303315
static int addSlot(Object clazz, PDict tpDict, TruffleString memberName, Object cfunc, int flags, int wrapper, Object memberDoc) {
304316
// create wrapper descriptor
305317
Object wrapperDescriptor = CreateFunctionNode.executeUncached(memberName, cfunc, wrapper, clazz, flags);
306-
WriteAttributeToDynamicObjectNode.getUncached().execute(wrapperDescriptor, SpecialAttributeNames.T___DOC__, memberDoc);
318+
if (!(wrapperDescriptor instanceof PythonObject)) {
319+
transferToInterpreter();
320+
throw shouldNotReachHere("Unexpected class of wrapperDescriptor: " + wrapperDescriptor.getClass());
321+
}
322+
WriteAttributeToDynamicObjectNode.getUncached().execute((PythonObject) wrapperDescriptor, SpecialAttributeNames.T___DOC__, memberDoc);
307323

308324
// add wrapper descriptor to tp_dict
309325
PyDictSetDefault.executeUncached(tpDict, memberName, wrapperDescriptor);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/hashlib/HashlibModuleBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void initialize(Python3Core core) {
167167
super.initialize(core);
168168
}
169169

170-
private void addDigestAlias(PythonModule self, Object mod, ReadAttributeFromDynamicObjectNode readNode, EconomicMapStorage storage, String digest) {
170+
private void addDigestAlias(PythonModule self, PythonModule mod, ReadAttributeFromDynamicObjectNode readNode, EconomicMapStorage storage, String digest) {
171171
TruffleString tsDigest = toTruffleStringUncached(digest);
172172
Object function = readNode.execute(mod, tsDigest);
173173
if (function != NO_VALUE) {
@@ -182,10 +182,10 @@ public void postInitialize(Python3Core core) {
182182
PythonModule self = core.lookupBuiltinModule(T_HASHLIB);
183183
ReadAttributeFromDynamicObjectNode readNode = ReadAttributeFromDynamicObjectNode.getUncached();
184184
EconomicMapStorage storage = (EconomicMapStorage) HiddenAttr.ReadNode.executeUncached(self, HiddenAttr.ORIGINAL_CONSTRUCTORS, NO_VALUE);
185-
Object sha3module = AbstractImportNode.importModule(T_SHA3);
185+
PythonModule sha3module = AbstractImportNode.importModule(T_SHA3);
186186
for (int i = 0; i < DIGEST_ALIASES.length; i += 2) {
187187
String module = DIGEST_ALIASES[i + 1];
188-
Object mod = module.equals(J_SHA3) ? sha3module : core.lookupBuiltinModule(toTruffleStringUncached(module));
188+
PythonModule mod = module.equals(J_SHA3) ? sha3module : core.lookupBuiltinModule(toTruffleStringUncached(module));
189189
addDigestAlias(self, mod, readNode, storage, DIGEST_ALIASES[i]);
190190
}
191191
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ public static long lookupNativeI64MemberInMRO(Object cls, CFields nativeMemberNa
921921
if (managedMemberName instanceof HiddenAttr ha) {
922922
attr = HiddenAttr.ReadNode.executeUncached((PythonAbstractObject) mroCls, ha, NO_VALUE);
923923
} else {
924-
attr = ReadAttributeFromObjectNode.getUncachedForceType().execute(mroCls, managedMemberName);
924+
attr = ReadAttributeFromObjectNode.getUncachedForceType().execute(mroCls, CompilerDirectives.castExact(managedMemberName, TruffleString.class));
925925
}
926926
if (attr != NO_VALUE) {
927927
return PyNumberAsSizeNode.executeExactUncached(attr);
@@ -1859,7 +1859,7 @@ static PBuiltinFunction doIt(Node inliningTarget, Object methodDef, int element,
18591859

18601860
// write doc string; we need to directly write to the storage otherwise it is disallowed
18611861
// writing to builtin types.
1862-
writeAttributeToDynamicObjectNode.execute(function.getStorage(), SpecialAttributeNames.T___DOC__, methodDoc);
1862+
writeAttributeToDynamicObjectNode.execute(function, SpecialAttributeNames.T___DOC__, methodDoc);
18631863

18641864
return function;
18651865
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ static Object doGeneric(GraalHPyContext context, TruffleString mName, Object spe
531531
break;
532532
}
533533
PBuiltinMethod method = factory.createBuiltinMethod(module, fun);
534-
writeAttrToMethodNode.execute(method.getStorage(), SpecialAttributeNames.T___MODULE__, mName);
534+
writeAttrToMethodNode.execute(method, SpecialAttributeNames.T___MODULE__, mName);
535535
writeAttrNode.execute(module, fun.getName(), method);
536536
}
537537
}
@@ -698,7 +698,7 @@ static PBuiltinFunction doIt(GraalHPyContext context, Object enclosingType, Obje
698698

699699
// write doc string; we need to directly write to the storage otherwise it is
700700
// disallowed writing to builtin types.
701-
writeAttributeToDynamicObjectNode.execute(function.getStorage(), SpecialAttributeNames.T___DOC__, methodDoc);
701+
writeAttributeToDynamicObjectNode.execute(function, SpecialAttributeNames.T___DOC__, methodDoc);
702702

703703
return function;
704704
}
@@ -801,12 +801,13 @@ static final class HPyProperty {
801801
final HPyProperty next;
802802

803803
HPyProperty(Object key, Object value, HPyProperty next) {
804+
assert key instanceof TruffleString || key instanceof HiddenAttr;
804805
this.key = key;
805806
this.value = value;
806807
this.next = next;
807808
}
808809

809-
HPyProperty(Object key, Object value) {
810+
HPyProperty(TruffleString key, Object value) {
810811
this(key, value, null);
811812
}
812813

@@ -850,8 +851,8 @@ static Object doHiddenAttr(Node inliningTarget, PythonAbstractObject receiver, H
850851
return readNode.execute(inliningTarget, receiver, key, PNone.NO_VALUE);
851852
}
852853

853-
@Fallback
854-
static Object doOther(Object receiver, Object key,
854+
@Specialization
855+
static Object doOther(Object receiver, TruffleString key,
855856
@Cached(inline = false) ReadAttributeFromObjectNode readAttributeFromObjectNode) {
856857
return readAttributeFromObjectNode.execute(receiver, key);
857858
}
@@ -861,6 +862,7 @@ static Object doOther(Object receiver, Object key,
861862
@GenerateCached(false)
862863
@GenerateUncached
863864
abstract static class WritePropertyNode extends Node {
865+
// key comes from HPyProperty#key which is either TruffleString or HiddenAttr
864866
abstract void execute(Node inliningTarget, Object receiver, Object key, Object value);
865867

866868
@Specialization
@@ -869,8 +871,8 @@ static void doHiddenAttr(Node inliningTarget, PythonAbstractObject receiver, Hid
869871
writeNode.execute(inliningTarget, receiver, key, value);
870872
}
871873

872-
@Fallback
873-
static void doOther(Object receiver, Object key, Object value,
874+
@Specialization
875+
static void doString(Object receiver, TruffleString key, Object value,
874876
@Cached(inline = false) WriteAttributeToObjectNode writeAttributeToObjectNode) {
875877
writeAttributeToObjectNode.execute(receiver, key, value);
876878
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ private Object fullLookup(VirtualFrame frame, Node inliningTarget, Object object
458458
throw raiseNode.get(inliningTarget).raise(AttributeError, ErrorMessages.OBJ_P_HAS_NO_ATTR_S, object, key);
459459
}
460460

461-
private Object readAttribute(Object object, Object key) {
461+
private Object readAttribute(Object object, TruffleString key) {
462462
if (attrRead == null) {
463463
CompilerDirectives.transferToInterpreterAndInvalidate();
464464
attrRead = insert(ReadAttributeFromObjectNode.create());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/superobject/SuperBuiltins.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.isJavaString;
5353
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
5454
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
55+
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
5556

5657
import java.util.List;
5758

@@ -448,18 +449,18 @@ Object get(VirtualFrame frame, SuperObject self, Object attr,
448449
* We want __class__ to return the class of the super object (i.e. super, or a
449450
* subclass), not the class of su->obj.
450451
*/
451-
TruffleString stringAttr = null;
452+
TruffleString stringAttr;
452453
if (attr instanceof PString) {
453454
stringAttr = ((PString) attr).getValueUncached();
454455
} else if (isJavaString(attr)) {
455456
stringAttr = toTruffleStringUncached((String) attr);
456457
} else if (attr instanceof TruffleString) {
457458
stringAttr = (TruffleString) attr;
459+
} else {
460+
throw shouldNotReachHere();
458461
}
459-
if (stringAttr != null) {
460-
if (equalNode.execute(stringAttr, T___CLASS__, TS_ENCODING)) {
461-
return genericGetAttr(frame, self, T___CLASS__);
462-
}
462+
if (equalNode.execute(stringAttr, T___CLASS__, TS_ENCODING)) {
463+
return genericGetAttr(frame, self, T___CLASS__);
463464
}
464465

465466
// acts as a branch profile
@@ -479,12 +480,12 @@ Object get(VirtualFrame frame, SuperObject self, Object attr,
479480
}
480481
i++; /* skip su->type (if any) */
481482
if (i >= n) {
482-
return genericGetAttr(frame, self, attr);
483+
return genericGetAttr(frame, self, stringAttr);
483484
}
484485

485486
for (; i < n; i++) {
486487
PythonAbstractClass tmp = mro[i];
487-
Object res = readFromDict.execute(tmp, attr);
488+
Object res = readFromDict.execute(tmp, stringAttr);
488489
if (res != PNone.NO_VALUE) {
489490
Object get = readGet.execute(res);
490491
if (get != PNone.NO_VALUE) {
@@ -503,7 +504,7 @@ Object get(VirtualFrame frame, SuperObject self, Object attr,
503504
}
504505
}
505506

506-
return genericGetAttr(frame, self, attr);
507+
return genericGetAttr(frame, self, stringAttr);
507508
}
508509

509510
private boolean isSameType(Object execute, Object abstractPythonClass) {

0 commit comments

Comments
 (0)