Skip to content

Commit 262c2c7

Browse files
committed
make sources loaded during initialization internal and add option to expose them
1 parent da8bd0a commit 262c2c7

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python;
2727

2828
import java.io.IOException;
29+
import java.net.URL;
2930
import java.text.MessageFormat;
3031
import java.util.ArrayList;
3132

@@ -70,6 +71,7 @@
7071
import com.oracle.truffle.api.nodes.Node;
7172
import com.oracle.truffle.api.nodes.RootNode;
7273
import com.oracle.truffle.api.source.Source;
74+
import com.oracle.truffle.api.source.Source.Builder;
7375

7476
@TruffleLanguage.Registration(id = PythonLanguage.ID, name = PythonLanguage.NAME, version = PythonLanguage.VERSION, mimeType = PythonLanguage.MIME_TYPE, interactive = true, internal = false)
7577
@ProvidedTags({StandardTags.CallTag.class, StandardTags.StatementTag.class, StandardTags.RootTag.class, StandardTags.TryBlockTag.class, DebuggerTags.AlwaysHalt.class})
@@ -395,4 +397,26 @@ public static PythonCore getCore() {
395397
public static TruffleLogger getLogger() {
396398
return TruffleLogger.getLogger(ID, PythonLanguage.class);
397399
}
400+
401+
public static Source newSource(PythonContext ctxt, String src, String name) {
402+
return newSource(ctxt, Source.newBuilder(src), name);
403+
}
404+
405+
public static Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
406+
return newSource(ctxt, ctxt.getEnv().newSourceBuilder(src), name);
407+
}
408+
409+
public static Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
410+
return newSource(ctxt, Source.newBuilder(url), name);
411+
}
412+
413+
private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> Source newSource(PythonContext ctxt, Builder<E1, E2, E3> srcBuilder,
414+
String name) throws E1 {
415+
Builder<E1, RuntimeException, RuntimeException> newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
416+
boolean internal = !ctxt.getCore().isInitialized() && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources) && !PythonOptions.getOption(ctxt, PythonOptions.LazyInit);
417+
if (internal) {
418+
srcBuilder.internal();
419+
}
420+
return newBuilder.build();
421+
}
398422
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,18 +747,19 @@ private static Source getSource(String basename, String prefix) {
747747
// pass
748748
}
749749
String suffix = FILE_SEPARATOR + basename + ".py";
750+
PythonContext ctxt = PythonLanguage.getContext();
750751
if (url != null) {
751752
// This path is hit when we load the core library e.g. from a Jar file
752753
try {
753-
return Source.newBuilder(new URL(url + suffix)).name(basename).mimeType(PythonLanguage.MIME_TYPE).build();
754+
return PythonLanguage.newSource(ctxt, new URL(url + suffix), basename);
754755
} catch (IOException e) {
755756
throw new RuntimeException("Could not read core library from " + url);
756757
}
757758
} else {
758-
Env env = PythonLanguage.getContext().getEnv();
759+
Env env = ctxt.getEnv();
759760
TruffleFile file = env.getTruffleFile(prefix + suffix);
760761
try {
761-
return env.newSourceBuilder(file).name(basename).mimeType(PythonLanguage.MIME_TYPE).build();
762+
return PythonLanguage.newSource(ctxt, file, basename);
762763
} catch (SecurityException | IOException t) {
763764
throw new RuntimeException("Could not read core library from " + file);
764765
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private Object evalExpression(String expression, PythonObject globals, PythonObj
500500
}
501501
}
502502
PythonParser parser = getCore().getParser();
503-
Source source = Source.newBuilder(expression).name(name).mimeType(PythonLanguage.MIME_TYPE).build();
503+
Source source = PythonLanguage.newSource(getContext(), expression, name);
504504
RootNode parsed = (RootNode) parser.parse(ParserMode.Eval, getCore(), source, callerFrame);
505505
return evalNode(parsed, globals, locals, closure);
506506
}
@@ -533,7 +533,7 @@ Object compile(PBytes source, String filename, String mode, Object kwFlags, Obje
533533
@Specialization
534534
@TruffleBoundary
535535
Object compile(String expression, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize) {
536-
Source source = Source.newBuilder(expression).name(filename).mimeType(PythonLanguage.MIME_TYPE).build();
536+
Source source = PythonLanguage.newSource(getContext(), expression, filename);
537537
ParserMode pm;
538538
if (mode.equals("exec")) {
539539
pm = ParserMode.File;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
4747

4848
import java.io.IOException;
49-
import java.net.URI;
5049
import java.util.List;
5150
import java.util.concurrent.locks.ReentrantLock;
5251
import java.util.regex.Pattern;
@@ -67,6 +66,7 @@
6766
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
6867
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6968
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
69+
import com.oracle.graal.python.runtime.PythonContext;
7070
import com.oracle.graal.python.runtime.PythonCore;
7171
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
7272
import com.oracle.graal.python.runtime.exception.PException;
@@ -340,29 +340,21 @@ public Object run(String path, String modulename) {
340340
@Specialization
341341
@TruffleBoundary
342342
public Object run(String path, PythonModule mod) {
343-
Env env = getContext().getEnv();
343+
PythonContext ctxt = getContext();
344+
Env env = ctxt.getEnv();
344345
try {
345346
String[] pathParts = path.split(Pattern.quote(PythonCore.FILE_SEPARATOR));
346347
String fileName = pathParts[pathParts.length - 1];
347348
TruffleFile file = env.getTruffleFile(path);
348-
Source src = null;
349-
try {
350-
if (file.exists()) {
351-
src = env.newSourceBuilder(file).mimeType(PythonLanguage.MIME_TYPE).build();
352-
}
353-
} catch (SecurityException e) {
354-
}
355-
if (src == null) {
356-
src = Source.newBuilder("").uri(URI.create(path)).mimeType(PythonLanguage.MIME_TYPE).name(fileName).build();
357-
}
349+
Source src = PythonLanguage.newSource(ctxt, file, fileName);
358350
RootNode parsedModule = (RootNode) getCore().getParser().parse(ParserMode.File, getCore(), src, null);
359351
if (parsedModule != null) {
360352
CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedModule);
361353
callTarget.call(PArguments.withGlobals(mod));
362354
}
363355
} catch (PException e) {
364356
throw e;
365-
} catch (IOException e) {
357+
} catch (IOException | SecurityException e) {
366358
throw raise(ImportError, e.getMessage());
367359
}
368360
return PNone.NONE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ private PythonOptions() {
4343
@Option(category = OptionCategory.DEBUG, help = "Defer loading the core lib until after language initialization, so it can be debugged.") //
4444
public static final OptionKey<Boolean> LazyInit = new OptionKey<>(false);
4545

46+
@Option(category = OptionCategory.DEBUG, help = "Expose internal sources as normal sources, so they will show up in the debugger and stacks") //
47+
public static final OptionKey<Boolean> ExposeInternalSources = new OptionKey<>(false);
48+
4649
@Option(category = OptionCategory.DEBUG, help = "Print the java stacktrace if enabled") //
4750
public static final OptionKey<Boolean> WithJavaStacktrace = new OptionKey<>(false);
4851

0 commit comments

Comments
 (0)