Skip to content

Commit 4920b99

Browse files
committed
Fix: report correct names in errors for built-in types
1 parent 3a3959a commit 4920b99

File tree

1 file changed

+21
-21
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/tuple

1 file changed

+21
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/tuple/StructSequence.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.oracle.graal.python.builtins.objects.tuple.StructSequenceFactory.NewNodeGen;
7272
import com.oracle.graal.python.builtins.objects.tuple.StructSequenceFactory.ReduceNodeGen;
7373
import com.oracle.graal.python.builtins.objects.tuple.StructSequenceFactory.ReprNodeGen;
74+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
7475
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
7576
import com.oracle.graal.python.nodes.ErrorMessages;
7677
import com.oracle.graal.python.nodes.PRootNode;
@@ -98,7 +99,6 @@
9899
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
99100
import com.oracle.truffle.api.RootCallTarget;
100101
import com.oracle.truffle.api.dsl.Cached;
101-
import com.oracle.truffle.api.dsl.Cached.Shared;
102102
import com.oracle.truffle.api.dsl.NodeFactory;
103103
import com.oracle.truffle.api.dsl.Specialization;
104104
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -272,11 +272,9 @@ private static void createConstructor(PythonLanguage language, Object klass, Des
272272
@Builtin(name = __NEW__, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true)
273273
public abstract static class DisabledNewNode extends PythonVarargsBuiltinNode {
274274

275-
@Child private GetNameNode getNameNode;
276-
277275
@Override
278276
@SuppressWarnings("unused")
279-
public Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
277+
public final Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
280278
if (arguments.length > 0) {
281279
return error(arguments[0], PythonUtils.EMPTY_OBJECT_ARRAY, PKeyword.EMPTY_KEYWORDS);
282280
}
@@ -287,11 +285,7 @@ public Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments,
287285
@Specialization
288286
@SuppressWarnings("unused")
289287
Object error(Object cls, Object[] arguments, PKeyword[] keywords) {
290-
if (getNameNode == null) {
291-
CompilerDirectives.transferToInterpreterAndInvalidate();
292-
getNameNode = insert(GetNameNode.create());
293-
}
294-
throw raise(TypeError, ErrorMessages.CANNOT_CREATE_INSTANCES, getNameNode.execute(cls));
288+
throw raise(TypeError, ErrorMessages.CANNOT_CREATE_INSTANCES, StructSequence.getTpName(cls));
295289
}
296290
}
297291

@@ -316,10 +310,9 @@ PTuple withoutDict(VirtualFrame frame, Object cls, Object sequence, @SuppressWar
316310
@Cached ToArrayNode toArrayNode,
317311
@Cached IsBuiltinClassProfile notASequenceProfile,
318312
@Cached BranchProfile wrongLenProfile,
319-
@Cached BranchProfile needsReallocProfile,
320-
@Shared("getNameNode") @Cached GetNameNode getNameNode) {
313+
@Cached BranchProfile needsReallocProfile) {
321314
Object[] src = sequenceToArray(frame, sequence, fastConstructListNode, toArrayNode, notASequenceProfile);
322-
Object[] dst = processSequence(cls, src, wrongLenProfile, needsReallocProfile, getNameNode);
315+
Object[] dst = processSequence(cls, src, wrongLenProfile, needsReallocProfile);
323316
for (int i = src.length; i < dst.length; ++i) {
324317
dst[i] = PNone.NONE;
325318
}
@@ -333,10 +326,9 @@ PTuple withDict(VirtualFrame frame, Object cls, Object sequence, PDict dict,
333326
@Cached IsBuiltinClassProfile notASequenceProfile,
334327
@Cached BranchProfile wrongLenProfile,
335328
@Cached BranchProfile needsReallocProfile,
336-
@Shared("getNameNode") @Cached GetNameNode getNameNode,
337329
@CachedLibrary(limit = "1") HashingStorageLibrary dictLib) {
338330
Object[] src = sequenceToArray(frame, sequence, fastConstructListNode, toArrayNode, notASequenceProfile);
339-
Object[] dst = processSequence(cls, src, wrongLenProfile, needsReallocProfile, getNameNode);
331+
Object[] dst = processSequence(cls, src, wrongLenProfile, needsReallocProfile);
340332
HashingStorage hs = dict.getDictStorage();
341333
ThreadState threadState = PArguments.getThreadState(frame);
342334
for (int i = src.length; i < dst.length; ++i) {
@@ -348,9 +340,8 @@ PTuple withDict(VirtualFrame frame, Object cls, Object sequence, PDict dict,
348340

349341
@Specialization(guards = {"!isNoValue(dict)", "!isDict(dict)"})
350342
@SuppressWarnings("unused")
351-
PTuple doDictError(VirtualFrame frame, Object cls, Object sequence, Object dict,
352-
@Shared("getNameNode") @Cached GetNameNode getNameNode) {
353-
throw raise(TypeError, ErrorMessages.TAKES_A_DICT_AS_SECOND_ARG_IF_ANY, getNameNode.execute(cls));
343+
PTuple doDictError(VirtualFrame frame, Object cls, Object sequence, Object dict) {
344+
throw raise(TypeError, ErrorMessages.TAKES_A_DICT_AS_SECOND_ARG_IF_ANY, StructSequence.getTpName(cls));
354345
}
355346

356347
private Object[] sequenceToArray(VirtualFrame frame, Object sequence, FastConstructListNode fastConstructListNode, ToArrayNode toArrayNode, IsBuiltinClassProfile notASequenceProfile) {
@@ -364,20 +355,20 @@ private Object[] sequenceToArray(VirtualFrame frame, Object sequence, FastConstr
364355
return toArrayNode.execute(seq.getSequenceStorage());
365356
}
366357

367-
private Object[] processSequence(Object cls, Object[] src, BranchProfile wrongLenProfile, BranchProfile needsReallocProfile, GetNameNode getNameNode) {
358+
private Object[] processSequence(Object cls, Object[] src, BranchProfile wrongLenProfile, BranchProfile needsReallocProfile) {
368359
int len = src.length;
369360
int minLen = inSequence;
370361
int maxLen = fieldNames.length;
371362

372363
if (len < minLen || len > maxLen) {
373364
wrongLenProfile.enter();
374365
if (minLen == maxLen) {
375-
throw raise(TypeError, ErrorMessages.TAKES_A_D_SEQUENCE, getNameNode.execute(cls), minLen, len);
366+
throw raise(TypeError, ErrorMessages.TAKES_A_D_SEQUENCE, StructSequence.getTpName(cls), minLen, len);
376367
}
377368
if (len < minLen) {
378-
throw raise(TypeError, ErrorMessages.TAKES_AN_AT_LEAST_D_SEQUENCE, getNameNode.execute(cls), minLen, len);
369+
throw raise(TypeError, ErrorMessages.TAKES_AN_AT_LEAST_D_SEQUENCE, StructSequence.getTpName(cls), minLen, len);
379370
} else { // len > maxLen
380-
throw raise(TypeError, ErrorMessages.TAKES_AN_AT_MOST_D_SEQUENCE, getNameNode.execute(cls), maxLen, len);
371+
throw raise(TypeError, ErrorMessages.TAKES_AN_AT_MOST_D_SEQUENCE, StructSequence.getTpName(cls), maxLen, len);
381372
}
382373
}
383374

@@ -491,4 +482,13 @@ public boolean isPythonInternal() {
491482
return true;
492483
}
493484
}
485+
486+
static String getTpName(Object cls) {
487+
if (cls instanceof PythonBuiltinClassType) {
488+
return ((PythonBuiltinClassType) cls).getPrintName();
489+
} else if (cls instanceof PythonBuiltinClass) {
490+
return ((PythonBuiltinClass) cls).getType().getPrintName();
491+
}
492+
return GetNameNode.getUncached().execute(cls);
493+
}
494494
}

0 commit comments

Comments
 (0)