Skip to content

Commit f705d68

Browse files
committed
fixed unsupported specializations in DictViewBuiltins
1 parent e76baa0 commit f705d68

File tree

2 files changed

+123
-11
lines changed

2 files changed

+123
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictViewBuiltins.java

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@
6767
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6868
import com.oracle.graal.python.builtins.objects.dict.PDictView.PDictItemsView;
6969
import com.oracle.graal.python.builtins.objects.dict.PDictView.PDictKeysView;
70-
import com.oracle.graal.python.builtins.objects.list.PList;
7170
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
7271
import com.oracle.graal.python.builtins.objects.set.PBaseSet;
7372
import com.oracle.graal.python.builtins.objects.set.PSet;
7473
import com.oracle.graal.python.builtins.objects.set.SetNodes;
7574
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
75+
import com.oracle.graal.python.nodes.ErrorMessages;
7676
import com.oracle.graal.python.nodes.PNodeWithContext;
7777
import com.oracle.graal.python.nodes.SpecialMethodNames;
78+
import static com.oracle.graal.python.nodes.SpecialMethodNames.ISDISJOINT;
7879
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
7980
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
8081
import com.oracle.graal.python.nodes.control.GetNextNode;
@@ -166,6 +167,13 @@ boolean contains(VirtualFrame frame, PDictItemsView self, PTuple key,
166167
return false;
167168
}
168169
}
170+
171+
@SuppressWarnings("unused")
172+
@Fallback
173+
boolean contains(Object self, Object key) {
174+
return false;
175+
}
176+
169177
}
170178

171179
/**
@@ -326,6 +334,18 @@ PBaseSet doKeysView(@SuppressWarnings("unused") VirtualFrame frame, PDictKeysVie
326334
return factory().createSet(storage);
327335
}
328336

337+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
338+
PBaseSet doKeysView(VirtualFrame frame, PDictKeysView self, Object other,
339+
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
340+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
341+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
342+
@CachedLibrary("self.getWrappedDict().getDictStorage()") HashingStorageLibrary lib) {
343+
HashingStorage left = self.getWrappedDict().getDictStorage();
344+
HashingStorage right = constructSetNode.executeWith(frame, other).getDictStorage();
345+
HashingStorage storage = lib.diffWithFrame(left, right, hasFrame, frame);
346+
return factory().createSet(storage);
347+
}
348+
329349
@Specialization
330350
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PBaseSet other,
331351
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
@@ -337,7 +357,7 @@ PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PBaseSet other,
337357
}
338358

339359
@Specialization
340-
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PDictItemsView other,
360+
PBaseSet doNotIterable(VirtualFrame frame, PDictItemsView self, PDictItemsView other,
341361
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
342362
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
343363
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
@@ -347,16 +367,24 @@ PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PDictItemsView oth
347367
return factory().createSet(storage);
348368
}
349369

350-
@Specialization
351-
PBaseSet doItemsView(VirtualFrame frame, PDictKeysView self, PList other,
370+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
371+
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, Object other,
352372
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
353-
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
354-
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
355-
PSet selfSet = constructSetNode.executeWith(frame, self);
356-
PSet otherSet = constructSetNode.executeWith(frame, other);
357-
HashingStorage storage = lib.diffWithFrame(selfSet.getDictStorage(), otherSet.getDictStorage(), hasFrame, frame);
373+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
374+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
375+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
376+
HashingStorage left = constructSetNode.executeWith(frame, self).getDictStorage();
377+
HashingStorage right = constructSetNode.executeWith(frame, other).getDictStorage();
378+
HashingStorage storage = lib.diffWithFrame(left, right, hasFrame, frame);
358379
return factory().createSet(storage);
359380
}
381+
382+
@SuppressWarnings("unused")
383+
@Specialization(guards = {"!isDictKeysView(other)", "!isDictItemsView(other)", "!lib.isIterable(other)"}, limit = "1")
384+
PBaseSet doNotIterable(VirtualFrame frame, PDictView self, Object other,
385+
@CachedLibrary("other") PythonObjectLibrary lib) {
386+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, other);
387+
}
360388
}
361389

362390
@Builtin(name = __AND__, minNumOfPositionalArgs = 2)
@@ -383,6 +411,18 @@ PBaseSet doKeysView(VirtualFrame frame, PDictKeysView self, PDictKeysView other,
383411
return factory().createSet(intersectedStorage);
384412
}
385413

414+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
415+
PBaseSet doKeysView(VirtualFrame frame, PDictKeysView self, Object other,
416+
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
417+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
418+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
419+
@CachedLibrary("self.getWrappedDict().getDictStorage()") HashingStorageLibrary lib) {
420+
HashingStorage left = self.getWrappedDict().getDictStorage();
421+
HashingStorage right = constructSetNode.executeWith(frame, other).getDictStorage();
422+
HashingStorage intersectedStorage = lib.intersectWithFrame(left, right, hasFrame, frame);
423+
return factory().createSet(intersectedStorage);
424+
}
425+
386426
@Specialization
387427
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PBaseSet other,
388428
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
@@ -407,6 +447,26 @@ PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PDictItemsView oth
407447
HashingStorage intersectedStorage = lib.intersectWithFrame(left, right, hasFrame, frame);
408448
return factory().createSet(intersectedStorage);
409449
}
450+
451+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
452+
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, Object other,
453+
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
454+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
455+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
456+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
457+
HashingStorage left = constructSetNode.executeWith(frame, self).getDictStorage();
458+
HashingStorage right = constructSetNode.executeWith(frame, other).getDictStorage();
459+
HashingStorage intersectedStorage = lib.intersectWithFrame(left, right, hasFrame, frame);
460+
return factory().createSet(intersectedStorage);
461+
}
462+
463+
@SuppressWarnings("unused")
464+
@Specialization(guards = {"!isDictKeysView(other)", "!isDictItemsView(other)",
465+
"!lib.isIterable(other)"}, limit = "1")
466+
PBaseSet doNotIterable(VirtualFrame frame, PDictView self, Object other,
467+
@CachedLibrary("other") PythonObjectLibrary lib) {
468+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, other);
469+
}
410470
}
411471

412472
@Builtin(name = __OR__, minNumOfPositionalArgs = 2)
@@ -429,6 +489,14 @@ PBaseSet doKeysView(@SuppressWarnings("unused") VirtualFrame frame, PDictKeysVie
429489
return factory().createSet(union(lib, self.getWrappedDict().getDictStorage(), other.getWrappedDict().getDictStorage()));
430490
}
431491

492+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
493+
PBaseSet doKeysView(@SuppressWarnings("unused") VirtualFrame frame, PDictKeysView self, Object other,
494+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
495+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
496+
@CachedLibrary("self.getWrappedDict().getDictStorage()") HashingStorageLibrary lib) {
497+
return factory().createSet(union(lib, self.getWrappedDict().getDictStorage(), constructSetNode.executeWith(frame, other).getDictStorage()));
498+
}
499+
432500
@Specialization
433501
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PBaseSet other,
434502
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
@@ -445,6 +513,20 @@ PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PDictItemsView oth
445513
PSet otherSet = constructSetNode.executeWith(frame, other);
446514
return factory().createSet(union(lib, selfSet.getDictStorage(), otherSet.getDictStorage()));
447515
}
516+
517+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
518+
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, Object other,
519+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
520+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
521+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
522+
return factory().createSet(union(lib, constructSetNode.executeWith(frame, self).getDictStorage(), constructSetNode.executeWith(frame, other).getDictStorage()));
523+
}
524+
525+
@Specialization(guards = {"!isDictKeysView(other)", "!isDictItemsView(other)", "!lib.isIterable(other)"}, limit = "1")
526+
PBaseSet doNotIterable(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("unused") PDictView self, Object other,
527+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary lib) {
528+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, other);
529+
}
448530
}
449531

450532
@Builtin(name = __XOR__, minNumOfPositionalArgs = 2)
@@ -467,22 +549,44 @@ PBaseSet doKeysView(@SuppressWarnings("unused") VirtualFrame frame, PDictKeysVie
467549
return factory().createSet(xor(lib, self.getWrappedDict().getDictStorage(), other.getWrappedDict().getDictStorage()));
468550
}
469551

552+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
553+
PBaseSet doKeysView(@SuppressWarnings("unused") VirtualFrame frame, PDictKeysView self, Object other,
554+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
555+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
556+
@CachedLibrary("self.getWrappedDict().getDictStorage()") HashingStorageLibrary lib) {
557+
return factory().createSet(xor(lib, self.getWrappedDict().getDictStorage(), constructSetNode.executeWith(frame, other).getDictStorage()));
558+
}
559+
470560
@Specialization
471-
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PBaseSet other,
561+
PBaseSet doItemsView(@SuppressWarnings("unused") VirtualFrame frame, PDictItemsView self, PBaseSet other,
472562
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
473563
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
474564
PSet selfSet = constructSetNode.executeWith(frame, self);
475565
return factory().createSet(xor(lib, selfSet.getDictStorage(), other.getDictStorage()));
476566
}
477567

478568
@Specialization
479-
PBaseSet doItemsView(VirtualFrame frame, PDictItemsView self, PDictItemsView other,
569+
PBaseSet doItemsView(@SuppressWarnings("unused") VirtualFrame frame, PDictItemsView self, PDictItemsView other,
480570
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
481571
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
482572
PSet selfSet = constructSetNode.executeWith(frame, self);
483573
PSet otherSet = constructSetNode.executeWith(frame, other);
484574
return factory().createSet(xor(lib, selfSet.getDictStorage(), otherSet.getDictStorage()));
485575
}
576+
577+
@Specialization(guards = "libOther.isIterable(other)", limit = "1")
578+
PBaseSet doItemsView(@SuppressWarnings("unused") VirtualFrame frame, PDictItemsView self, Object other,
579+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary libOther,
580+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode,
581+
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
582+
return factory().createSet(xor(lib, constructSetNode.executeWith(frame, self).getDictStorage(), constructSetNode.executeWith(frame, other).getDictStorage()));
583+
}
584+
585+
@Specialization(guards = {"!isDictKeysView(other)", "!isDictItemsView(other)", "!lib.isIterable(other)"}, limit = "1")
586+
PBaseSet doNotIterable(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("unused") PDictView self, Object other,
587+
@SuppressWarnings("unused") @CachedLibrary("other") PythonObjectLibrary lib) {
588+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, other);
589+
}
486590
}
487591

488592
@Builtin(name = __LE__, minNumOfPositionalArgs = 2)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PGuards.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ public static boolean isDictView(Object obj) {
424424
return obj instanceof PDictView;
425425
}
426426

427+
public static boolean isDictKeysView(Object obj) {
428+
return obj instanceof PDictView.PDictKeysView;
429+
}
430+
431+
public static boolean isDictItemsView(Object obj) {
432+
return obj instanceof PDictView.PDictItemsView;
433+
}
434+
427435
public static boolean isPSlice(Object obj) {
428436
return obj instanceof PSlice;
429437
}

0 commit comments

Comments
 (0)