Skip to content

Commit c935844

Browse files
committed
[GR-9160] [GR-7735] [GR-9221] [GR-9222] Support ContextPolicy.REUSE
PullRequest: graalpython/135
2 parents d3fc35e + 93ae2c1 commit c935844

File tree

71 files changed

+931
-1206
lines changed

Some content is hidden

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

71 files changed

+931
-1206
lines changed

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

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@
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;
4745
import java.nio.file.Path;
4846
import java.nio.file.Paths;
4947

5048
import org.graalvm.polyglot.Context;
49+
import org.graalvm.polyglot.Engine;
5150
import org.graalvm.polyglot.PolyglotException;
52-
import org.junit.BeforeClass;
5351

5452
import com.oracle.graal.python.PythonLanguage;
5553
import com.oracle.graal.python.runtime.PythonContext;
@@ -81,7 +79,26 @@ public class PythonTests {
8179
final static ByteArrayOutputStream outArray = new ByteArrayOutputStream();
8280
final static PrintStream errStream = new PrintStream(errArray);
8381
final static PrintStream outStream = new PrintStream(outArray);
84-
public static Context context = null;
82+
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) {
87+
PythonTests.outArray.reset();
88+
PythonTests.errArray.reset();
89+
if (context != null) {
90+
closeContext();
91+
}
92+
context = Context.newBuilder().engine(engine).allowAllAccess(true).arguments("python", newArgs).build();
93+
context.initialize("python");
94+
context.enter();
95+
}
96+
97+
public static void closeContext() {
98+
context.leave();
99+
context.close();
100+
context = null;
101+
}
85102

86103
public static void assertBenchNoError(Path scriptName, String[] args) {
87104
final ByteArrayOutputStream byteArrayErr = new ByteArrayOutputStream();
@@ -177,22 +194,6 @@ public static VirtualFrame createVirtualFrame() {
177194
return Truffle.getRuntime().createVirtualFrame(null, new FrameDescriptor());
178195
}
179196

180-
public static void ensureContext() {
181-
resetContext(new String[0]);
182-
// XXX
183-
Field field;
184-
try {
185-
field = PythonTests.context.getClass().getDeclaredField("impl");
186-
field.setAccessible(true);
187-
Object polyglotContextImpl = field.get(PythonTests.context);
188-
Method enterMethod = polyglotContextImpl.getClass().getDeclaredMethod("enter", new Class<?>[0]);
189-
enterMethod.setAccessible(true);
190-
enterMethod.invoke(polyglotContextImpl);
191-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
192-
e.printStackTrace();
193-
}
194-
}
195-
196197
static void flush(OutputStream out, OutputStream err) {
197198
PythonTests.outStream.flush();
198199
PythonTests.errStream.flush();
@@ -223,18 +224,6 @@ public static File getBenchFile(Path filename) {
223224
return file;
224225
}
225226

226-
public static PythonContext getContext() {
227-
PythonTests.ensureContext();
228-
return PythonLanguage.getContext();
229-
}
230-
231-
static Context getContext(String[] args) {
232-
resetContext(args);
233-
PythonTests.outArray.reset();
234-
PythonTests.errArray.reset();
235-
return PythonTests.context;
236-
}
237-
238227
private static String getFileContent(File file) {
239228
String ret = null;
240229
Reader reader;
@@ -262,8 +251,8 @@ private static String getFileContent(File file) {
262251
}
263252

264253
public static RootNode getParseResult(com.oracle.truffle.api.source.Source source, PrintStream out, PrintStream err) {
265-
PythonTests.ensureContext();
266-
PythonContext ctx = PythonLanguage.getContext();
254+
PythonTests.enterContext();
255+
PythonContext ctx = PythonLanguage.getContextRef().get();
267256
ctx.setOut(out);
268257
ctx.setErr(err);
269258
return (RootNode) ctx.getCore().getParser().parse(ParserMode.File, ctx.getCore(), source, null);
@@ -298,24 +287,10 @@ public static File getTestFile(Path filename) {
298287
throw new RuntimeException("Unable to locate " + path);
299288
}
300289

301-
public static void resetContext(String[] args) {
302-
if (PythonTests.context != null) {
303-
PythonTests.context.close();
304-
}
305-
org.graalvm.polyglot.Context.Builder ctxBuilder = Context.newBuilder();
306-
ctxBuilder.allowAllAccess(true);
307-
PythonTests.outArray.reset();
308-
PythonTests.errArray.reset();
309-
ctxBuilder.out(PythonTests.outStream);
310-
ctxBuilder.err(PythonTests.errStream);
311-
ctxBuilder.arguments("python", args);
312-
PythonTests.context = ctxBuilder.build();
313-
PythonTests.context.initialize("python");
314-
}
315-
316290
public static void runScript(String[] args, File path, OutputStream out, OutputStream err) {
317291
try {
318-
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());
319294
} catch (IOException e) {
320295
e.printStackTrace();
321296
throw new RuntimeException(e);
@@ -326,15 +301,17 @@ public static void runScript(String[] args, File path, OutputStream out, OutputS
326301

327302
public static void runScript(String[] args, String source, OutputStream out, OutputStream err) {
328303
try {
329-
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.create("python", source));
304+
enterContext(args);
305+
context.eval(org.graalvm.polyglot.Source.create("python", source));
330306
} finally {
331307
flush(out, err);
332308
}
333309
}
334310

335311
public static void runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
336312
try {
337-
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.create("python", source));
313+
enterContext(args);
314+
context.eval(org.graalvm.polyglot.Source.create("python", source));
338315
} finally {
339316
cb.run();
340317
flush(out, err);
@@ -365,10 +342,4 @@ public static void runThrowableScript(String[] args, String source, OutputStream
365342
}
366343
}
367344
}
368-
369-
@BeforeClass
370-
public static void setUp() {
371-
PythonTests.resetContext(new String[0]);
372-
}
373-
374345
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2017, 2018, 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.test.advance;
42+
43+
import org.graalvm.polyglot.Context;
44+
import org.graalvm.polyglot.Engine;
45+
import org.junit.Test;
46+
47+
import com.oracle.graal.python.test.PythonTests;
48+
49+
public class MultiContextTest extends PythonTests {
50+
@Test
51+
public void testContextReuse() {
52+
Engine engine = Engine.newBuilder().build();
53+
for (int i = 0; i < 10; i++) {
54+
try (Context context = newContext(engine)) {
55+
context.eval("python", "memoryview(b'abc')");
56+
}
57+
}
58+
}
59+
60+
private static Context newContext(Engine engine) {
61+
return Context.newBuilder().allowAllAccess(true).engine(engine).build();
62+
}
63+
}

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

0 commit comments

Comments
 (0)