Skip to content

Commit 9fc09f9

Browse files
committed
[GR-11437] Switch to TruffleLogger for warnings and info messages.
PullRequest: graalpython/173
2 parents 9fa38f4 + 7100eb6 commit 9fc09f9

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ protected void launch(Builder contextBuilder) {
168168
}
169169
if (verboseFlag) {
170170
contextBuilder.option("python.VerboseFlag", "true");
171+
contextBuilder.option("log.python.level", "FINE");
172+
}
173+
174+
String pythonpath = System.getenv("PYTHONPATH");
175+
if (pythonpath != null) {
176+
contextBuilder.option("python.PythonPath", pythonpath);
171177
}
172178

173179
ConsoleHandler consoleHandler = createConsoleHandler(System.in, System.out);

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
5050
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5151
import com.oracle.graal.python.nodes.object.GetClassNode;
52-
import com.oracle.graal.python.nodes.statement.ImportNode;
5352
import com.oracle.graal.python.parser.PythonParserImpl;
5453
import com.oracle.graal.python.runtime.PythonContext;
5554
import com.oracle.graal.python.runtime.PythonCore;
@@ -118,7 +117,7 @@ protected void finalizeContext(PythonContext context) {
118117
protected boolean patchContext(PythonContext context, Env newEnv) {
119118
nativeBuildTime = false; // now we're running
120119
ensureHomeInOptions(newEnv);
121-
PythonCore.writeInfo(newEnv, "Using preinitialized context.");
120+
PythonCore.writeInfo("Using preinitialized context.");
122121
context.patch(newEnv);
123122
return true;
124123
}
@@ -136,7 +135,7 @@ private void ensureHomeInOptions(Env env) {
136135
String coreHome = env.getOptions().get(PythonOptions.CoreHome);
137136
String stdLibHome = env.getOptions().get(PythonOptions.StdLibHome);
138137

139-
PythonCore.writeInfo(env, (MessageFormat.format("Initial locations:" +
138+
PythonCore.writeInfo((MessageFormat.format("Initial locations:" +
140139
"\n\tLanguage home: {0}" +
141140
"\n\tSysPrefix: {1}" +
142141
"\n\tCoreHome: {2}" +
@@ -193,7 +192,7 @@ private void ensureHomeInOptions(Env env) {
193192
env.getOptions().set(PythonOptions.StdLibHome, stdLibHome);
194193
}
195194

196-
PythonCore.writeInfo(env, (MessageFormat.format("Updated locations:" +
195+
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
197196
"\n\tLanguage home: {0}" +
198197
"\n\tSysPrefix: {1}" +
199198
"\n\tCoreHome: {2}" +
@@ -216,29 +215,31 @@ protected CallTarget parse(ParsingRequest request) throws Exception {
216215
PythonContext context = this.getContextReference().get();
217216
PythonCore pythonCore = context.getCore();
218217
Source source = request.getSource();
218+
CompilerDirectives.transferToInterpreter();
219219
if (pythonCore.isInitialized()) {
220220
context.initializeMainModule(source.getPath());
221221

222222
// if we are running the interpreter, module 'site' is automatically imported
223223
if (source.isInteractive()) {
224-
CompilerAsserts.neverPartOfCompilation();
225-
// no frame required
226-
new ImportNode("site").execute(null);
224+
Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, doParse(pythonCore, Source.newBuilder(ID, "import site", "<site import>").build()))).call();
227225
}
228226
}
229-
RootNode root;
227+
RootNode root = doParse(pythonCore, source);
228+
if (pythonCore.isInitialized()) {
229+
return Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, root));
230+
} else {
231+
return Truffle.getRuntime().createCallTarget(root);
232+
}
233+
}
234+
235+
private RootNode doParse(PythonCore pythonCore, Source source) {
230236
try {
231-
root = (RootNode) pythonCore.getParser().parse(source.isInteractive() ? ParserMode.InteractiveStatement : ParserMode.File, pythonCore, source, null);
237+
return (RootNode) pythonCore.getParser().parse(source.isInteractive() ? ParserMode.InteractiveStatement : ParserMode.File, pythonCore, source, null);
232238
} catch (PException e) {
233239
// handle PException during parsing (PIncompleteSourceException will propagate through)
234240
Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, e)).call();
235241
throw e;
236242
}
237-
if (pythonCore.isInitialized()) {
238-
return Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, root));
239-
} else {
240-
return Truffle.getRuntime().createCallTarget(root);
241-
}
242243
}
243244

244245
@Override
@@ -370,7 +371,7 @@ protected String toString(PythonContext context, Object value) {
370371
}
371372

372373
public static TruffleLogger getLogger() {
373-
return TruffleLogger.getLogger(ID, PythonLanguage.class);
374+
return TruffleLogger.getLogger(ID);
374375
}
375376

376377
public static Source newSource(PythonContext ctxt, String src, String name) {

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
131131
import com.oracle.graal.python.runtime.PythonContext;
132132
import com.oracle.graal.python.runtime.PythonCore;
133+
import com.oracle.graal.python.runtime.PythonOptions;
133134
import com.oracle.graal.python.runtime.PythonParser;
134135
import com.oracle.graal.python.runtime.exception.PException;
135136
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -162,12 +163,12 @@ public final class Python3Core implements PythonCore {
162163
"method",
163164
"code",
164165
"_warnings",
166+
"posix",
167+
"_io",
165168
"_frozen_importlib_external",
166169
"_frozen_importlib",
167-
"posix",
168170
"classes",
169171
"_weakref",
170-
"_io",
171172
"set",
172173
"itertools",
173174
"base_exception",
@@ -364,19 +365,23 @@ public PythonModule initializeSysModule() {
364365

365366
private void initializeSysPath(PythonModule sys, String[] args) {
366367
Env env = getContext().getEnv();
367-
Object[] path = new Object[]{
368-
getScriptPath(env, args),
369-
PythonCore.getStdlibHome(env),
370-
PythonCore.getCoreHome(env) + PythonCore.FILE_SEPARATOR + "modules"};
368+
String option = PythonOptions.getOption(getContext(), PythonOptions.PythonPath);
369+
Object[] path;
370+
int pathIdx = 0;
371+
if (option.length() > 0) {
372+
String[] split = option.split(PythonCore.PATH_SEPARATOR);
373+
path = new Object[split.length + 3];
374+
System.arraycopy(split, 0, path, 0, split.length);
375+
pathIdx = split.length;
376+
} else {
377+
path = new Object[3];
378+
}
379+
path[pathIdx] = getScriptPath(env, args);
380+
path[pathIdx + 1] = PythonCore.getStdlibHome(env);
381+
path[pathIdx + 2] = PythonCore.getCoreHome(env) + PythonCore.FILE_SEPARATOR + "modules";
371382
PList sysPaths = factory().createList(path);
372383
sys.setAttribute("path", sysPaths);
373384
// sysPaths.append(getPythonLibraryExtrasPath());
374-
String pythonPath = System.getenv("PYTHONPATH");
375-
if (pythonPath != null) {
376-
for (String s : pythonPath.split(PythonCore.PATH_SEPARATOR)) {
377-
sysPaths.append(s);
378-
}
379-
}
380385
}
381386

382387
private static String getScriptPath(Env env, String[] args) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
7272
import com.oracle.graal.python.runtime.PythonContext;
7373
import com.oracle.graal.python.runtime.PythonCore;
74+
import com.oracle.graal.python.runtime.PythonOptions;
7475
import com.oracle.graal.python.runtime.exception.PException;
7576
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7677
import com.oracle.truffle.api.CompilerDirectives;
@@ -159,7 +160,7 @@ public void initialize(PythonCore core) {
159160
false, // no_user_site
160161
false, // optimize
161162
false, // quiet
162-
false, // verbose
163+
PythonOptions.getOption(core.getContext(), PythonOptions.VerboseFlag).booleanValue(), // verbose
163164
}));
164165
// the default values taken from JPython
165166
builtinConstants.put("float_info", core.factory().createTuple(new Object[]{

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

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

2828
import java.io.File;
29-
import java.io.IOException;
3029

3130
import com.oracle.graal.python.PythonLanguage;
3231
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -55,7 +54,6 @@ public interface PythonCore {
5554
static final String NO_PREFIX_WARNING = "could not determine Graal.Python's sys prefix path - you may need to pass --python.SysPrefix.";
5655
static final String NO_CORE_WARNING = "could not determine Graal.Python's core path - you may need to pass --python.CoreHome.";
5756
static final String NO_STDLIB = "could not determine Graal.Python's standard library path. You need to pass --python.StdLibHome if you want to use the standard library.";
58-
static final boolean LIBPOLYGLOT = Boolean.getBoolean("graalvm.libpolyglot");
5957

6058
/**
6159
* Load the core library and prepare all builtin classes and modules.
@@ -117,25 +115,12 @@ public interface PythonCore {
117115

118116
public PInt getFalse();
119117

120-
static void writeWarning(TruffleLanguage.Env env, String warning) {
121-
if (!LIBPOLYGLOT || env.getOptions().get(PythonOptions.VerboseFlag)) {
122-
write(env, "WARNING: " + warning);
123-
}
118+
static void writeWarning(String warning) {
119+
PythonLanguage.getLogger().warning(warning);
124120
}
125121

126-
static void writeInfo(TruffleLanguage.Env env, String warning) {
127-
if (env.getOptions().get(PythonOptions.VerboseFlag)) {
128-
write(env, warning);
129-
}
130-
}
131-
132-
static void write(TruffleLanguage.Env env, String warning) {
133-
try {
134-
env.err().write("[python] ".getBytes());
135-
env.err().write(warning.getBytes());
136-
env.err().write('\n');
137-
} catch (IOException e) {
138-
}
122+
static void writeInfo(String message) {
123+
PythonLanguage.getLogger().fine(message);
139124
}
140125

141126
@TruffleBoundary
@@ -152,7 +137,7 @@ public static String getCoreHomeOrFail() {
152137
public static String getSysPrefix(TruffleLanguage.Env env) {
153138
String sysPrefix = env.getOptions().get(PythonOptions.SysPrefix);
154139
if (sysPrefix.isEmpty()) {
155-
writeWarning(env, NO_PREFIX_WARNING);
140+
writeWarning(NO_PREFIX_WARNING);
156141
env.getOptions().set(PythonOptions.SysPrefix, PREFIX);
157142
return LIB_GRAALPYTHON;
158143
}
@@ -163,7 +148,7 @@ public static String getSysPrefix(TruffleLanguage.Env env) {
163148
public static String getCoreHome(TruffleLanguage.Env env) {
164149
String coreHome = env.getOptions().get(PythonOptions.CoreHome);
165150
if (coreHome.isEmpty()) {
166-
writeWarning(env, NO_CORE_WARNING);
151+
writeWarning(NO_CORE_WARNING);
167152
env.getOptions().set(PythonOptions.CoreHome, LIB_GRAALPYTHON);
168153
return LIB_GRAALPYTHON;
169154
}
@@ -174,7 +159,7 @@ public static String getCoreHome(TruffleLanguage.Env env) {
174159
public static String getStdlibHome(TruffleLanguage.Env env) {
175160
String stdLibHome = env.getOptions().get(PythonOptions.StdLibHome);
176161
if (stdLibHome.isEmpty()) {
177-
writeWarning(env, NO_STDLIB);
162+
writeWarning(NO_STDLIB);
178163
env.getOptions().set(PythonOptions.StdLibHome, LIB_PYTHON_3);
179164
return LIB_PYTHON_3;
180165
}

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
@@ -85,6 +85,9 @@ private PythonOptions() {
8585
@Option(category = OptionCategory.USER, help = "") //
8686
public static final OptionKey<Boolean> InspectFlag = new OptionKey<>(false);
8787

88+
@Option(category = OptionCategory.USER, help = "") //
89+
public static final OptionKey<String> PythonPath = new OptionKey<>("");
90+
8891
@Option(category = OptionCategory.USER, help = "Remove assert statements and any code conditional on the value of __debug__.") //
8992
public static final OptionKey<Boolean> PythonOptimizeFlag = new OptionKey<>(false);
9093

0 commit comments

Comments
 (0)