Skip to content

Commit 6d54bd7

Browse files
committed
[GR-44450] Check for subclasses in IsBuiltinObjectProfile
PullRequest: graalpython/3051
2 parents dda8276 + cdbc81a commit 6d54bd7

File tree

51 files changed

+639
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+639
-551
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_exception.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,28 @@ def test_del(self):
648648
except NameError:
649649
pass
650650

651+
def expected_exception_in_builtins_allows_subclasses(self):
652+
class StopIter1(StopIteration):
653+
pass
654+
655+
class IndexError1(IndexError):
656+
pass
657+
658+
class AttributeError1(AttributeError):
659+
pass
660+
661+
class Iter1:
662+
def __iter__(self): return self
663+
664+
def __next__(self): raise StopIter1
665+
666+
class Iter2:
667+
def __getitem__(self, i): raise IndexError1
668+
669+
class Obj1:
670+
def __getattr__(self, i): raise AttributeError1
671+
672+
assert list(Iter1()) == []
673+
assert list(Iter2()) == []
674+
sentinel = object()
675+
assert getattr(Obj1(), 'does_not_exist', sentinel) is sentinel

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
7171
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
7272
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
73-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.InlineIsBuiltinClassProfile;
73+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinClassExactProfile;
7474
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
7575
import com.oracle.graal.python.nodes.util.SplitArgsNode;
7676
import com.oracle.graal.python.runtime.exception.PException;
@@ -125,11 +125,11 @@ abstract static class ArrayNode extends PythonVarargsBuiltinNode {
125125
static Object array2(VirtualFrame frame, Object cls, Object[] args, PKeyword[] kwargs,
126126
@Bind("this") Node inliningTarget,
127127
@Cached InlinedConditionProfile hasInitializerProfile,
128-
@Cached InlineIsBuiltinClassProfile isNotSubtypeProfile,
128+
@Cached IsBuiltinClassExactProfile isNotSubtypeProfile,
129129
@Cached CastToTruffleStringCheckedNode cast,
130130
@Cached ArrayNodeInternal arrayNodeInternal,
131131
@Cached PRaiseNode.Lazy raise) {
132-
if (isNotSubtypeProfile.profileIsBuiltinClass(inliningTarget, cls, PythonBuiltinClassType.PArray)) {
132+
if (isNotSubtypeProfile.profileClass(inliningTarget, cls, PythonBuiltinClassType.PArray)) {
133133
if (kwargs.length != 0) {
134134
throw raise.get(inliningTarget).raise(TypeError, S_TAKES_NO_KEYWORD_ARGS, "array.array()");
135135
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5959
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
6060
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
61-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.InlineIsBuiltinClassProfile;
61+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinClassProfile;
6262
import com.oracle.graal.python.nodes.object.GetClassNode;
6363
import com.oracle.graal.python.runtime.PythonContext;
6464
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
@@ -119,7 +119,7 @@ public Object execute(VirtualFrame frame) {
119119
@TruffleBoundary
120120
private static void handleException(PythonContext context, PException e) {
121121
Object pythonException = e.getEscapedException();
122-
if (!InlineIsBuiltinClassProfile.profileClassSlowPath(GetClassNode.executeUncached(pythonException), PythonBuiltinClassType.SystemExit)) {
122+
if (!IsBuiltinClassProfile.profileClassSlowPath(GetClassNode.executeUncached(pythonException), PythonBuiltinClassType.SystemExit)) {
123123
PyObjectCallMethodObjArgs callWrite = PyObjectCallMethodObjArgs.getUncached();
124124
callWrite.execute(null, null, context.getStderr(), T_WRITE, toTruffleStringUncached("Error in atexit._run_exitfuncs:\n"));
125125
try {

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

Lines changed: 80 additions & 78 deletions
Large diffs are not rendered by default.

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,7 @@ protected final BinaryComparisonNode createComparison() {
16551655
}
16561656

16571657
@Specialization(guards = "args.length == 0")
1658+
@SuppressWarnings("truffle-static-method")
16581659
Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings("unused") Object[] args, Object keywordArgIn, Object defaultVal,
16591660
@Bind("this") Node inliningTarget,
16601661
@Exclusive @Cached PyObjectGetIter getIter,
@@ -2218,89 +2219,81 @@ public abstract static class SumFunctionNode extends PythonBuiltinNode {
22182219
@Specialization(rewriteOn = UnexpectedResultException.class)
22192220
int sumIntNone(VirtualFrame frame, Object arg1, @SuppressWarnings("unused") PNone start,
22202221
@Bind("this") Node inliningTarget,
2221-
@Shared @Cached IsBuiltinObjectProfile errorProfile1,
2222-
@Shared @Cached IsBuiltinObjectProfile errorProfile2,
2223-
@Shared @Cached IsBuiltinObjectProfile errorProfile3,
2222+
@Shared @Cached IsBuiltinObjectProfile errorProfile,
22242223
@Shared("getIter") @Cached PyObjectGetIter getIter) throws UnexpectedResultException {
2225-
return sumIntInternal(frame, arg1, 0, getIter, inliningTarget, errorProfile1, errorProfile2, errorProfile3);
2224+
return sumIntInternal(frame, inliningTarget, arg1, 0, getIter, errorProfile);
22262225
}
22272226

22282227
@Specialization(rewriteOn = UnexpectedResultException.class)
22292228
int sumIntInt(VirtualFrame frame, Object arg1, int start,
22302229
@Bind("this") Node inliningTarget,
2231-
@Shared @Cached IsBuiltinObjectProfile errorProfile1,
2232-
@Shared @Cached IsBuiltinObjectProfile errorProfile2,
2233-
@Shared @Cached IsBuiltinObjectProfile errorProfile3,
2230+
@Shared @Cached IsBuiltinObjectProfile errorProfile,
22342231
@Shared("getIter") @Cached PyObjectGetIter getIter) throws UnexpectedResultException {
2235-
return sumIntInternal(frame, arg1, start, getIter, inliningTarget, errorProfile1, errorProfile2, errorProfile3);
2232+
return sumIntInternal(frame, inliningTarget, arg1, start, getIter, errorProfile);
22362233
}
22372234

2238-
private int sumIntInternal(VirtualFrame frame, Object arg1, int start, PyObjectGetIter getIter,
2239-
Node inliningTarget, IsBuiltinObjectProfile errorProfile1,
2240-
IsBuiltinObjectProfile errorProfile2, IsBuiltinObjectProfile errorProfile3) throws UnexpectedResultException {
2235+
private int sumIntInternal(VirtualFrame frame, Node inliningTarget, Object arg1, int start, PyObjectGetIter getIter,
2236+
IsBuiltinObjectProfile errorProfile) throws UnexpectedResultException {
22412237
Object iterator = getIter.execute(frame, inliningTarget, arg1);
22422238
int value = start;
22432239
while (true) {
22442240
int nextValue;
22452241
try {
22462242
nextValue = PGuards.expectInteger(next.executeObject(frame, iterator));
22472243
} catch (PException e) {
2248-
e.expectStopIteration(inliningTarget, errorProfile1);
2244+
e.expectStopIteration(inliningTarget, errorProfile);
22492245
return value;
22502246
} catch (UnexpectedResultException e) {
22512247
Object newValue = add.executeObject(frame, value, e.getResult());
2252-
throw new UnexpectedResultException(iterateGeneric(frame, iterator, newValue, inliningTarget, errorProfile2));
2248+
throw new UnexpectedResultException(iterateGeneric(frame, inliningTarget, iterator, newValue, errorProfile));
22532249
}
22542250
try {
22552251
value = add.executeInt(frame, value, nextValue);
22562252
} catch (UnexpectedResultException e) {
2257-
throw new UnexpectedResultException(iterateGeneric(frame, iterator, e.getResult(), inliningTarget, errorProfile3));
2253+
throw new UnexpectedResultException(iterateGeneric(frame, inliningTarget, iterator, e.getResult(), errorProfile));
22582254
}
22592255
}
22602256
}
22612257

22622258
@Specialization(rewriteOn = UnexpectedResultException.class)
22632259
double sumDoubleDouble(VirtualFrame frame, Object arg1, double start,
22642260
@Bind("this") Node inliningTarget,
2265-
@Shared @Cached IsBuiltinObjectProfile errorProfile1,
2266-
@Shared @Cached IsBuiltinObjectProfile errorProfile2,
2267-
@Shared @Cached IsBuiltinObjectProfile errorProfile3,
2261+
@Shared @Cached IsBuiltinObjectProfile errorProfile,
22682262
// dummy inline profile, so it can be @Shared, to optimize generated code:
22692263
@SuppressWarnings("unused") @Shared @Cached InlinedConditionProfile hasStart,
22702264
@Shared("getIter") @Cached PyObjectGetIter getIter,
22712265
// dummy raiseNode, so it can be @Shared, to optimize generated code:
22722266
@SuppressWarnings("unused") @Shared @Cached PRaiseNode.Lazy raiseNode) throws UnexpectedResultException {
2273-
return sumDoubleInternal(frame, arg1, start, getIter, inliningTarget, errorProfile1, errorProfile2, errorProfile3);
2267+
return sumDoubleInternal(frame, inliningTarget, arg1, start, getIter, errorProfile);
22742268
}
22752269

2276-
private double sumDoubleInternal(VirtualFrame frame, Object arg1, double start, PyObjectGetIter getIter,
2277-
Node inliningTarget, IsBuiltinObjectProfile errorProfile1,
2278-
IsBuiltinObjectProfile errorProfile2, IsBuiltinObjectProfile errorProfile3) throws UnexpectedResultException {
2270+
private double sumDoubleInternal(VirtualFrame frame, Node inliningTarget, Object arg1, double start, PyObjectGetIter getIter,
2271+
IsBuiltinObjectProfile errorProfile) throws UnexpectedResultException {
22792272
Object iterator = getIter.execute(frame, inliningTarget, arg1);
22802273
double value = start;
22812274
while (true) {
22822275
double nextValue;
22832276
try {
22842277
nextValue = PGuards.expectDouble(next.executeObject(frame, iterator));
22852278
} catch (PException e) {
2286-
e.expectStopIteration(inliningTarget, errorProfile1);
2279+
e.expectStopIteration(inliningTarget, errorProfile);
22872280
return value;
22882281
} catch (UnexpectedResultException e) {
22892282
Object newValue = add.executeObject(frame, value, e.getResult());
2290-
throw new UnexpectedResultException(iterateGeneric(frame, iterator, newValue, inliningTarget, errorProfile2));
2283+
throw new UnexpectedResultException(iterateGeneric(frame, inliningTarget, iterator, newValue, errorProfile));
22912284
}
22922285
try {
22932286
value = add.executeDouble(frame, value, nextValue);
22942287
} catch (UnexpectedResultException e) {
2295-
throw new UnexpectedResultException(iterateGeneric(frame, iterator, e.getResult(), inliningTarget, errorProfile3));
2288+
throw new UnexpectedResultException(iterateGeneric(frame, inliningTarget, iterator, e.getResult(), errorProfile));
22962289
}
22972290
}
22982291
}
22992292

23002293
@Specialization(replaces = {"sumIntNone", "sumIntInt", "sumDoubleDouble"})
23012294
Object sum(VirtualFrame frame, Object arg1, Object start,
23022295
@Bind("this") Node inliningTarget,
2303-
@Shared @Cached IsBuiltinObjectProfile errorProfile1,
2296+
@Shared @Cached IsBuiltinObjectProfile errorProfile,
23042297
@Shared("getIter") @Cached PyObjectGetIter getIter,
23052298
@Shared @Cached InlinedConditionProfile hasStart,
23062299
@Shared @Cached PRaiseNode.Lazy raiseNode) {
@@ -2312,10 +2305,10 @@ Object sum(VirtualFrame frame, Object arg1, Object start,
23122305
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.CANT_SUM_BYTEARRAY);
23132306
}
23142307
Object iterator = getIter.execute(frame, inliningTarget, arg1);
2315-
return iterateGeneric(frame, iterator, hasStart.profile(inliningTarget, start != NO_VALUE) ? start : 0, inliningTarget, errorProfile1);
2308+
return iterateGeneric(frame, inliningTarget, iterator, hasStart.profile(inliningTarget, start != NO_VALUE) ? start : 0, errorProfile);
23162309
}
23172310

2318-
private Object iterateGeneric(VirtualFrame frame, Object iterator, Object start, Node inliningTarget, IsBuiltinObjectProfile errorProfile) {
2311+
private Object iterateGeneric(VirtualFrame frame, Node inliningTarget, Object iterator, Object start, IsBuiltinObjectProfile errorProfile) {
23192312
Object value = start;
23202313
while (true) {
23212314
Object nextValue;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7979
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8080
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
81-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.InlineIsBuiltinClassProfile;
81+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinClassExactProfile;
8282
import com.oracle.graal.python.nodes.object.GetClassNode;
8383
import com.oracle.graal.python.runtime.AsyncHandler;
8484
import com.oracle.graal.python.runtime.PythonContext;
@@ -439,7 +439,7 @@ PReferenceType refTypeWithCallback(Object cls, Object object, Object callback,
439439
PReferenceType refType(Object cls, PythonAbstractNativeObject pythonObject, Object callback,
440440
@Bind("this") Node inliningTarget,
441441
@Exclusive @Cached GetClassNode getClassNode,
442-
@Cached InlineIsBuiltinClassProfile profile,
442+
@Cached IsBuiltinClassExactProfile profile,
443443
@Cached GetMroNode getMroNode,
444444
@Shared @Cached PythonObjectFactory factory,
445445
@Exclusive @Cached PRaiseNode.Lazy raiseNode) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextSlotBuiltins.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@
9595
import java.nio.charset.CharsetEncoder;
9696
import java.nio.charset.StandardCharsets;
9797

98-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
9998
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBinaryBuiltinNode;
10099
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
101100
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
101+
import com.oracle.graal.python.builtins.modules.ctypes.StgDictObject;
102102
import com.oracle.graal.python.builtins.objects.PNone;
103103
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
104104
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
@@ -146,7 +146,6 @@
146146
import com.oracle.graal.python.nodes.SpecialAttributeNames;
147147
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
148148
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
149-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
150149
import com.oracle.graal.python.nodes.object.GetClassNode;
151150
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
152151
import com.oracle.graal.python.nodes.object.SetDictNode;
@@ -807,35 +806,26 @@ static Object doTpDict(PythonManagedClass object, Object value,
807806
@Cached HashingStorageGetIterator getIterator,
808807
@Cached HashingStorageIteratorNext itNext,
809808
@Cached HashingStorageIteratorKey itKey,
810-
@Cached HashingStorageIteratorValue itValue,
811-
@Cached IsBuiltinObjectProfile isPrimitiveDictProfile1,
812-
@Cached IsBuiltinObjectProfile isPrimitiveDictProfile2) {
813-
if (isBuiltinDict(inliningTarget, isPrimitiveDictProfile1, isPrimitiveDictProfile2, value)) {
809+
@Cached HashingStorageIteratorValue itValue) {
810+
if (value instanceof PDict dict && (PGuards.isBuiltinDict(dict) || value instanceof StgDictObject)) {
814811
// special and fast case: commit items and change store
815-
PDict d = (PDict) value;
816-
HashingStorage storage = d.getDictStorage();
812+
HashingStorage storage = dict.getDictStorage();
817813
HashingStorageIterator it = getIterator.execute(inliningTarget, storage);
818814
while (itNext.execute(inliningTarget, storage, it)) {
819815
writeAttrNode.execute(object, itKey.execute(inliningTarget, storage, it), itValue.execute(inliningTarget, storage, it));
820816
}
821817
PDict existing = getDict.execute(object);
822818
if (existing != null) {
823-
d.setDictStorage(existing.getDictStorage());
819+
dict.setDictStorage(existing.getDictStorage());
824820
} else {
825-
d.setDictStorage(new DynamicObjectStorage(object.getStorage()));
821+
dict.setDictStorage(new DynamicObjectStorage(object.getStorage()));
826822
}
827-
setDict.execute(inliningTarget, object, d);
823+
setDict.execute(inliningTarget, object, dict);
828824
} else {
829825
// TODO custom mapping object
830826
}
831827
return PNone.NO_VALUE;
832828
}
833-
834-
private static boolean isBuiltinDict(Node inliningTarget, IsBuiltinObjectProfile isPrimitiveDictProfile1, IsBuiltinObjectProfile isPrimitiveDictProfile2, Object value) {
835-
return value instanceof PDict &&
836-
(isPrimitiveDictProfile1.profileObject(inliningTarget, value, PythonBuiltinClassType.PDict) ||
837-
isPrimitiveDictProfile2.profileObject(inliningTarget, value, PythonBuiltinClassType.StgDict));
838-
}
839829
}
840830

841831
@CApiBuiltin(ret = Void, args = {PyTypeObject, Py_ssize_t}, call = Ignored)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextUnicodeBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
127127
import com.oracle.graal.python.lib.PyObjectLookupAttr;
128128
import com.oracle.graal.python.lib.PySliceNew;
129+
import com.oracle.graal.python.lib.PyUnicodeCheckExactNode;
129130
import com.oracle.graal.python.lib.PyUnicodeFromEncodedObject;
130131
import com.oracle.graal.python.nodes.ErrorMessages;
131132
import com.oracle.graal.python.nodes.PGuards;
@@ -134,7 +135,6 @@
134135
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
135136
import com.oracle.graal.python.nodes.call.CallNode;
136137
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
137-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
138138
import com.oracle.graal.python.nodes.object.GetClassNode;
139139
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
140140
import com.oracle.graal.python.nodes.truffle.PythonTypes;
@@ -323,13 +323,13 @@ static Object withTS(TruffleString str,
323323
@Specialization
324324
static Object withPString(PString str,
325325
@Bind("this") Node inliningTarget,
326-
@Cached IsBuiltinObjectProfile isBuiltinClassProfile,
326+
@Cached PyUnicodeCheckExactNode unicodeCheckExactNode,
327327
@Cached ReadAttributeFromDynamicObjectNode readNode,
328328
@Exclusive @Cached StringNodes.InternStringNode internNode,
329329
@Exclusive @Cached HashingStorageGetItem getItem,
330330
@Exclusive @Cached HashingStorageSetItem setItem,
331331
@Exclusive @Cached PythonObjectFactory.Lazy factory) {
332-
if (!isBuiltinClassProfile.profileObject(inliningTarget, str, PythonBuiltinClassType.PString)) {
332+
if (!unicodeCheckExactNode.execute(inliningTarget, str)) {
333333
return getNativeNull(inliningTarget);
334334
}
335335
boolean isInterned = readNode.execute(str, PString.INTERNED) != PNone.NO_VALUE;

0 commit comments

Comments
 (0)