Skip to content

Commit e0eb8e9

Browse files
committed
[GR-27559] Move more POL messages into nodes
PullRequest: graalpython/1957
2 parents 3a6b091 + 05e7786 commit e0eb8e9

File tree

92 files changed

+1678
-1748
lines changed

Some content is hidden

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

92 files changed

+1678
-1748
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/ConversionNodeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.oracle.graal.python.builtins.objects.function.Signature;
5050
import com.oracle.graal.python.nodes.PRootNode;
5151
import com.oracle.graal.python.nodes.call.CallTargetInvokeNode;
52-
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise;
52+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
5353
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
5454
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
5555
import com.oracle.graal.python.runtime.GilNode;
@@ -65,13 +65,13 @@ public class ConversionNodeTests {
6565
static final Signature SIGNATURE = new Signature(-1, false, -1, false, new String[]{"arg"}, null);
6666
@Rule public ExpectedException expectedException = ExpectedException.none();
6767

68-
protected static Object call(Object arg, ArgumentCastNodeWithRaise castNode) {
68+
protected static Object call(Object arg, ArgumentCastNode castNode) {
6969
PythonLanguage language = PythonLanguage.get(castNode);
7070
final PythonContext pythonContext = PythonContext.get(castNode);
7171

7272
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(language) {
7373
@Child private CalleeContext calleeContext = CalleeContext.create();
74-
@Child private ArgumentCastNodeWithRaise node = castNode;
74+
@Child private ArgumentCastNode node = castNode;
7575

7676
@Override
7777
public Object execute(VirtualFrame frame) {

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/FileDescriptorConversionNodeTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public void longFitsInt() {
9999

100100
@Test
101101
public void longTooBig() {
102-
expectPythonMessage("OverflowError: Python int too large to convert to int");
102+
expectPythonMessage("OverflowError: Python int too large to convert to Java int");
103103
call(1L << 40);
104104
}
105105

106106
@Test
107107
public void longTooSmall() {
108-
expectPythonMessage("OverflowError: Python int too large to convert to int");
108+
expectPythonMessage("OverflowError: Python int too large to convert to Java int");
109109
call(-1L << 40);
110110
}
111111

@@ -116,13 +116,13 @@ public void pintFitsInt() {
116116

117117
@Test
118118
public void pintTooBig() {
119-
expectPythonMessage("OverflowError: Python int too large to convert to int");
119+
expectPythonMessage("OverflowError: Python int too large to convert to Java int");
120120
call(factory().createInt(BigInteger.ONE.shiftLeft(100)));
121121
}
122122

123123
@Test
124124
public void pintTooSmall() {
125-
expectPythonMessage("OverflowError: Python int too large to convert to int");
125+
expectPythonMessage("OverflowError: Python int too large to convert to Java int");
126126
call(factory().createInt(BigInteger.ONE.shiftLeft(100).negate()));
127127
}
128128

@@ -145,7 +145,7 @@ public void filenoWrongType() {
145145

146146
@Test
147147
public void filenoTooBig() {
148-
expectPythonMessage("OverflowError: Python int too large to convert to int");
148+
expectPythonMessage("OverflowError: Python int too large to convert to Java int");
149149
call(evalValue("class C:\n def fileno(self):\n return 1 << 40\nC()"));
150150
}
151151

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,15 @@ def test_assert(self):
4747
except:
4848
pass
4949
else:
50-
raise Exception("Assertions doesn't work!")
50+
raise Exception("Assertions don't work!")
51+
52+
def test_assert_message(self):
53+
try:
54+
assert False, 1
55+
except AssertionError as e:
56+
if e.args != (1,):
57+
raise Exception(f"Expected AssertionError.args to be {(1,)}, was {e.args}")
58+
else:
59+
raise Exception("Assertions don't work!")
60+
5161

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.regex.Matcher;
4545
import java.util.regex.Pattern;
4646

47+
import com.oracle.graal.python.lib.PyObjectLookupAttr;
4748
import org.graalvm.nativeimage.ImageInfo;
4849

4950
import com.oracle.graal.python.PythonLanguage;
@@ -211,7 +212,6 @@
211212
import com.oracle.graal.python.builtins.objects.module.PythonModule;
212213
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
213214
import com.oracle.graal.python.builtins.objects.object.PythonObject;
214-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
215215
import com.oracle.graal.python.builtins.objects.posix.DirEntryBuiltins;
216216
import com.oracle.graal.python.builtins.objects.posix.ScandirIteratorBuiltins;
217217
import com.oracle.graal.python.builtins.objects.property.PropertyBuiltins;
@@ -759,7 +759,7 @@ public void warn(PythonBuiltinClassType type, String format, Object... args) {
759759
*/
760760
public Object getStderr() {
761761
try {
762-
return PythonObjectLibrary.getUncached().lookupAttribute(sysModule, null, "stderr");
762+
return PyObjectLookupAttr.getUncached().execute(null, sysModule, "stderr");
763763
} catch (PException e) {
764764
try {
765765
getContext().getEnv().err().write("lost sys.stderr\n".getBytes());

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.oracle.graal.python.annotations.ArgumentClinic;
3939
import com.oracle.graal.python.builtins.Builtin;
4040
import com.oracle.graal.python.builtins.CoreFunctions;
41+
import com.oracle.graal.python.builtins.Python3Core;
4142
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4243
import com.oracle.graal.python.builtins.PythonBuiltins;
4344
import com.oracle.graal.python.builtins.objects.PNone;
@@ -54,6 +55,7 @@
5455
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5556
import com.oracle.graal.python.builtins.objects.range.PIntRange;
5657
import com.oracle.graal.python.builtins.objects.str.StringNodes;
58+
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
5759
import com.oracle.graal.python.nodes.PGuards;
5860
import com.oracle.graal.python.nodes.PRaiseNode;
5961
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -65,7 +67,6 @@
6567
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6668
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6769
import com.oracle.graal.python.nodes.util.SplitArgsNode;
68-
import com.oracle.graal.python.builtins.Python3Core;
6970
import com.oracle.graal.python.runtime.exception.PException;
7071
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7172
import com.oracle.graal.python.runtime.sequence.PSequence;
@@ -329,7 +330,7 @@ abstract static class ArrayReconstructorNode extends PythonClinicBuiltinNode {
329330
Object reconstructCached(VirtualFrame frame, Object arrayType, String typeCode, @SuppressWarnings("unused") int mformatCode, PBytes bytes,
330331
@Cached("mformatCode") int cachedCode,
331332
@Cached("createIdentityProfile()") ValueProfile formatProfile,
332-
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
333+
@Cached PyObjectCallMethodObjArgs callDecode,
333334
@Cached ArrayBuiltins.FromBytesNode fromBytesNode,
334335
@Cached ArrayBuiltins.FromUnicodeNode fromUnicodeNode,
335336
@Cached IsSubtypeNode isSubtypeNode,
@@ -338,12 +339,12 @@ Object reconstructCached(VirtualFrame frame, Object arrayType, String typeCode,
338339
if (format == null) {
339340
throw raise(ValueError, "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");
340341
}
341-
return doReconstruct(frame, arrayType, typeCode, cachedCode, bytes, lib, fromBytesNode, fromUnicodeNode, isSubtypeNode, byteSwapNode, formatProfile.profile(format));
342+
return doReconstruct(frame, arrayType, typeCode, cachedCode, bytes, callDecode, fromBytesNode, fromUnicodeNode, isSubtypeNode, byteSwapNode, formatProfile.profile(format));
342343
}
343344

344345
@Specialization(replaces = "reconstructCached")
345346
Object reconstruct(VirtualFrame frame, Object arrayType, String typeCode, int mformatCode, PBytes bytes,
346-
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
347+
@Cached PyObjectCallMethodObjArgs callDecode,
347348
@Cached ArrayBuiltins.FromBytesNode fromBytesNode,
348349
@Cached ArrayBuiltins.FromUnicodeNode fromUnicodeNode,
349350
@Cached IsSubtypeNode isSubtypeNode,
@@ -352,10 +353,10 @@ Object reconstruct(VirtualFrame frame, Object arrayType, String typeCode, int mf
352353
if (format == null) {
353354
throw raise(ValueError, "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");
354355
}
355-
return doReconstruct(frame, arrayType, typeCode, mformatCode, bytes, lib, fromBytesNode, fromUnicodeNode, isSubtypeNode, byteSwapNode, format);
356+
return doReconstruct(frame, arrayType, typeCode, mformatCode, bytes, callDecode, fromBytesNode, fromUnicodeNode, isSubtypeNode, byteSwapNode, format);
356357
}
357358

358-
private Object doReconstruct(VirtualFrame frame, Object arrayType, String typeCode, int mformatCode, PBytes bytes, PythonObjectLibrary lib,
359+
private Object doReconstruct(VirtualFrame frame, Object arrayType, String typeCode, int mformatCode, PBytes bytes, PyObjectCallMethodObjArgs callDecode,
359360
ArrayBuiltins.FromBytesNode fromBytesNode, ArrayBuiltins.FromUnicodeNode fromUnicodeNode, IsSubtypeNode isSubtypeNode,
360361
ArrayBuiltins.ByteSwapNode byteSwapNode, BufferFormat format) {
361362
if (!isSubtypeNode.execute(frame, arrayType, PythonBuiltinClassType.PArray)) {
@@ -371,7 +372,7 @@ private Object doReconstruct(VirtualFrame frame, Object arrayType, String typeCo
371372
String newTypeCode = machineFormat.format == format ? typeCode : machineFormat.format.baseTypeCode;
372373
array = factory().createArray(arrayType, newTypeCode, machineFormat.format);
373374
if (machineFormat.unicodeEncoding != null) {
374-
Object decoded = lib.lookupAndCallRegularMethod(bytes, frame, "decode", machineFormat.unicodeEncoding);
375+
Object decoded = callDecode.execute(frame, bytes, "decode", machineFormat.unicodeEncoding);
375376
fromUnicodeNode.execute(frame, array, decoded);
376377
} else {
377378
fromBytesNode.execute(frame, array, bytes);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5151
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5252
import com.oracle.graal.python.builtins.objects.function.PKeyword;
53-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
53+
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
5454
import com.oracle.graal.python.nodes.call.CallNode;
5555
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5656
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -117,13 +117,13 @@ public Object execute(VirtualFrame frame) {
117117
@TruffleBoundary
118118
private static void handleException(PythonContext context, PException e) {
119119
PBaseException pythonException = e.getEscapedException();
120-
PythonObjectLibrary lib = PythonObjectLibrary.getUncached();
121120
if (!IsBuiltinClassProfile.profileClassSlowPath(GetClassNode.getUncached().execute(pythonException), PythonBuiltinClassType.SystemExit)) {
122-
lib.lookupAndCallRegularMethod(context.getCore().getStderr(), null, "write", "Error in atexit._run_exitfuncs:\n");
121+
PyObjectCallMethodObjArgs callWrite = PyObjectCallMethodObjArgs.getUncached();
122+
callWrite.execute(null, context.getCore().getStderr(), "write", "Error in atexit._run_exitfuncs:\n");
123123
try {
124124
ExceptionUtils.printExceptionTraceback(context, pythonException);
125125
} catch (PException pe) {
126-
lib.lookupAndCallRegularMethod(context.getCore().getStderr(), null, "write", "Failed to print traceback\n");
126+
callWrite.execute(null, context.getCore().getStderr(), "write", "Failed to print traceback\n");
127127
}
128128
}
129129
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,15 @@
187187
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsAcceptableBaseNode;
188188
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
189189
import com.oracle.graal.python.lib.CanBeDoubleNode;
190+
import com.oracle.graal.python.lib.PyCallableCheckNode;
190191
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
191192
import com.oracle.graal.python.lib.PyFloatFromString;
193+
import com.oracle.graal.python.lib.PyMappingCheckNode;
192194
import com.oracle.graal.python.lib.PyMemoryViewFromObject;
193195
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
194196
import com.oracle.graal.python.lib.PyNumberFloatNode;
195197
import com.oracle.graal.python.lib.PyNumberIndexNode;
198+
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
196199
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
197200
import com.oracle.graal.python.lib.PyObjectSizeNode;
198201
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
@@ -1559,16 +1562,17 @@ abstract static class ReportAbstractClassNode extends PNodeWithContext {
15591562

15601563
@Specialization
15611564
static PException report(VirtualFrame frame, Object type,
1562-
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
1565+
@Cached PyObjectCallMethodObjArgs callSort,
1566+
@Cached PyObjectCallMethodObjArgs callJoin,
15631567
@Cached PyObjectSizeNode sizeNode,
15641568
@Cached ReadAttributeFromObjectNode readAttributeFromObjectNode,
15651569
@Cached CastToJavaStringNode cast,
15661570
@Cached ListNodes.ConstructListNode constructListNode,
15671571
@Cached PRaiseNode raiseNode) {
15681572
PList list = constructListNode.execute(frame, readAttributeFromObjectNode.execute(type, __ABSTRACTMETHODS__));
15691573
int methodCount = sizeNode.execute(frame, list);
1570-
lib.lookupAndCallRegularMethod(list, frame, "sort");
1571-
String joined = cast.execute(lib.lookupAndCallRegularMethod(", ", frame, "join", list));
1574+
callSort.execute(frame, list, "sort");
1575+
String joined = cast.execute(callJoin.execute(frame, ", ", "join", list));
15721576
throw raiseNode.raise(TypeError, "Can't instantiate abstract class %N with abstract method%s %s", type, methodCount > 1 ? "s" : "", joined);
15731577
}
15741578

@@ -3015,8 +3019,8 @@ Object methodBuiltin(@SuppressWarnings("unused") Object cls, PBuiltinFunction fu
30153019

30163020
@Specialization
30173021
Object methodGeneric(@SuppressWarnings("unused") Object cls, Object func, Object self,
3018-
@CachedLibrary(limit = "3") PythonObjectLibrary dataModelLibrary) {
3019-
if (dataModelLibrary.isCallable(func)) {
3022+
@Cached PyCallableCheckNode callableCheck) {
3023+
if (callableCheck.execute(func)) {
30203024
return factory().createMethod(self, func);
30213025
} else {
30223026
throw raise(TypeError, ErrorMessages.FIRST_ARG_MUST_BE_CALLABLE);
@@ -3189,30 +3193,20 @@ Object initArgs(Object cls, Object[] args, @SuppressWarnings("unused") PKeyword[
31893193
@Builtin(name = "mappingproxy", constructsClass = PythonBuiltinClassType.PMappingproxy, isPublic = false, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2)
31903194
@GenerateNodeFactory
31913195
public abstract static class MappingproxyNode extends PythonBuiltinNode {
3192-
@Specialization(guards = "isMapping(obj, lib)", limit = "1")
3193-
Object doMapping(Object klass, PythonObject obj,
3194-
@SuppressWarnings("unused") @CachedLibrary("obj") PythonObjectLibrary lib) {
3195-
return factory().createMappingproxy(klass, obj);
3196+
@Specialization(guards = "!isNoValue(obj)")
3197+
Object doMapping(Object klass, Object obj,
3198+
@Cached PyMappingCheckNode mappingCheckNode) {
3199+
if (mappingCheckNode.execute(obj)) {
3200+
return factory().createMappingproxy(klass, obj);
3201+
}
3202+
throw raise(TypeError, ErrorMessages.ARG_MUST_BE_S_NOT_P, "mappingproxy()", "mapping", obj);
31963203
}
31973204

31983205
@Specialization(guards = "isNoValue(none)")
31993206
@SuppressWarnings("unused")
32003207
Object doMissing(Object klass, PNone none) {
32013208
throw raise(TypeError, ErrorMessages.MISSING_D_REQUIRED_S_ARGUMENT_S_POS, "mappingproxy()", "mapping", 1);
32023209
}
3203-
3204-
@Specialization(guards = {"!isMapping(obj, lib)", "!isNoValue(obj)"}, limit = "1")
3205-
Object doInvalid(@SuppressWarnings("unused") Object klass, Object obj,
3206-
@SuppressWarnings("unused") @CachedLibrary("obj") PythonObjectLibrary lib) {
3207-
throw raise(TypeError, ErrorMessages.ARG_MUST_BE_S_NOT_P, "mappingproxy()", "mapping", obj);
3208-
}
3209-
3210-
protected static boolean isMapping(Object o, PythonObjectLibrary library) {
3211-
if (o instanceof PList || o instanceof PTuple) {
3212-
return false;
3213-
}
3214-
return library.isMapping(o);
3215-
}
32163210
}
32173211

32183212
abstract static class DescriptorNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)