Skip to content

Commit 2009e34

Browse files
committed
Migrate more usages of getDict
1 parent 64905ae commit 2009e34

29 files changed

+251
-354
lines changed

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
235235
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
236236
import com.oracle.graal.python.nodes.object.GetClassNode;
237-
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
237+
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
238238
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
239239
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
240240
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -266,7 +266,6 @@
266266
import com.oracle.truffle.api.dsl.ReportPolymorphism.Megamorphic;
267267
import com.oracle.truffle.api.dsl.Specialization;
268268
import com.oracle.truffle.api.frame.VirtualFrame;
269-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
270269
import com.oracle.truffle.api.library.CachedLibrary;
271270
import com.oracle.truffle.api.nodes.UnexpectedResultException;
272271
import com.oracle.truffle.api.object.HiddenKey;
@@ -2576,20 +2575,9 @@ private void copyDictSlots(PythonClass pythonClass, PDict namespace, PythonObjec
25762575
} else if (key instanceof String && typeDict == null) {
25772576
pythonClass.setAttribute(key, value);
25782577
} else {
2579-
// DynamicObjectStorage ignores non-string keys
2580-
typeDict = GetDictIfExistsNode.getUncached().execute(pythonClass);
2581-
if (typeDict == null) {
2582-
// 1.) create DynamicObjectStorage based dict from pythonClass
2583-
typeDict = PythonObjectFactory.getUncached().createDictFixedStorage(pythonClass);
2584-
try {
2585-
lib.setDict(pythonClass, typeDict);
2586-
} catch (UnsupportedMessageException ex) {
2587-
CompilerDirectives.transferToInterpreterAndInvalidate();
2588-
throw new IllegalStateException("can't set dict into " + pythonClass, ex);
2589-
}
2590-
}
2591-
// 2.) writing a non string key converts DynamicObjectStorage to
2592-
// EconomicMapStorage
2578+
// Creates DynamicObjectStorage which ignores non-string keys
2579+
typeDict = GetOrCreateDictNode.getUncached().execute(pythonClass);
2580+
// Writing a non string key converts DynamicObjectStorage to EconomicMapStorage
25932581
HashingStorage updatedStore = hashingStorageLib.setItem(typeDict.getDictStorage(), key, value);
25942582
typeDict.setDictStorage(updatedStore);
25952583
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCArrayTypeBuiltins.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@
6767
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
6868
import com.oracle.graal.python.builtins.objects.dict.PDict;
6969
import com.oracle.graal.python.builtins.objects.function.PKeyword;
70+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7071
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
7172
import com.oracle.graal.python.nodes.SpecialMethodNames;
7273
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
7374
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7475
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
75-
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
76+
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
7677
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
78+
import com.oracle.graal.python.nodes.object.SetDictNode;
7779
import com.oracle.graal.python.runtime.exception.PException;
7880
import com.oracle.truffle.api.dsl.Cached;
7981
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -105,7 +107,8 @@ Object PyCArrayType_new(VirtualFrame frame, Object type, Object[] args, PKeyword
105107
@Cached IsBuiltinClassProfile profile,
106108
@Cached PyNumberAsSizeNode asSizeNode,
107109
@Cached TypeNode typeNew,
108-
@Cached GetOrCreateDictNode getDict,
110+
@Cached GetDictIfExistsNode getDict,
111+
@Cached SetDictNode setDict,
109112
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
110113
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
111114
/*
@@ -179,7 +182,11 @@ Object PyCArrayType_new(VirtualFrame frame, Object type, Object[] args, PKeyword
179182

180183
/* replace the class dict by our updated spam dict */
181184
PDict resDict = getDict.execute(result);
185+
if (resDict == null) {
186+
resDict = factory().createDictFixedStorage((PythonObject) result);
187+
}
182188
stgdict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), stgdict.getDictStorage()));
189+
setDict.execute((PythonObject) result, stgdict);
183190

184191
/*
185192
* Special case for character arrays. A permanent annoyance: char arrays are also

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCFuncPtrTypeBuiltins.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalObjectArrayNode;
6565
import com.oracle.graal.python.builtins.objects.dict.PDict;
6666
import com.oracle.graal.python.builtins.objects.function.PKeyword;
67+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
6768
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6869
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6970
import com.oracle.graal.python.nodes.PGuards;
@@ -72,7 +73,8 @@
7273
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7374
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7475
import com.oracle.graal.python.nodes.object.GetClassNode;
75-
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
76+
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
77+
import com.oracle.graal.python.nodes.object.SetDictNode;
7678
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
7779
import com.oracle.truffle.api.dsl.Cached;
7880
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -112,7 +114,8 @@ Object PyCFuncPtrType_new(VirtualFrame frame, Object type, Object[] args, PKeywo
112114
@Cached CastToJavaIntExactNode asNumber,
113115
@Cached LookupAttributeInMRONode.Dynamic lookupAttr,
114116
@Cached GetInternalObjectArrayNode getArray,
115-
@Cached GetOrCreateDictNode getDict,
117+
@Cached GetDictIfExistsNode getDict,
118+
@Cached SetDictNode setDict,
116119
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
117120
@CachedLibrary(limit = "1") HashingStorageLibrary hlib) {
118121
StgDictObject stgdict = factory().createStgDictObject(PythonBuiltinClassType.StgDict);
@@ -132,7 +135,11 @@ Object PyCFuncPtrType_new(VirtualFrame frame, Object type, Object[] args, PKeywo
132135

133136
/* replace the class dict by our updated storage dict */
134137
PDict resDict = getDict.execute(result);
138+
if (resDict == null) {
139+
resDict = factory().createDictFixedStorage((PythonObject) result);
140+
}
135141
stgdict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), stgdict.getDictStorage()));
142+
setDict.execute((PythonObject) result, stgdict);
136143
stgdict.align = FieldDesc.P.pffi_type.alignment;
137144
stgdict.length = 1;
138145
stgdict.size = StgDictObject.VOID_PTR_SIZE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCPointerTypeBuiltins.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@
6969
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
7070
import com.oracle.graal.python.builtins.objects.dict.PDict;
7171
import com.oracle.graal.python.builtins.objects.function.PKeyword;
72+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7273
import com.oracle.graal.python.builtins.objects.str.PString;
7374
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
7475
import com.oracle.graal.python.nodes.PRaiseNode;
7576
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7677
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7778
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
78-
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
79+
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
80+
import com.oracle.graal.python.nodes.object.SetDictNode;
7981
import com.oracle.graal.python.nodes.util.CastToJavaBooleanNode;
8082
import com.oracle.graal.python.runtime.exception.PythonErrorType;
8183
import com.oracle.truffle.api.dsl.Cached;
@@ -105,7 +107,8 @@ protected abstract static class PyCPointerTypeNewNode extends PythonBuiltinNode
105107
@Specialization
106108
protected Object PyCPointerType_new(VirtualFrame frame, Object type, Object[] args, PKeyword[] kwds,
107109
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
108-
@Cached GetOrCreateDictNode getDict,
110+
@Cached GetDictIfExistsNode getDict,
111+
@Cached SetDictNode setDict,
109112
@Cached IsTypeNode isTypeNode,
110113
@Cached TypeNode newType,
111114
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
@@ -146,7 +149,11 @@ protected Object PyCPointerType_new(VirtualFrame frame, Object type, Object[] ar
146149

147150
/* replace the class dict by our updated spam dict */
148151
PDict resDict = getDict.execute(result);
152+
if (resDict == null) {
153+
resDict = factory().createDictFixedStorage((PythonObject) result);
154+
}
149155
stgdict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), stgdict.getDictStorage()));
156+
setDict.execute((PythonObject) result, stgdict);
150157

151158
return result;
152159
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCSimpleTypeBuiltins.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
7575
import com.oracle.graal.python.builtins.objects.dict.PDict;
7676
import com.oracle.graal.python.builtins.objects.function.PKeyword;
77+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7778
import com.oracle.graal.python.builtins.objects.str.PString;
7879
import com.oracle.graal.python.builtins.objects.str.StringNodes.InternStringNode;
7980
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetBaseClassNode;
@@ -83,7 +84,8 @@
8384
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8485
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8586
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
86-
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
87+
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
88+
import com.oracle.graal.python.nodes.object.SetDictNode;
8789
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
8890
import com.oracle.graal.python.runtime.exception.PException;
8991
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -119,7 +121,8 @@ protected boolean isStruct() {
119121
Object PyCSimpleType_new(VirtualFrame frame, Object type, Object[] args, PKeyword[] kwds,
120122
@Cached TypeNode typeNew,
121123
@Cached InternStringNode internStringNode,
122-
@Cached GetOrCreateDictNode getDict,
124+
@Cached GetDictIfExistsNode getDict,
125+
@Cached SetDictNode setDict,
123126
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
124127
@Cached("create(_type_)") LookupAttributeInMRONode lookupAttrId,
125128
@Cached GetBaseClassNode getBaseClassNode,
@@ -172,7 +175,11 @@ Object PyCSimpleType_new(VirtualFrame frame, Object type, Object[] args, PKeywor
172175

173176
/* replace the class dict by our updated spam dict */
174177
PDict resDict = getDict.execute(result);
178+
if (resDict == null) {
179+
resDict = factory().createDictFixedStorage((PythonObject) result);
180+
}
175181
stgdict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), stgdict.getDictStorage()));
182+
setDict.execute((PythonObject) result, stgdict);
176183

177184
/*
178185
* Install from_param class methods in ctypes base classes. Overrides the
@@ -208,6 +215,7 @@ Object PyCSimpleType_new(VirtualFrame frame, Object type, Object[] args, PKeywor
208215
internStringNode,
209216
toJavaStringNode,
210217
getDict,
218+
setDict,
211219
hlib,
212220
factory());
213221
StgDictObject sw_dict = pyTypeStgDictNode.execute(swapped);
@@ -227,7 +235,8 @@ static Object CreateSwappedType(VirtualFrame frame, Object type, Object[] args,
227235
TypeNode typeNew,
228236
InternStringNode internStringNode,
229237
CastToJavaStringNode toString,
230-
GetOrCreateDictNode getDict,
238+
GetDictIfExistsNode getDict,
239+
SetDictNode setDict,
231240
HashingStorageLibrary hlib,
232241
PythonObjectFactory factory) {
233242
int argsLen = args.length;
@@ -255,7 +264,11 @@ static Object CreateSwappedType(VirtualFrame frame, Object type, Object[] args,
255264

256265
/* replace the class dict by our updated spam dict */
257266
PDict resDict = getDict.execute(result);
267+
if (resDict == null) {
268+
resDict = factory.createDictFixedStorage((PythonObject) result);
269+
}
258270
stgdict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), stgdict.getDictStorage()));
271+
setDict.execute((PythonObject) result, stgdict);
259272

260273
return result;
261274
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/StructUnionTypeBuiltins.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@
9292
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
9393
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
9494
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
95+
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
9596
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
97+
import com.oracle.graal.python.nodes.object.SetDictNode;
9698
import com.oracle.graal.python.nodes.subscript.GetItemNode;
9799
import com.oracle.graal.python.runtime.exception.PException;
98100
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
99101
import com.oracle.graal.python.util.PythonUtils;
100-
import com.oracle.truffle.api.CompilerDirectives;
101102
import com.oracle.truffle.api.dsl.Cached;
102103
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
103104
import com.oracle.truffle.api.dsl.ImportStatic;
104105
import com.oracle.truffle.api.dsl.NodeFactory;
105106
import com.oracle.truffle.api.dsl.Specialization;
106107
import com.oracle.truffle.api.frame.VirtualFrame;
107-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
108108
import com.oracle.truffle.api.library.CachedLibrary;
109109

110110
@CoreFunctions(extendClasses = {
@@ -141,14 +141,16 @@ protected Object StructUnionTypeNew(VirtualFrame frame, Object type, Object[] ar
141141
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
142142
@Cached TypeNode typeNew,
143143
@Cached PyTypeStgDictNode pyTypeStgDictNode,
144+
@Cached GetDictIfExistsNode getDict,
145+
@Cached SetDictNode setDict,
144146
@Cached GetBaseClassNode getBaseClassNode,
145147
@Cached("create(_fields_)") SetAttributeNode setFieldsAttributeNode) {
146148
/*
147149
* create the new instance (which is a class, since we are a metatype!)
148150
*/
149151
Object result = typeNew.execute(frame, type, args[0], args[1], args[2], kwds);
150152

151-
PDict resDict = lib.getDict(result);
153+
PDict resDict = getDict.execute(result);
152154
if (resDict == null) {
153155
resDict = factory().createDictFixedStorage((PythonObject) result);
154156
}
@@ -166,11 +168,7 @@ protected Object StructUnionTypeNew(VirtualFrame frame, Object type, Object[] ar
166168
* requirements of the instances
167169
*/
168170
dict.setDictStorage(hlib.addAllToOther(resDict.getDictStorage(), dict.getDictStorage()));
169-
try {
170-
lib.setDict(result, dict);
171-
} catch (UnsupportedMessageException e) {
172-
throw CompilerDirectives.shouldNotReachHere(e);
173-
}
171+
setDict.execute((PythonObject) result, dict);
174172
dict.format = "B";
175173

176174
dict.paramfunc = CArgObjectBuiltins.StructUnionTypeParamFunc;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BytesIOBuiltins.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import static com.oracle.graal.python.builtins.modules.io.IONodes.WRITABLE;
6565
import static com.oracle.graal.python.builtins.modules.io.IONodes.WRITE;
6666
import static com.oracle.graal.python.builtins.modules.io.IONodes.WRITELINES;
67-
import static com.oracle.graal.python.builtins.modules.io.IONodes.getDict;
6867
import static com.oracle.graal.python.nodes.ErrorMessages.EXISTING_EXPORTS_OF_DATA_OBJECT_CANNOT_BE_RE_SIZED;
6968
import static com.oracle.graal.python.nodes.ErrorMessages.INVALID_WHENCE_D_SHOULD_BE_0_1_OR_2;
7069
import static com.oracle.graal.python.nodes.ErrorMessages.IO_CLOSED;
@@ -113,6 +112,7 @@
113112
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
114113
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
115114
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
115+
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
116116
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
117117
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
118118
import com.oracle.graal.python.runtime.exception.PException;
@@ -709,13 +709,12 @@ Object unshare(PBytesIO self,
709709
@Builtin(name = __GETSTATE__, minNumOfPositionalArgs = 1)
710710
@GenerateNodeFactory
711711
abstract static class GetStateNode extends ClosedCheckPythonUnaryBuiltinNode {
712-
@Specialization(guards = "self.hasBuf()", limit = "1")
712+
@Specialization(guards = "self.hasBuf()")
713713
Object doit(VirtualFrame frame, PBytesIO self,
714714
@Cached GetValueNode getValueNode,
715-
@CachedLibrary("self") PythonObjectLibrary libSelf) {
715+
@Cached GetOrCreateDictNode getDict) {
716716
Object initValue = getValueNode.call(frame, self);
717-
PDict dict = getDict(self, libSelf, factory());
718-
Object[] state = new Object[]{initValue, self.getPos(), dict};
717+
Object[] state = new Object[]{initValue, self.getPos(), getDict.execute(self)};
719718
return factory().createTuple(state);
720719
}
721720
}
@@ -729,8 +728,8 @@ Object doit(VirtualFrame frame, PBytesIO self, PTuple state,
729728
@Cached WriteNode writeNode,
730729
@Cached PyIndexCheckNode indexCheckNode,
731730
@Cached PyNumberAsSizeNode asSizeNode,
732-
@CachedLibrary(limit = "1") HashingStorageLibrary hlib,
733-
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
731+
@Cached GetOrCreateDictNode getDict,
732+
@CachedLibrary(limit = "1") HashingStorageLibrary hlib) {
734733
Object[] array = getArray.execute(state.getSequenceStorage());
735734
if (array.length < 3) {
736735
return notTuple(self, state);
@@ -769,7 +768,7 @@ Object doit(VirtualFrame frame, PBytesIO self, PTuple state,
769768
* Alternatively, we could replace the internal dictionary completely. However, it
770769
* seems more practical to just update it.
771770
*/
772-
PDict dict = getDict(self, lib, factory());
771+
PDict dict = getDict.execute(self);
773772
hlib.addAllToOther(((PDict) array[2]).getDictStorage(), dict.getDictStorage());
774773
}
775774
return PNone.NONE;

0 commit comments

Comments
 (0)