Skip to content

Commit f355052

Browse files
committed
add a simpler guard for checking for builtin lists, tuples, and dicts
1 parent 7921c16 commit f355052

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectGetItem.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,29 @@
7373
public abstract class PyObjectGetItem extends PNodeWithContext {
7474
public abstract Object execute(Frame frame, Object object, Object key);
7575

76-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
76+
@Specialization(guards = "cannotBeOverriddenForImmutableType(object)")
7777
Object doList(VirtualFrame frame, PList object, Object key,
78-
@SuppressWarnings("unused") @Shared("getClass") @Cached GetClassNode getClassNode,
7978
@Cached ListBuiltins.GetItemNode getItemNode) {
8079
return getItemNode.execute(frame, object, key);
8180
}
8281

83-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
82+
@Specialization(guards = "cannotBeOverriddenForImmutableType(object)")
8483
Object doTuple(VirtualFrame frame, PTuple object, Object key,
85-
@SuppressWarnings("unused") @Shared("getClass") @Cached GetClassNode getClassNode,
8684
@Cached TupleBuiltins.GetItemNode getItemNode) {
8785
return getItemNode.execute(frame, object, key);
8886
}
8987

9088
@InliningCutoff // TODO: inline this probably?
91-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
89+
@Specialization(guards = "cannotBeOverriddenForImmutableType(object)")
9290
Object doDict(VirtualFrame frame, PDict object, Object key,
93-
@SuppressWarnings("unused") @Shared("getClass") @Cached GetClassNode getClassNode,
9491
@Cached DictBuiltins.GetItemNode getItemNode) {
9592
return getItemNode.execute(frame, object, key);
9693
}
9794

9895
@InliningCutoff // no point inlining the complex case
9996
@Specialization(replaces = {"doList", "doTuple", "doDict"})
10097
Object doGeneric(VirtualFrame frame, Object object, Object key,
101-
@Shared("getClass") @Cached GetClassNode getClassNode,
98+
@Cached GetClassNode getClassNode,
10299
@Cached(parameters = "GetItem") LookupSpecialMethodSlotNode lookupGetItem,
103100
@Cached CallBinaryMethodNode callGetItem,
104101
@Cached PyObjectGetItemClass getItemClass,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,16 @@ public static boolean cannotBeOverridden(Object object, GetClassNode getClassNod
587587
return clazz instanceof PythonBuiltinClassType || clazz instanceof PythonBuiltinClass;
588588
}
589589

590+
/**
591+
* Tests if the class of this Python object is a builtin class. This method is supposed to be
592+
* used for builtin types that do not support __class__ assignment at all, so we can safely
593+
* read the initialPythonClass field and assume that is the current class as well.
594+
*/
595+
public static boolean cannotBeOverriddenForImmutableType(PythonObject object) {
596+
Object clazz = object.getInitialPythonClass();
597+
return clazz instanceof PythonBuiltinClassType || clazz instanceof PythonBuiltinClass;
598+
}
599+
590600
public static boolean isBuiltinDict(PDict dict) {
591601
/*
592602
* dict's __class__ cannot be reassigned and other objects cannot have their class assigned

0 commit comments

Comments
 (0)