Skip to content

Commit e0a2bc3

Browse files
committed
make sure we properly enter and leave the context around all tests
1 parent a4fc609 commit e0a2bc3

File tree

14 files changed

+151
-103
lines changed

14 files changed

+151
-103
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.truffle.api.nodes.RootNode;
5757
import org.graalvm.polyglot.Context;
5858
import org.hamcrest.CoreMatchers;
59+
import org.junit.After;
5960
import org.junit.Assert;
6061
import org.junit.Before;
6162
import org.junit.Rule;
@@ -74,6 +75,11 @@ public void setUp() {
7475
PythonTests.enterContext();
7576
}
7677

78+
@After
79+
public void tearDown() {
80+
PythonTests.closeContext();
81+
}
82+
7783
@Test
7884
public void none() {
7985
Assert.assertEquals(PosixSupportLibrary.DEFAULT_DIR_FD, call(PNone.NONE));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import org.graalvm.polyglot.Context;
4848
import org.hamcrest.CoreMatchers;
49+
import org.junit.After;
4950
import org.junit.Assert;
5051
import org.junit.Before;
5152
import org.junit.Rule;
@@ -75,6 +76,11 @@ public void setUp() {
7576
PythonTests.enterContext();
7677
}
7778

79+
@After
80+
public void tearDown() {
81+
PythonTests.closeContext();
82+
}
83+
7884
@Test
7985
public void none() {
8086
expectedException.expect(PException.class);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/nodes/util/CastToJavaUnsignedLongNodeTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.truffle.api.Truffle;
5050
import com.oracle.truffle.api.frame.VirtualFrame;
5151
import com.oracle.truffle.api.nodes.RootNode;
52+
import org.junit.After;
5253
import org.junit.Assert;
5354
import org.junit.Before;
5455
import org.junit.Test;
@@ -66,6 +67,11 @@ public void setUp() {
6667
PythonTests.enterContext();
6768
}
6869

70+
@After
71+
public void tearDown() {
72+
PythonTests.closeContext();
73+
}
74+
6975
@Test
7076
public void positiveInt() {
7177
Assert.assertEquals(0, castInt(0));

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/nodes/util/NarrowBigIntegerNodeTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141

4242
package com.oracle.graal.python.nodes.util;
4343

44-
import com.oracle.graal.python.builtins.objects.ints.PInt;
45-
import com.oracle.graal.python.test.PythonTests;
44+
import java.math.BigInteger;
45+
46+
import org.junit.After;
4647
import org.junit.Assert;
4748
import org.junit.Before;
4849
import org.junit.Test;
4950

50-
import java.math.BigInteger;
51+
import com.oracle.graal.python.builtins.objects.ints.PInt;
52+
import com.oracle.graal.python.test.PythonTests;
5153

5254
public class NarrowBigIntegerNodeTests {
5355

@@ -56,6 +58,11 @@ public void setUp() {
5658
PythonTests.enterContext();
5759
}
5860

61+
@After
62+
public void tearDown() {
63+
PythonTests.closeContext();
64+
}
65+
5966
private static NarrowBigIntegerNode narrow = NarrowBigIntegerNodeGen.getUncached();
6067

6168
@Test

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,19 @@ public class PythonTests {
9898
executable = sb.toString();
9999
}
100100

101-
public static void enterContext(String... newArgs) {
102-
enterContext(Collections.emptyMap(), newArgs);
101+
public static Context enterContext(String... newArgs) {
102+
return enterContext(Collections.emptyMap(), newArgs);
103103
}
104104

105-
public static void enterContext(Map<String, String> options, String[] args) {
105+
public static Context enterContext(Map<String, String> options, String[] args) {
106106
PythonTests.outArray.reset();
107107
PythonTests.errArray.reset();
108108
Context prevContext = context;
109109
context = Context.newBuilder().engine(engine).allowExperimentalOptions(true).allowAllAccess(true).options(options).arguments("python", args).option("python.Executable", executable).build();
110110
context.initialize("python");
111-
if (prevContext != null) {
112-
closeContext(prevContext);
113-
}
111+
assert prevContext == null;
114112
context.enter();
113+
return context;
115114
}
116115

117116
private static void closeContext(Context ctxt) {
@@ -290,11 +289,15 @@ private static String getFileContent(File file) {
290289
}
291290

292291
public static RootNode getParseResult(com.oracle.truffle.api.source.Source source, PrintStream out, PrintStream err) {
293-
PythonTests.enterContext();
294-
PythonContext ctx = PythonLanguage.getContext();
295-
ctx.setOut(out);
296-
ctx.setErr(err);
297-
return (RootNode) ctx.getCore().getParser().parse(ParserMode.File, 0, ctx.getCore(), source, null, null);
292+
enterContext();
293+
try {
294+
PythonContext ctx = PythonLanguage.getContext();
295+
ctx.setOut(out);
296+
ctx.setErr(err);
297+
return (RootNode) ctx.getCore().getParser().parse(ParserMode.File, 0, ctx.getCore(), source, null, null);
298+
} finally {
299+
closeContext();
300+
}
298301
}
299302

300303
public static RootNode getParseResult(String code) {
@@ -336,6 +339,7 @@ public static Value runScript(String[] args, File path, OutputStream out, Output
336339
throw new RuntimeException(e);
337340
} finally {
338341
flush(out, err);
342+
closeContext();
339343
}
340344
}
341345

@@ -345,6 +349,7 @@ public static Value runScript(Map<String, String> options, String[] args, String
345349
return context.eval(org.graalvm.polyglot.Source.create("python", source));
346350
} finally {
347351
flush(out, err);
352+
closeContext();
348353
}
349354
}
350355

@@ -354,6 +359,7 @@ public static Value runScript(String[] args, String source, OutputStream out, Ou
354359
return context.eval(org.graalvm.polyglot.Source.create("python", source));
355360
} finally {
356361
flush(out, err);
362+
closeContext();
357363
}
358364
}
359365

@@ -363,6 +369,7 @@ public static Value runScript(String[] args, org.graalvm.polyglot.Source source,
363369
return context.eval(source);
364370
} finally {
365371
flush(out, err);
372+
closeContext();
366373
}
367374
}
368375

@@ -377,6 +384,7 @@ public static Value runScript(Map<String, String> options, String[] args, String
377384
} finally {
378385
cb.run();
379386
flush(out, err);
387+
closeContext();
380388
}
381389
}
382390

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.Assert.assertEquals;
3131
import static org.junit.Assert.assertTrue;
3232

33+
import org.graalvm.polyglot.Context;
3334
import org.graalvm.polyglot.Value;
3435
import org.junit.Test;
3536

@@ -369,11 +370,16 @@ public void cellType() {
369370
" return bar\n" +
370371
"\n" +
371372
"foo(3).__closure__[0]";
372-
Value eval = PythonTests.eval(source);
373-
assertTrue(eval.getMetaObject().hasMember("__name__"));
374-
Value type = eval.getMetaObject().getMember("__name__");
375-
assertTrue(type.isString());
376-
assertEquals("cell", type.asString());
373+
Context context = PythonTests.enterContext();
374+
try {
375+
Value eval = context.eval("python", source);
376+
assertTrue(eval.getMetaObject().hasMember("__name__"));
377+
Value type = eval.getMetaObject().getMember("__name__");
378+
assertTrue(type.isString());
379+
assertEquals("cell", type.asString());
380+
} finally {
381+
PythonTests.closeContext();
382+
}
377383
}
378384

379385
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,15 @@
2525
*/
2626
package com.oracle.graal.python.test.builtin;
2727

28-
import java.nio.file.*;
28+
import static com.oracle.graal.python.test.PythonTests.assertPrintContains;
29+
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2930

30-
import org.junit.*;
31+
import java.nio.file.Path;
32+
import java.nio.file.Paths;
3133

32-
import com.oracle.graal.python.test.PythonTests;
33-
34-
import static com.oracle.graal.python.test.PythonTests.*;
34+
import org.junit.Test;
3535

3636
public class ImportTests {
37-
@Before
38-
public void setup() {
39-
PythonTests.enterContext();
40-
}
41-
4237
@Test
4338
public void relativeImportTest() {
4439
Path script = Paths.get("relative_import.py");

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
package com.oracle.graal.python.test.builtin;
2727

28+
import org.junit.After;
2829
import org.junit.Before;
2930

3031
import com.oracle.graal.python.test.PythonTests;
@@ -34,4 +35,9 @@ public class IteratorTests {
3435
public void setup() {
3536
PythonTests.enterContext();
3637
}
38+
39+
@After
40+
public void tearDown() {
41+
PythonTests.closeContext();
42+
}
3743
}

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

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2929
import static org.junit.Assert.assertEquals;
3030

31-
import org.junit.Before;
3231
import org.junit.Test;
3332

3433
import com.oracle.graal.python.PythonLanguage;
@@ -46,11 +45,6 @@
4645
import com.oracle.truffle.api.nodes.UnexpectedResultException;
4746

4847
public class PRangeTests {
49-
@Before
50-
public void setup() {
51-
PythonTests.enterContext();
52-
}
53-
5448
static class TestRoot extends RootNode {
5549
protected TestRoot(PythonLanguage language) {
5650
super(language);
@@ -68,52 +62,67 @@ public void doInsert(Node child) {
6862

6963
@Test
7064
public void loopWithOnlyStop() throws UnexpectedResultException {
71-
PRange range = PythonObjectFactory.getUncached().createIntRange(10);
72-
int index = 0;
73-
TestRoot testRoot = new TestRoot(PythonLanguage.getCurrent());
74-
Object iter = PythonObjectLibrary.getUncached().getIterator(range);
75-
GetNextNode next = GetNextNode.create();
76-
testRoot.doInsert(next);
77-
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.getUncached();
65+
PythonTests.enterContext();
66+
try {
67+
PRange range = PythonObjectFactory.getUncached().createIntRange(10);
68+
int index = 0;
69+
TestRoot testRoot = new TestRoot(PythonLanguage.getCurrent());
70+
Object iter = PythonObjectLibrary.getUncached().getIterator(range);
71+
GetNextNode next = GetNextNode.create();
72+
testRoot.doInsert(next);
73+
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.getUncached();
7874

79-
while (true) {
80-
try {
81-
int item = next.executeInt(null, iter);
82-
assertEquals(index, item);
83-
} catch (PException e) {
84-
e.expectStopIteration(errorProfile);
85-
break;
75+
while (true) {
76+
try {
77+
int item = next.executeInt(null, iter);
78+
assertEquals(index, item);
79+
} catch (PException e) {
80+
e.expectStopIteration(errorProfile);
81+
break;
82+
}
83+
index++;
8684
}
87-
index++;
85+
} finally {
86+
PythonTests.closeContext();
8887
}
8988
}
9089

9190
@Test
9291
public void loopWithStep() throws UnexpectedResultException {
93-
PRange range = PythonObjectFactory.getUncached().createIntRange(0, 10, 2, 5);
94-
int index = 0;
95-
TestRoot testRoot = new TestRoot(PythonLanguage.getCurrent());
96-
Object iter = PythonObjectLibrary.getUncached().getIterator(range);
97-
GetNextNode next = GetNextNode.create();
98-
testRoot.doInsert(next);
99-
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.getUncached();
92+
PythonTests.enterContext();
93+
try {
94+
PRange range = PythonObjectFactory.getUncached().createIntRange(0, 10, 2, 5);
95+
int index = 0;
96+
TestRoot testRoot = new TestRoot(PythonLanguage.getCurrent());
97+
Object iter = PythonObjectLibrary.getUncached().getIterator(range);
98+
GetNextNode next = GetNextNode.create();
99+
testRoot.doInsert(next);
100+
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.getUncached();
100101

101-
while (true) {
102-
try {
103-
int item = next.executeInt(null, iter);
104-
assertEquals(index, item);
105-
} catch (PException e) {
106-
e.expectStopIteration(errorProfile);
107-
break;
102+
while (true) {
103+
try {
104+
int item = next.executeInt(null, iter);
105+
assertEquals(index, item);
106+
} catch (PException e) {
107+
e.expectStopIteration(errorProfile);
108+
break;
109+
}
110+
index += 2;
108111
}
109-
index += 2;
112+
} finally {
113+
PythonTests.closeContext();
110114
}
111115
}
112116

113117
@Test
114118
public void getItem() {
115-
PIntRange range = PythonObjectFactory.getUncached().createIntRange(10);
116-
assertEquals(3, range.getIntItemNormalized(3));
119+
PythonTests.enterContext();
120+
try {
121+
PIntRange range = PythonObjectFactory.getUncached().createIntRange(10);
122+
assertEquals(3, range.getIntItemNormalized(3));
123+
} finally {
124+
PythonTests.closeContext();
125+
}
117126
}
118127

119128
@Test

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@
2525
*/
2626
package com.oracle.graal.python.test.module;
2727

28-
import org.junit.*;
28+
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2929

30-
import com.oracle.graal.python.test.PythonTests;
31-
32-
import static com.oracle.graal.python.test.PythonTests.*;
30+
import org.junit.Test;
3331

3432
public class MathTests {
35-
@Before
36-
public void setUp() {
37-
PythonTests.enterContext();
38-
}
39-
4033
@Test
4134
public void piAndE() {
4235
String source = "import math\n" + //

0 commit comments

Comments
 (0)