Skip to content

Commit 8bcb50e

Browse files
committed
simplify context enter/leave for tests, use shared engine where possible, and remove a singleton holding on to the context reference
1 parent 09b4e69 commit 8bcb50e

File tree

13 files changed

+58
-85
lines changed

13 files changed

+58
-85
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import java.io.PrintStream;
4040
import java.io.Reader;
4141
import java.lang.reflect.Field;
42-
import java.lang.reflect.InvocationTargetException;
43-
import java.lang.reflect.Method;
4442
import java.net.JarURLConnection;
4543
import java.net.URLConnection;
4644
import java.nio.file.Files;
@@ -50,7 +48,6 @@
5048
import org.graalvm.polyglot.Context;
5149
import org.graalvm.polyglot.Engine;
5250
import org.graalvm.polyglot.PolyglotException;
53-
import org.junit.BeforeClass;
5451

5552
import com.oracle.graal.python.PythonLanguage;
5653
import com.oracle.graal.python.runtime.PythonContext;
@@ -82,21 +79,25 @@ public class PythonTests {
8279
final static ByteArrayOutputStream outArray = new ByteArrayOutputStream();
8380
final static PrintStream errStream = new PrintStream(errArray);
8481
final static PrintStream outStream = new PrintStream(outArray);
85-
public static Engine engine = Engine.newBuilder().out(PythonTests.outStream).err(PythonTests.errStream).build();
86-
public static Context context = null;
8782

88-
public static void resetContext(String[] args) {
89-
if (PythonTests.context != null) {
90-
PythonTests.context.close();
91-
}
92-
org.graalvm.polyglot.Context.Builder ctxBuilder = Context.newBuilder();
93-
ctxBuilder.engine(engine);
94-
ctxBuilder.allowAllAccess(true);
83+
private static Engine engine = Engine.newBuilder().out(PythonTests.outStream).err(PythonTests.errStream).build();
84+
private static Context context = null;
85+
86+
public static void enterContext(String... newArgs) {
9587
PythonTests.outArray.reset();
9688
PythonTests.errArray.reset();
97-
ctxBuilder.arguments("python", args);
98-
PythonTests.context = ctxBuilder.build();
99-
PythonTests.context.initialize("python");
89+
if (context != null) {
90+
context.leave();
91+
context.close();
92+
}
93+
context = Context.newBuilder().engine(engine).allowAllAccess(true).arguments("python", newArgs).build();
94+
context.initialize("python");
95+
context.enter();
96+
}
97+
98+
public static void closeContext() {
99+
context.leave();
100+
context.close();
100101
}
101102

102103
public static void assertBenchNoError(Path scriptName, String[] args) {
@@ -193,22 +194,6 @@ public static VirtualFrame createVirtualFrame() {
193194
return Truffle.getRuntime().createVirtualFrame(null, new FrameDescriptor());
194195
}
195196

196-
public static void ensureContext() {
197-
resetContext(new String[0]);
198-
// XXX
199-
Field field;
200-
try {
201-
field = PythonTests.context.getClass().getDeclaredField("impl");
202-
field.setAccessible(true);
203-
Object polyglotContextImpl = field.get(PythonTests.context);
204-
Method enterMethod = polyglotContextImpl.getClass().getDeclaredMethod("enter", new Class<?>[0]);
205-
enterMethod.setAccessible(true);
206-
enterMethod.invoke(polyglotContextImpl);
207-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
208-
e.printStackTrace();
209-
}
210-
}
211-
212197
static void flush(OutputStream out, OutputStream err) {
213198
PythonTests.outStream.flush();
214199
PythonTests.errStream.flush();
@@ -239,18 +224,6 @@ public static File getBenchFile(Path filename) {
239224
return file;
240225
}
241226

242-
public static PythonContext getContext() {
243-
PythonTests.ensureContext();
244-
return PythonLanguage.getContextRef().get();
245-
}
246-
247-
static Context getContext(String[] args) {
248-
resetContext(args);
249-
PythonTests.outArray.reset();
250-
PythonTests.errArray.reset();
251-
return PythonTests.context;
252-
}
253-
254227
private static String getFileContent(File file) {
255228
String ret = null;
256229
Reader reader;
@@ -278,7 +251,7 @@ private static String getFileContent(File file) {
278251
}
279252

280253
public static RootNode getParseResult(com.oracle.truffle.api.source.Source source, PrintStream out, PrintStream err) {
281-
PythonTests.ensureContext();
254+
PythonTests.enterContext();
282255
PythonContext ctx = PythonLanguage.getContextRef().get();
283256
ctx.setOut(out);
284257
ctx.setErr(err);
@@ -316,7 +289,8 @@ public static File getTestFile(Path filename) {
316289

317290
public static void runScript(String[] args, File path, OutputStream out, OutputStream err) {
318291
try {
319-
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.newBuilder("python", path).build());
292+
enterContext(args);
293+
context.eval(org.graalvm.polyglot.Source.newBuilder("python", path).build());
320294
} catch (IOException e) {
321295
e.printStackTrace();
322296
throw new RuntimeException(e);
@@ -327,15 +301,17 @@ public static void runScript(String[] args, File path, OutputStream out, OutputS
327301

328302
public static void runScript(String[] args, String source, OutputStream out, OutputStream err) {
329303
try {
330-
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.create("python", source));
304+
enterContext(args);
305+
context.eval(org.graalvm.polyglot.Source.create("python", source));
331306
} finally {
332307
flush(out, err);
333308
}
334309
}
335310

336311
public static void runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
337312
try {
338-
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.create("python", source));
313+
enterContext(args);
314+
context.eval(org.graalvm.polyglot.Source.create("python", source));
339315
} finally {
340316
cb.run();
341317
flush(out, err);
@@ -366,10 +342,4 @@ public static void runThrowableScript(String[] args, String source, OutputStream
366342
}
367343
}
368344
}
369-
370-
@BeforeClass
371-
public static void setUp() {
372-
PythonTests.resetContext(new String[0]);
373-
}
374-
375345
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/ImportTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class ImportTests {
3737
@Before
3838
public void setup() {
39-
PythonTests.resetContext(new String[0]);
39+
PythonTests.enterContext();
4040
}
4141

4242
@Test

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/IteratorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
public class IteratorTests {
3333
@Before
3434
public void setup() {
35-
PythonTests.resetContext(new String[0]);
35+
PythonTests.enterContext();
3636
}
3737
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/PRangeTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
public class PRangeTests {
4545
@Before
4646
public void setup() {
47-
PythonTests.ensureContext();
47+
PythonTests.enterContext();
4848
}
4949

5050
@Test
5151
public void loopWithOnlyStop() throws UnexpectedResultException {
52-
PRange range = PythonObjectFactory.get().createRange(10);
52+
PRange range = PythonObjectFactory.create().createRange(10);
5353
int index = 0;
5454
Object iter = GetIteratorNodeGen.create().executeWith(range);
5555
GetNextNode next = GetNextNode.create();
@@ -69,7 +69,7 @@ public void loopWithOnlyStop() throws UnexpectedResultException {
6969

7070
@Test
7171
public void loopWithStep() throws UnexpectedResultException {
72-
PRange range = PythonObjectFactory.get().createRange(0, 10, 2);
72+
PRange range = PythonObjectFactory.create().createRange(0, 10, 2);
7373
int index = 0;
7474
Object iter = GetIteratorNodeGen.create().executeWith(range);
7575
GetNextNode next = GetNextNode.create();
@@ -89,13 +89,13 @@ public void loopWithStep() throws UnexpectedResultException {
8989

9090
@Test
9191
public void getItem() {
92-
PRange range = PythonObjectFactory.get().createRange(10);
92+
PRange range = PythonObjectFactory.create().createRange(10);
9393
assertEquals(3, range.getItem(3));
9494
}
9595

9696
@Test
9797
public void getItemNegative() {
98-
PRange range = PythonObjectFactory.get().createRange(10);
98+
PRange range = PythonObjectFactory.create().createRange(10);
9999
assertEquals(7, range.getItem(-3));
100100
}
101101

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public class TestParserTranslator {
112112
PythonContext context;
113113

114114
public TestParserTranslator() {
115-
context = PythonTests.getContext();
115+
PythonTests.enterContext();
116+
context = PythonLanguage.getContextRef().get();
116117
}
117118

118119
RootNode parse(String src) {

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/module/AtexitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void atExit() {
6363
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
6464
final PrintStream printStream = new PrintStream(byteArray);
6565
PythonTests.runScript(new String[0], source, printStream, System.err, () -> {
66-
PythonTests.context.close();
66+
PythonTests.closeContext();
6767
});
6868
String result = byteArray.toString().replaceAll("\r\n", "\n");
6969
assertEquals("arg kw\n", result);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/module/MathTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
import static com.oracle.graal.python.test.PythonTests.*;
3333

3434
public class MathTests {
35+
@Before
36+
public void setUp() {
37+
PythonTests.enterContext();
38+
}
3539

3640
@Test
3741
public void piAndE() {
3842
String source = "import math\n" + //
3943
"print(math.e)\n" + //
4044
"print(math.pi)\n" + //
4145
"print(math.e + math.pi)\n";
42-
PythonTests.resetContext(new String[0]);
4346
assertPrints("2.718281828459045\n" + "3.141592653589793\n" + "5.859874482048838\n", source);
4447
}
4548

@@ -50,7 +53,6 @@ public void logAndExp() {
5053
"b = 10\n" + //
5154
"c = math.exp(b)\n" + //
5255
"print(a // c)\n";
53-
PythonTests.resetContext(new String[0]);
5456
assertPrints("0.0\n", source);
5557
}
5658

@@ -60,7 +62,6 @@ public void sinAndCos() {
6062
"x = math.pi\n" + //
6163
"y = math.sin(x)\n" + //
6264
"print(y // math.cos(math.e))\n";
63-
PythonTests.resetContext(new String[0]);
6465
assertPrints("-1.0\n", source);
6566
}
6667

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/module/RandomTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727

2828
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2929

30+
import org.junit.Before;
3031
import org.junit.Test;
3132

3233
import com.oracle.graal.python.test.PythonTests;
3334

3435
public class RandomTests {
36+
@Before
37+
public void setUp() {
38+
PythonTests.enterContext();
39+
}
3540

3641
@Test
3742
public void randomRandom() {
3843
String source = "import random\n" + //
3944
"random.seed(1)\n" + //
4045
"print(int(random.random()))\n";
41-
PythonTests.resetContext(new String[0]);
4246
assertPrints("0\n", source);
4347
}
4448

@@ -49,7 +53,6 @@ public void randRange0() {
4953
"stop = 10\n" + //
5054
"ran = random.randrange(stop)\n" + //
5155
"print(ran // stop)\n";
52-
PythonTests.resetContext(new String[0]);
5356
assertPrints("0\n", source);
5457
}
5558

@@ -59,7 +62,6 @@ public void randRange1() {
5962
"stop = 9223372036854775807\n" + //
6063
"ran = random.randrange(stop)\n" + //
6164
"print(ran // stop)\n";
62-
PythonTests.resetContext(new String[0]);
6365
assertPrints("0\n", source);
6466
}
6567

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/GetAttributeDispatchTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@
2727

2828
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2929

30+
import org.junit.Before;
3031
import org.junit.Test;
3132

3233
import com.oracle.graal.python.test.PythonTests;
3334

3435
public class GetAttributeDispatchTests {
36+
@Before
37+
public void setUp() {
38+
PythonTests.enterContext();
39+
}
3540

3641
@Test
3742
public void cachedModuleAttr() {
3843
String source = "import time\n" + //
3944
"for i in range(2):\n" + //
4045
" time.foo = 42\n" + //
4146
" print(time.foo)\n";
42-
PythonTests.resetContext(new String[0]);
4347
assertPrints("42\n42\n", source);
4448
}
4549

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/PythonModuleTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__;
3232
import static org.junit.Assert.assertEquals;
3333

34+
import org.junit.Before;
3435
import org.junit.Test;
3536

37+
import com.oracle.graal.python.PythonLanguage;
3638
import com.oracle.graal.python.builtins.objects.function.PArguments;
3739
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
3840
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -43,10 +45,16 @@
4345
import com.oracle.graal.python.test.PythonTests;
4446

4547
public class PythonModuleTests {
48+
private PythonContext context;
49+
50+
@Before
51+
public void setUp() {
52+
PythonTests.enterContext();
53+
context = PythonLanguage.getContextRef().get();
54+
}
4655

4756
@Test
4857
public void pythonModuleTest() {
49-
final PythonContext context = PythonTests.getContext();
5058
PythonModule module = context.getCore().factory().createPythonModule("testModule");
5159

5260
assertEquals("testModule", module.getAttribute(__NAME__).toString());
@@ -56,7 +64,6 @@ public void pythonModuleTest() {
5664

5765
@Test
5866
public void builtinsMinTest() {
59-
final PythonContext context = PythonTests.getContext();
6067
final PythonModule builtins = context.getBuiltins();
6168
PBuiltinMethod min = (PBuiltinMethod) builtins.getAttribute(BuiltinNames.MIN);
6269
Object returnValue = InvokeNode.create(min).invoke(createWithUserArguments(builtins, 4, 2, 1));
@@ -65,7 +72,6 @@ public void builtinsMinTest() {
6572

6673
@Test
6774
public void builtinsIntTest() {
68-
final PythonContext context = PythonTests.getContext();
6975
final PythonModule builtins = context.getBuiltins();
7076
PythonBuiltinClass intClass = (PythonBuiltinClass) builtins.getAttribute(BuiltinNames.INT);
7177
Object returnValue = InvokeNode.create(intClass).invoke(createWithUserArguments(intClass, "42"));
@@ -74,7 +80,6 @@ public void builtinsIntTest() {
7480

7581
@Test
7682
public void mainModuleTest() {
77-
final PythonContext context = PythonTests.getContext();
7883
PythonModule main = context.getMainModule();
7984
PythonModule builtins = (PythonModule) main.getAttribute(__BUILTINS__);
8085
PBuiltinMethod abs = (PBuiltinMethod) builtins.getAttribute(BuiltinNames.ABS);

0 commit comments

Comments
 (0)