Skip to content

Commit a09d543

Browse files
committed
PartialBuiltins: fix __setstate__, get test_functools to pass
1 parent 3258c05 commit a09d543

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ protected boolean atLeastOneArg(Object[] args) {
183183

184184
@Specialization(guards = {"atLeastOneArg(args)", "isPartialWithoutDict(getDict, args, lib, false)"})
185185
Object createFromPartialWoDictWoKw(Object cls, Object[] args, PKeyword[] keywords,
186-
@Cached GetDictIfExistsNode getDict,
186+
@SuppressWarnings("unused") @Cached GetDictIfExistsNode getDict,
187187
@Cached ConditionProfile hasArgsProfile,
188188
@Cached ConditionProfile hasKeywordsProfile,
189-
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
189+
@SuppressWarnings("unused") @CachedLibrary(limit = "1") HashingStorageLibrary lib) {
190190
assert args[0] instanceof PPartial;
191191
final PPartial function = (PPartial) args[0];
192192
Object[] funcArgs = getNewPartialArgs(function, args, hasArgsProfile, 1);
@@ -202,8 +202,8 @@ Object createFromPartialWoDictWoKw(Object cls, Object[] args, PKeyword[] keyword
202202
}
203203

204204
@Specialization(guards = {"atLeastOneArg(args)", "isPartialWithoutDict(getDict, args, lib, true)", "!withKeywords(keywords)"})
205-
Object createFromPartialWoDictWKw(Object cls, Object[] args, PKeyword[] keywords,
206-
@Cached GetDictIfExistsNode getDict,
205+
Object createFromPartialWoDictWKw(Object cls, Object[] args, @SuppressWarnings("unused") PKeyword[] keywords,
206+
@SuppressWarnings("unused") @Cached GetDictIfExistsNode getDict,
207207
@Cached ConditionProfile hasArgsProfile,
208208
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
209209
assert args[0] instanceof PPartial;
@@ -214,7 +214,7 @@ Object createFromPartialWoDictWKw(Object cls, Object[] args, PKeyword[] keywords
214214

215215
@Specialization(guards = {"atLeastOneArg(args)", "isPartialWithoutDict(getDict, args, lib, true)", "withKeywords(keywords)"})
216216
Object createFromPartialWoDictWKwKw(VirtualFrame frame, Object cls, Object[] args, PKeyword[] keywords,
217-
@Cached GetDictIfExistsNode getDict,
217+
@SuppressWarnings("unused") @Cached GetDictIfExistsNode getDict,
218218
@Cached ConditionProfile hasArgsProfile,
219219
@Cached HashingStorage.InitNode initNode,
220220
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
@@ -230,7 +230,7 @@ Object createFromPartialWoDictWKwKw(VirtualFrame frame, Object cls, Object[] arg
230230

231231
@Specialization(guards = {"atLeastOneArg(args)", "!isPartialWithoutDict(getDict, args)"})
232232
Object createGeneric(Object cls, Object[] args, PKeyword[] keywords,
233-
@Cached GetDictIfExistsNode getDict,
233+
@SuppressWarnings("unused") @Cached GetDictIfExistsNode getDict,
234234
@Cached ConditionProfile hasKeywordsProfile,
235235
@Cached PyCallableCheckNode callableCheckNode) {
236236
Object function = args[0];

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/partial/PPartial.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void setArgs(PTuple args, SequenceNodes.GetSequenceStorageNode storageNod
8989
public PDict getKw() {
9090
return kw;
9191
}
92-
92+
9393
public PDict getOrCreateKw(PythonObjectFactory factory) {
9494
if (kw == null) {
9595
kw = factory.createDict();
@@ -106,7 +106,7 @@ public boolean hasKw(HashingStorageLibrary lib) {
106106
return lib.length(kw.getDictStorage()) > 0;
107107
}
108108

109-
public void setKwCopy(PDict kwArgs, PythonObjectFactory factory, HashingStorageLibrary lib) {
110-
this.kw = factory.createDict(lib.copy(kwArgs.getDictStorage()));
109+
public void setKw(PDict kwArgs) {
110+
this.kw = kwArgs;
111111
}
112112
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/partial/PartialBuiltins.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5656
import com.oracle.graal.python.builtins.PythonBuiltins;
5757
import com.oracle.graal.python.builtins.objects.PNone;
58+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
5859
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
5960
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
6061
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
@@ -66,16 +67,20 @@
6667
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
6768
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6869
import com.oracle.graal.python.lib.PyCallableCheckNode;
70+
import com.oracle.graal.python.lib.PyDictCheckExactNode;
6971
import com.oracle.graal.python.lib.PyObjectReprAsJavaStringNode;
7072
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
73+
import com.oracle.graal.python.lib.PyTupleCheckExactNode;
7174
import com.oracle.graal.python.nodes.ErrorMessages;
7275
import com.oracle.graal.python.nodes.PGuards;
7376
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
77+
import com.oracle.graal.python.nodes.builtins.TupleNodes;
7478
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
7579
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7680
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7781
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7882
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
83+
import com.oracle.graal.python.nodes.object.DeleteDictNode;
7984
import com.oracle.graal.python.nodes.object.GetClassNode;
8085
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
8186
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
@@ -190,15 +195,21 @@ Object reduce(PPartial self,
190195
@Builtin(name = __SETSTATE__, minNumOfPositionalArgs = 2)
191196
@GenerateNodeFactory
192197
public abstract static class PartialSetStateNode extends PythonBinaryBuiltinNode {
198+
193199
@Specialization
194200
public Object setState(VirtualFrame frame, PPartial self, PTuple state,
195201
@Cached SetDictNode setDictNode,
202+
@Cached DeleteDictNode deleteDictNode,
196203
@Cached SequenceNodes.GetSequenceStorageNode storageNode,
197204
@Cached SequenceStorageNodes.GetInternalObjectArrayNode arrayNode,
198205
@Cached PyCallableCheckNode callableCheckNode,
206+
@Cached PyTupleCheckExactNode tupleCheckExactNode,
207+
@Cached PyDictCheckExactNode dictCheckExactNode,
199208
@Cached TupleBuiltins.GetItemNode getItemNode,
209+
@Cached TupleNodes.ConstructTupleNode constructTupleNode,
200210
@Cached SequenceStorageNodes.LenNode lenNode,
201-
@CachedLibrary(limit = "3") HashingStorageLibrary lib) {
211+
@Cached HashingCollectionNodes.GetHashingStorageNode getHashingStorageNode,
212+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
202213
if (lenNode.execute(state.getSequenceStorage()) != 4) {
203214
throw raise(PythonBuiltinClassType.TypeError, INVALID_PARTIAL_STATE);
204215
}
@@ -216,13 +227,27 @@ public Object setState(VirtualFrame frame, PPartial self, PTuple state,
216227

217228
self.setFn(function);
218229

219-
assert fnArgs instanceof PTuple;
220-
self.setArgs((PTuple) fnArgs, storageNode, arrayNode);
230+
final PTuple fnArgsTuple;
231+
if (!tupleCheckExactNode.execute(fnArgs)) {
232+
fnArgsTuple = constructTupleNode.execute(frame, fnArgs);
233+
} else {
234+
fnArgsTuple = (PTuple) fnArgs;
235+
}
236+
self.setArgs(fnArgsTuple, storageNode, arrayNode);
221237

222-
assert fnKwargs instanceof PDict;
223-
self.setKwCopy((PDict) fnKwargs, factory(), lib);
238+
final PDict fnKwargsDict;
239+
if (fnKwargs == PNone.NONE) {
240+
fnKwargsDict = factory().createDict();
241+
} else if (!dictCheckExactNode.execute(fnKwargs)) {
242+
fnKwargsDict = factory().createDict(lib.copy(getHashingStorageNode.execute(frame, fnKwargs)));
243+
} else {
244+
fnKwargsDict = (PDict) fnKwargs;
245+
}
246+
self.setKw(fnKwargsDict);
224247

225-
if (dict != PNone.NONE) {
248+
if (dict == PNone.NONE) {
249+
deleteDictNode.execute(self);
250+
} else {
226251
assert dict instanceof PDict;
227252
setDictNode.execute(self, (PDict) dict);
228253
}
@@ -240,7 +265,7 @@ public Object fallback(Object self, Object state) {
240265
@Builtin(name = __CALL__, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true)
241266
@GenerateNodeFactory
242267
protected abstract static class PartialCallNode extends PythonVarargsBuiltinNode {
243-
private int indexOf(PKeyword[] keywords, PKeyword kw) {
268+
private static int indexOf(PKeyword[] keywords, PKeyword kw) {
244269
for (int i = 0; i < keywords.length; i++) {
245270
if (keywords[i].getName().equals(kw.getName())) {
246271
return i;
@@ -257,17 +282,17 @@ protected boolean withKeywords(PKeyword[] keywords) {
257282
Object callWoDict(VirtualFrame frame, PPartial self, Object[] args, PKeyword[] keywords,
258283
@Cached ConditionProfile hasArgsProfile,
259284
@Cached CallVarargsMethodNode callNode,
260-
@CachedLibrary(limit = "3") HashingStorageLibrary lib) {
285+
@SuppressWarnings("unused") @CachedLibrary(limit = "3") HashingStorageLibrary lib) {
261286
Object[] callArgs = getNewPartialArgs(self, args, hasArgsProfile);
262287
return callNode.execute(frame, self.getFn(), callArgs, keywords);
263288
}
264289

265290
@Specialization(guards = {"self.hasKw(lib)", "!withKeywords(keywords)"})
266-
Object callWDictWoKw(VirtualFrame frame, PPartial self, Object[] args, PKeyword[] keywords,
291+
Object callWDictWoKw(VirtualFrame frame, PPartial self, Object[] args, @SuppressWarnings("unused") PKeyword[] keywords,
267292
@Cached ExpandKeywordStarargsNode starargsNode,
268293
@Cached ConditionProfile hasArgsProfile,
269294
@Cached CallVarargsMethodNode callNode,
270-
@CachedLibrary(limit = "3") HashingStorageLibrary lib) {
295+
@SuppressWarnings("unused") @CachedLibrary(limit = "3") HashingStorageLibrary lib) {
271296
Object[] callArgs = getNewPartialArgs(self, args, hasArgsProfile);
272297
return callNode.execute(frame, self.getFn(), callArgs, starargsNode.execute(self.getKw()));
273298
}
@@ -277,7 +302,7 @@ Object callWDictWKw(VirtualFrame frame, PPartial self, Object[] args, PKeyword[]
277302
@Cached ExpandKeywordStarargsNode starargsNode,
278303
@Cached ConditionProfile hasArgsProfile,
279304
@Cached CallVarargsMethodNode callNode,
280-
@CachedLibrary(limit = "3") HashingStorageLibrary lib) {
305+
@SuppressWarnings("unused") @CachedLibrary(limit = "3") HashingStorageLibrary lib) {
281306
Object[] callArgs = getNewPartialArgs(self, args, hasArgsProfile);
282307

283308
final PKeyword[] pKeywords = starargsNode.execute(self.getKw());

0 commit comments

Comments
 (0)