Skip to content

Commit 7f6eaba

Browse files
committed
[GR-34574] Embed Python3Core into PythonContext to avoid indirection.
PullRequest: graalpython/2000
2 parents 1873128 + cb4c3ec commit 7f6eaba

File tree

67 files changed

+291
-324
lines changed

Some content is hidden

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

67 files changed

+291
-324
lines changed

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/ParserBenchRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public ParserBenchRunner() {
107107
context.enter();
108108

109109
this.pyContext = PythonContext.get(null);
110-
this.parser = (PythonParserImpl) pyContext.getCore().getParser();
111-
this.core = pyContext.getCore();
110+
this.parser = (PythonParserImpl) pyContext.getParser();
111+
this.core = pyContext;
112112
}
113113

114114
public static void main(String[] args) throws RunnerException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public boolean isPythonInternal() {
9696
}.getCallTarget();
9797
try {
9898
Object[] arguments = PArguments.create(1);
99-
PArguments.setGlobals(arguments, pythonContext.getCore().factory().createDict());
99+
PArguments.setGlobals(arguments, pythonContext.factory().createDict());
100100
PArguments.setException(arguments, PException.NO_EXCEPTION);
101101
PArguments.setArgument(arguments, 0, arg);
102102
PythonThreadState threadState = pythonContext.getThreadState(language);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public static RootNode getParseResult(com.oracle.truffle.api.source.Source sourc
297297
PythonContext ctx = PythonContext.get(null);
298298
ctx.setOut(out);
299299
ctx.setErr(err);
300-
return (RootNode) ctx.getCore().getParser().parse(ParserMode.File, 0, ctx.getCore(), source, null, null);
300+
return (RootNode) ctx.getParser().parse(ParserMode.File, 0, ctx, source, null, null);
301301
} finally {
302302
closeContext();
303303
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ParserTestBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ protected Source createSource(File testFile) throws Exception {
113113

114114
public Node parse(String src, String moduleName, PythonParser.ParserMode mode, Frame fd) {
115115
Source source = Source.newBuilder(PythonLanguage.ID, src, moduleName).build();
116-
PythonParser parser = context.getCore().getParser();
117-
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context.getCore(), source, fd, null);
116+
PythonParser parser = context.getParser();
117+
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context, source, fd, null);
118118
lastGlobalScope = ((PythonParserImpl) parser).getLastGlobaScope();
119119
lastSST = ((PythonParserImpl) parser).getLastSST();
120120
return result;
@@ -125,8 +125,8 @@ public Node parse(String src, String moduleName, PythonParser.ParserMode mode) {
125125
}
126126

127127
public Node parse(Source source, PythonParser.ParserMode mode) {
128-
PythonParser parser = context.getCore().getParser();
129-
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context.getCore(), source, null, null);
128+
PythonParser parser = context.getParser();
129+
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context, source, null, null);
130130
lastGlobalScope = ((PythonParserImpl) parser).getLastGlobaScope();
131131
lastSST = ((PythonParserImpl) parser).getLastSST();
132132
return result;

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/SSTSerializationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,18 +1260,18 @@ public void checkSerialization(String code) throws Exception {
12601260
}
12611261

12621262
public void checkSerialization(Source source) throws Exception {
1263-
PythonCodeSerializer serializer = context.getCore().getSerializer();
1263+
PythonCodeSerializer serializer = context.getSerializer();
12641264

12651265
// at first parse the source and obtain the parse result
12661266
RootNode parserResult = (RootNode) parse(source, PythonParser.ParserMode.File);
12671267
ScopeInfo parserScope = getLastGlobalScope();
12681268
checkScopeSerialization(parserScope);
12691269
Assert.assertNotNull("Parser result is null", parserResult);
12701270
// serialize the source
1271-
byte[] serializeResult = serializer.serialize(context.getCore(), parserResult);
1271+
byte[] serializeResult = serializer.serialize(context, parserResult);
12721272
Assert.assertNotNull("Serialized data are null", serializeResult);
12731273
// and get the tree from serialized data
1274-
RootNode deserialize = serializer.deserialize(context.getCore(), serializeResult);
1274+
RootNode deserialize = serializer.deserialize(context, serializeResult);
12751275

12761276
Assert.assertNotNull("Deserialized result is null", parserResult);
12771277
// compare the tree from parser with the tree from serializer
@@ -1533,9 +1533,9 @@ public long[] getTimes(File file) throws Exception {
15331533
result[0] = end - startMemory;
15341534
result[3] = end - startFile;
15351535

1536-
PythonCodeSerializer serializer = context.getCore().getSerializer();
1536+
PythonCodeSerializer serializer = context.getSerializer();
15371537
startFile = System.nanoTime();
1538-
byte[] serializeResult = serializer.serialize(context.getCore(), parserResult);
1538+
byte[] serializeResult = serializer.serialize(context, parserResult);
15391539
end = System.nanoTime();
15401540
result[1] = end - startFile;
15411541
TruffleFile serFile = context.getEnv().getInternalTruffleFile(file.getAbsolutePath() + ".pyc");
@@ -1550,7 +1550,7 @@ public long[] getTimes(File file) throws Exception {
15501550
TruffleFile tFile = context.getEnv().getInternalTruffleFile(file.getAbsolutePath() + ".pyc");
15511551
byte[] desbytes = tFile.readAllBytes();
15521552
startMemory = System.nanoTime();
1553-
serializer.deserialize(context.getCore(), desbytes);
1553+
serializer.deserialize(context, desbytes);
15541554
end = System.nanoTime();
15551555
result[2] = end - startMemory;
15561556
result[5] = end - startFile;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void tearDown() {
8989

9090
@Test
9191
public void pythonModuleTest() {
92-
PythonModule module = context.getCore().factory().createPythonModule("testModule");
92+
PythonModule module = context.factory().createPythonModule("testModule");
9393
assertEquals("testModule", module.getAttribute(__NAME__).toString());
9494
assertEquals("None", module.getAttribute(__DOC__).toString());
9595
assertEquals("None", module.getAttribute(__PACKAGE__).toString());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
313313

314314
@Override
315315
protected PythonContext createContext(Env env) {
316-
Python3Core newCore = new Python3Core(new PythonParserImpl(env), env.isNativeAccessAllowed());
317-
final PythonContext context = new PythonContext(this, env, newCore);
316+
final PythonContext context = new PythonContext(this, env, new PythonParserImpl(env));
318317
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
319318

320319
Object[] engineOptionsUnroll = this.engineOptionsStorage;
@@ -375,7 +374,6 @@ public static String getEvalMimeType(int optimize) {
375374
@Override
376375
protected CallTarget parse(ParsingRequest request) {
377376
PythonContext context = PythonContext.get(null);
378-
Python3Core core = context.getCore();
379377
Source source = request.getSource();
380378
if (source.getMimeType() == null || MIME_TYPE.equals(source.getMimeType())) {
381379
if (!request.getArgumentNames().isEmpty()) {
@@ -391,7 +389,7 @@ protected CallTarget parse(ParsingRequest request) {
391389
gil.release(context, wasAcquired);
392390
}
393391
}
394-
if (core.isInitialized()) {
392+
if (context.isCoreInitialized()) {
395393
return PythonUtils.getOrCreateCallTarget(new TopLevelExceptionHandler(this, root, source));
396394
} else {
397395
return PythonUtils.getOrCreateCallTarget(root);
@@ -406,18 +404,18 @@ protected CallTarget parse(ParsingRequest request) {
406404
if (bytes.length == 0) {
407405
return createCachedCallTarget(l -> new BadOPCodeNode(l), BadOPCodeNode.class);
408406
}
409-
return PythonUtils.getOrCreateCallTarget(core.getSerializer().deserialize(core, bytes));
407+
return PythonUtils.getOrCreateCallTarget(context.getSerializer().deserialize(context, bytes));
410408
}
411409
for (int optimize = 0; optimize < MIME_TYPE_EVAL.length; optimize++) {
412410
if (MIME_TYPE_EVAL[optimize].equals(source.getMimeType())) {
413411
assert !source.isInteractive();
414-
return PythonUtils.getOrCreateCallTarget((RootNode) core.getParser().parse(ParserMode.Eval, optimize, core, source, null, null));
412+
return PythonUtils.getOrCreateCallTarget((RootNode) context.getParser().parse(ParserMode.Eval, optimize, context, source, null, null));
415413
}
416414
}
417415
for (int optimize = 0; optimize < MIME_TYPE_COMPILE.length; optimize++) {
418416
if (MIME_TYPE_COMPILE[optimize].equals(source.getMimeType())) {
419417
assert !source.isInteractive();
420-
return PythonUtils.getOrCreateCallTarget((RootNode) core.getParser().parse(ParserMode.File, optimize, core, source, null, null));
418+
return PythonUtils.getOrCreateCallTarget((RootNode) context.getParser().parse(ParserMode.File, optimize, context, source, null, null));
421419
}
422420
}
423421
throw CompilerDirectives.shouldNotReachHere("unknown mime type: " + source.getMimeType());
@@ -439,9 +437,8 @@ private RootNode doParse(PythonContext context, Source source, int optimize) {
439437
// by default we assume a module
440438
mode = ParserMode.File;
441439
}
442-
Python3Core pythonCore = context.getCore();
443440
try {
444-
return (RootNode) pythonCore.getParser().parse(mode, optimize, pythonCore, source, null, null);
441+
return (RootNode) context.getParser().parse(mode, optimize, context, source, null, null);
445442
} catch (PException e) {
446443
// handle PException during parsing (PIncompleteSourceException will propagate through)
447444
PythonUtils.getOrCreateCallTarget(new TopLevelExceptionHandler(this, e)).call();
@@ -494,7 +491,7 @@ public Object execute(VirtualFrame frame) {
494491

495492
private RootCallTarget parse(PythonContext context, VirtualFrame frame) {
496493
CompilerAsserts.neverPartOfCompilation();
497-
RootNode rootNode = (RootNode) context.getCore().getParser().parse(ParserMode.WithArguments, 0, context.getCore(), source, frame, argumentNames);
494+
RootNode rootNode = (RootNode) context.getParser().parse(ParserMode.WithArguments, 0, context, source, frame, argumentNames);
498495
return rootNode.getCallTarget();
499496
}
500497
};
@@ -556,8 +553,7 @@ private Object parseAndEval(PythonContext context, MaterializedFrame frame) {
556553

557554
@TruffleBoundary
558555
protected static ExpressionNode parseInline(Source code, PythonContext context, MaterializedFrame lexicalContextFrame) {
559-
Python3Core pythonCore = context.getCore();
560-
return (ExpressionNode) pythonCore.getParser().parse(ParserMode.InlineEvaluation, 0, pythonCore, code, lexicalContextFrame, null);
556+
return (ExpressionNode) context.getParser().parse(ParserMode.InlineEvaluation, 0, context, code, lexicalContextFrame, null);
561557
}
562558

563559
@Override
@@ -568,9 +564,9 @@ protected Object getLanguageView(PythonContext context, Object value) {
568564
try {
569565
if (interopLib.isBoolean(value)) {
570566
if (interopLib.asBoolean(value)) {
571-
return context.getCore().getTrue();
567+
return context.getTrue();
572568
} else {
573-
return context.getCore().getFalse();
569+
return context.getFalse();
574570
}
575571
} else if (interopLib.isString(value)) {
576572
return factory.createString(interopLib.asString(value));
@@ -718,7 +714,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
718714
}
719715

720716
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
721-
boolean coreIsInitialized = ctxt.getCore().isInitialized();
717+
boolean coreIsInitialized = ctxt.isCoreInitialized();
722718
boolean internal = !coreIsInitialized && !ctxt.getLanguage().getEngineOption(PythonOptions.ExposeInternalSources);
723719
if (internal) {
724720
srcBuilder.internal(true);

0 commit comments

Comments
 (0)