Skip to content

Commit da4903c

Browse files
[GR-29490] Avoid usage of context in PBaseException.toString.
PullRequest: graalpython/1716
2 parents d30b29a + 3b90879 commit da4903c

File tree

13 files changed

+354
-289
lines changed

13 files changed

+354
-289
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import org.junit.Rule;
44+
import org.junit.rules.ExpectedException;
45+
46+
import com.oracle.graal.python.PythonLanguage;
47+
import com.oracle.graal.python.builtins.objects.frame.PFrame;
48+
import com.oracle.graal.python.builtins.objects.function.PArguments;
49+
import com.oracle.graal.python.builtins.objects.function.Signature;
50+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
51+
import com.oracle.graal.python.nodes.PRootNode;
52+
import com.oracle.graal.python.nodes.call.CallTargetInvokeNode;
53+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise;
54+
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
55+
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
56+
import com.oracle.graal.python.runtime.PythonContext;
57+
import com.oracle.graal.python.runtime.exception.PException;
58+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
59+
import com.oracle.truffle.api.RootCallTarget;
60+
import com.oracle.truffle.api.Truffle;
61+
import com.oracle.truffle.api.frame.VirtualFrame;
62+
63+
public class ConversionNodeTests {
64+
static final Signature SIGNATURE = new Signature(-1, false, -1, false, new String[]{"arg"}, null);
65+
@Rule public ExpectedException expectedException = ExpectedException.none();
66+
67+
protected static Object call(Object arg, ArgumentCastNodeWithRaise castNode) {
68+
final PythonContext pythonContext = PythonLanguage.getContext();
69+
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(null) {
70+
@Child private CalleeContext calleeContext = CalleeContext.create();
71+
@Child private ArgumentCastNodeWithRaise node = castNode;
72+
73+
@Override
74+
public Object execute(VirtualFrame frame) {
75+
calleeContext.enter(frame);
76+
try {
77+
return node.execute(frame, PArguments.getArgument(frame, 0));
78+
} finally {
79+
calleeContext.exit(frame, this);
80+
}
81+
}
82+
83+
@Override
84+
public Signature getSignature() {
85+
return SIGNATURE;
86+
}
87+
88+
@Override
89+
public boolean isPythonInternal() {
90+
return true;
91+
}
92+
});
93+
try {
94+
Object[] arguments = PArguments.create(1);
95+
PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict());
96+
PArguments.setException(arguments, PException.NO_EXCEPTION);
97+
PArguments.setArgument(arguments, 0, arg);
98+
PFrame.Reference frameInfo = IndirectCalleeContext.enter(pythonContext, arguments, callTarget);
99+
try {
100+
return CallTargetInvokeNode.invokeUncached(callTarget, arguments);
101+
} finally {
102+
IndirectCalleeContext.exit(pythonContext, frameInfo);
103+
}
104+
} catch (PException e) {
105+
// materialize PException's error message since we are leaving Python
106+
PException exceptionForReraise = e.getExceptionForReraise();
107+
PythonObjectLibrary pythonObjectLibrary = PythonObjectLibrary.getUncached();
108+
exceptionForReraise.setMessage(exceptionForReraise.getUnreifiedException().getFormattedMessage(pythonObjectLibrary, pythonObjectLibrary));
109+
throw exceptionForReraise;
110+
}
111+
}
112+
113+
protected void expectPythonMessage(String expectedMessage) {
114+
expectedException.expect(PException.class);
115+
expectedException.expectMessage(expectedMessage);
116+
}
117+
}

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

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,13 @@
5050
import org.junit.After;
5151
import org.junit.Assert;
5252
import org.junit.Before;
53-
import org.junit.Rule;
5453
import org.junit.Test;
55-
import org.junit.rules.ExpectedException;
5654

57-
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.DirFdConversionNode;
5855
import com.oracle.graal.python.builtins.objects.PNone;
59-
import com.oracle.graal.python.builtins.objects.frame.PFrame;
60-
import com.oracle.graal.python.builtins.objects.function.PArguments;
61-
import com.oracle.graal.python.runtime.ExecutionContext;
62-
import com.oracle.graal.python.runtime.PythonContext;
63-
import com.oracle.graal.python.runtime.exception.PException;
6456
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6557
import com.oracle.graal.python.test.PythonTests;
66-
import com.oracle.truffle.api.RootCallTarget;
67-
import com.oracle.truffle.api.Truffle;
68-
import com.oracle.truffle.api.frame.VirtualFrame;
69-
import com.oracle.truffle.api.nodes.RootNode;
7058

71-
public class DirFdConversionNodeTests {
72-
@Rule public ExpectedException expectedException = ExpectedException.none();
59+
public class DirFdConversionNodeTests extends ConversionNodeTests {
7360

7461
@Before
7562
public void setUp() {
@@ -105,15 +92,13 @@ public void longFitsInt() {
10592

10693
@Test
10794
public void longTooBig() {
108-
expectedException.expect(PException.class);
109-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
95+
expectPythonMessage("OverflowError: fd is greater than maximum");
11096
call(1L << 40);
11197
}
11298

11399
@Test
114100
public void longTooSmall() {
115-
expectedException.expect(PException.class);
116-
expectedException.expectMessage("OverflowError: fd is less than minimum");
101+
expectPythonMessage("OverflowError: fd is less than minimum");
117102
call(-1L << 40);
118103
}
119104

@@ -124,15 +109,13 @@ public void pintFitsInt() {
124109

125110
@Test
126111
public void pintTooBig() {
127-
expectedException.expect(PException.class);
128-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
112+
expectPythonMessage("OverflowError: fd is greater than maximum");
129113
call(factory().createInt(BigInteger.ONE.shiftLeft(100)));
130114
}
131115

132116
@Test
133117
public void pintTooSmall() {
134-
expectedException.expect(PException.class);
135-
expectedException.expectMessage("OverflowError: fd is less than minimum");
118+
expectPythonMessage("OverflowError: fd is less than minimum");
136119
call(factory().createInt(BigInteger.ONE.shiftLeft(100).negate()));
137120
}
138121

@@ -143,15 +126,13 @@ public void indexFitsInt() {
143126

144127
@Test
145128
public void indexTooBig() {
146-
expectedException.expect(PException.class);
147-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
129+
expectPythonMessage("OverflowError: fd is greater than maximum");
148130
call(evalValue("class C:\n def __index__(self):\n return 1 << 40\nC()"));
149131
}
150132

151133
@Test
152134
public void indexTooSmall() {
153-
expectedException.expect(PException.class);
154-
expectedException.expectMessage("OverflowError: fd is less than minimum");
135+
expectPythonMessage("OverflowError: fd is less than minimum");
155136
call(evalValue("class C:\n def __index__(self):\n return -1 << 100\nC()"));
156137
}
157138

@@ -162,30 +143,12 @@ public void indexInIntSubclass() {
162143

163144
@Test
164145
public void unsupportedType1() {
165-
expectedException.expect(PException.class);
166-
expectedException.expectMessage("TypeError: argument should be integer or None, not float");
146+
expectPythonMessage("TypeError: argument should be integer or None, not float");
167147
call(3.14);
168148
}
169149

170-
private static int call(Object arg) {
171-
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new RootNode(null) {
172-
@Child private DirFdConversionNode node = PosixModuleBuiltinsFactory.DirFdConversionNodeGen.create();
173-
174-
@Override
175-
public Object execute(VirtualFrame frame) {
176-
Object[] arguments = PArguments.create(0);
177-
PythonContext pythonContext = getContext();
178-
PArguments.setGlobals(arguments, pythonContext.getCore().factory().createDict());
179-
PFrame.Reference frameInfo = ExecutionContext.IndirectCalleeContext.enterIndirect(pythonContext, arguments);
180-
PArguments.setCurrentFrameInfo(arguments, frameInfo);
181-
try {
182-
return node.execute(Truffle.getRuntime().createMaterializedFrame(arguments), arg);
183-
} finally {
184-
ExecutionContext.IndirectCalleeContext.exit(pythonContext, frameInfo);
185-
}
186-
}
187-
});
188-
Object result = callTarget.call();
150+
protected static int call(Object arg) {
151+
Object result = call(arg, PosixModuleBuiltinsFactory.DirFdConversionNodeGen.create());
189152
Assert.assertThat(result, CoreMatchers.instanceOf(Integer.class));
190153
return (int) result;
191154
}

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

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,13 @@
5050
import org.junit.After;
5151
import org.junit.Assert;
5252
import org.junit.Before;
53-
import org.junit.Rule;
5453
import org.junit.Test;
55-
import org.junit.rules.ExpectedException;
5654

57-
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.FileDescriptorConversionNode;
5855
import com.oracle.graal.python.builtins.objects.PNone;
59-
import com.oracle.graal.python.builtins.objects.frame.PFrame;
60-
import com.oracle.graal.python.builtins.objects.function.PArguments;
61-
import com.oracle.graal.python.runtime.ExecutionContext;
62-
import com.oracle.graal.python.runtime.PythonContext;
63-
import com.oracle.graal.python.runtime.exception.PException;
6456
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6557
import com.oracle.graal.python.test.PythonTests;
66-
import com.oracle.truffle.api.RootCallTarget;
67-
import com.oracle.truffle.api.Truffle;
68-
import com.oracle.truffle.api.frame.VirtualFrame;
69-
import com.oracle.truffle.api.nodes.RootNode;
7058

71-
public class FileDescriptorConversionNodeTests {
72-
@Rule public ExpectedException expectedException = ExpectedException.none();
59+
public class FileDescriptorConversionNodeTests extends ConversionNodeTests {
7360

7461
@Before
7562
public void setUp() {
@@ -83,16 +70,14 @@ public void tearDown() {
8370

8471
@Test
8572
public void none() {
86-
expectedException.expect(PException.class);
87-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
73+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
8874
call(PNone.NONE);
8975
Assert.assertEquals(AT_FDCWD.value, call(PNone.NO_VALUE));
9076
}
9177

9278
@Test
9379
public void noValue() {
94-
expectedException.expect(PException.class);
95-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
80+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
9681
call(PNone.NO_VALUE);
9782
}
9883

@@ -114,15 +99,13 @@ public void longFitsInt() {
11499

115100
@Test
116101
public void longTooBig() {
117-
expectedException.expect(PException.class);
118-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
102+
expectPythonMessage("OverflowError: Python int too large to convert to int");
119103
call(1L << 40);
120104
}
121105

122106
@Test
123107
public void longTooSmall() {
124-
expectedException.expect(PException.class);
125-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
108+
expectPythonMessage("OverflowError: Python int too large to convert to int");
126109
call(-1L << 40);
127110
}
128111

@@ -133,22 +116,19 @@ public void pintFitsInt() {
133116

134117
@Test
135118
public void pintTooBig() {
136-
expectedException.expect(PException.class);
137-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
119+
expectPythonMessage("OverflowError: Python int too large to convert to int");
138120
call(factory().createInt(BigInteger.ONE.shiftLeft(100)));
139121
}
140122

141123
@Test
142124
public void pintTooSmall() {
143-
expectedException.expect(PException.class);
144-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
125+
expectPythonMessage("OverflowError: Python int too large to convert to int");
145126
call(factory().createInt(BigInteger.ONE.shiftLeft(100).negate()));
146127
}
147128

148129
@Test
149130
public void indexNotUsed() {
150-
expectedException.expect(PException.class);
151-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
131+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
152132
call(evalValue("class C:\n def __index__(self):\n return 42\nC()"));
153133
}
154134

@@ -159,44 +139,24 @@ public void filenoOk() {
159139

160140
@Test
161141
public void filenoWrongType() {
162-
expectedException.expect(PException.class);
163-
expectedException.expectMessage("TypeError: fileno() returned a non-integer");
142+
expectPythonMessage("TypeError: fileno() returned a non-integer");
164143
call(evalValue("class C:\n def fileno(self):\n return 3.14\nC()"));
165144
}
166145

167146
@Test
168147
public void filenoTooBig() {
169-
expectedException.expect(PException.class);
170-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
148+
expectPythonMessage("OverflowError: Python int too large to convert to int");
171149
call(evalValue("class C:\n def fileno(self):\n return 1 << 40\nC()"));
172150
}
173151

174152
@Test
175153
public void unsupportedType1() {
176-
expectedException.expect(PException.class);
177-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
154+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
178155
call(3.14);
179156
}
180157

181-
private static int call(Object arg) {
182-
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new RootNode(null) {
183-
@Child private FileDescriptorConversionNode node = PosixModuleBuiltinsFactory.FileDescriptorConversionNodeGen.create();
184-
185-
@Override
186-
public Object execute(VirtualFrame frame) {
187-
Object[] arguments = PArguments.create(0);
188-
PythonContext pythonContext = getContext();
189-
PArguments.setGlobals(arguments, pythonContext.getCore().factory().createDict());
190-
PFrame.Reference frameInfo = ExecutionContext.IndirectCalleeContext.enterIndirect(pythonContext, arguments);
191-
PArguments.setCurrentFrameInfo(arguments, frameInfo);
192-
try {
193-
return node.execute(Truffle.getRuntime().createMaterializedFrame(arguments), arg);
194-
} finally {
195-
ExecutionContext.IndirectCalleeContext.exit(pythonContext, frameInfo);
196-
}
197-
}
198-
});
199-
Object result = callTarget.call();
158+
protected static int call(Object arg) {
159+
Object result = call(arg, PosixModuleBuiltinsFactory.FileDescriptorConversionNodeGen.create());
200160
Assert.assertThat(result, CoreMatchers.instanceOf(Integer.class));
201161
return (int) result;
202162
}

0 commit comments

Comments
 (0)