Skip to content

Commit ee8bcab

Browse files
committed
support -W option
1 parent 0da4104 commit ee8bcab

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public static void main(String[] args) {
8888
private boolean wantsExperimental = false;
8989
private Map<String, String> enginePolyglotOptions;
9090
private boolean dontWriteBytecode = false;
91+
private String warnOptions = null;
9192

9293
@Override
9394
protected List<String> preprocessArguments(List<String> givenArgs, Map<String, String> polyglotOptions) {
@@ -153,6 +154,20 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
153154
case "-S":
154155
noSite = true;
155156
break;
157+
case "-W":
158+
i += 1;
159+
if (warnOptions == null) {
160+
warnOptions = "";
161+
} else {
162+
warnOptions += ",";
163+
}
164+
if (i < arguments.size()) {
165+
warnOptions += arguments.get(i);
166+
} else {
167+
print("Argument expected for the -W option");
168+
printShortHelp();
169+
}
170+
break;
156171
case "-X":
157172
i++;
158173
if (i < arguments.size()) {
@@ -230,6 +245,14 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
230245
inputFile = arg;
231246
programArgs.add(inputFile);
232247
break;
248+
} else if (arg.startsWith("-W")) {
249+
// alternate allowed form
250+
if (warnOptions == null) {
251+
warnOptions = "";
252+
} else {
253+
warnOptions += ",";
254+
}
255+
warnOptions += arg.substring(2);
233256
} else if (!arg.startsWith("--") && arg.length() > 2) {
234257
// short arguments can be given together
235258
String[] split = arg.substring(1).split("");
@@ -372,12 +395,17 @@ protected void launch(Builder contextBuilder) {
372395
verboseFlag = verboseFlag || System.getenv("PYTHONVERBOSE") != null;
373396
unbufferedIO = unbufferedIO || System.getenv("PYTHONUNBUFFERED") != null;
374397
dontWriteBytecode = dontWriteBytecode || System.getenv("PYTHONDONTWRITEBYTECODE") != null;
398+
if (warnOptions == null) {
399+
warnOptions = System.getenv("PYTHONWARNINGS");
400+
}
375401
String cachePrefix = System.getenv("PYTHONPYCACHEPREFIX");
376402
if (cachePrefix != null) {
377403
contextBuilder.option("python.PyCachePrefix", cachePrefix);
378404
}
379405
}
380-
406+
if (warnOptions == null || warnOptions.isEmpty()) {
407+
warnOptions = "default";
408+
}
381409
String executable = getContextOptionIfSetViaCommandLine("python.Executable");
382410
if (executable != null) {
383411
contextBuilder.option("python.ExecutableList", executable);
@@ -394,6 +422,7 @@ protected void launch(Builder contextBuilder) {
394422
contextBuilder.option("python.InspectFlag", Boolean.toString(inspectFlag));
395423
contextBuilder.option("python.VerboseFlag", Boolean.toString(verboseFlag));
396424
contextBuilder.option("python.IsolateFlag", Boolean.toString(isolateFlag));
425+
contextBuilder.option("python.WarnOptions", warnOptions);
397426
contextBuilder.option("python.DontWriteBytecodeFlag", Boolean.toString(dontWriteBytecode));
398427
if (verboseFlag) {
399428
contextBuilder.option("log.python.level", "FINE");
@@ -581,9 +610,8 @@ protected void printHelp(OptionCategory maxCategory) {
581610
"-V : print the Python version number and exit (also --version)\n" +
582611
" when given twice, print more information about the build\n" +
583612
"-X opt : CPython implementation-specific options. Ignored on GraalPython\n" +
584-
// "-W arg : warning control; arg is
585-
// action:message:category:module:lineno\n" +
586-
// " also PYTHONWARNINGS=arg\n" +
613+
"-W arg : warning control; arg is action:message:category:module:lineno\n" +
614+
" also PYTHONWARNINGS=arg\n" +
587615
// "-x : skip first line of source, allowing use of non-Unix forms of
588616
// #!cmd\n" +
589617
// "-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public void postInitialize(PythonCore core) {
199199
sys.setAttribute("dont_write_bytecode", context.getOption(PythonOptions.DontWriteBytecodeFlag));
200200
String pycachePrefix = context.getOption(PythonOptions.PyCachePrefix);
201201
sys.setAttribute("pycache_prefix", pycachePrefix.isEmpty() ? PNone.NONE : pycachePrefix);
202+
sys.setAttribute("warnoptions", core.factory().createTuple(context.getOption(PythonOptions.WarnOptions).split(",")));
202203
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
203204
false, // bytes_warning
204205
!context.getOption(PythonOptions.PythonOptimizeFlag), // debug

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
@@ -114,6 +114,9 @@ private PythonOptions() {
114114
"Equivalent to setting the PYTHONPYCACHEPREFIX environment variable for the standard launcher.", stability = OptionStability.STABLE) //
115115
public static final OptionKey<String> PyCachePrefix = new OptionKey<>("");
116116

117+
@Option(category = OptionCategory.USER, help = "Equivalent to setting the PYTHONWARNINGS environment variable for the standard launcher.", stability = OptionStability.STABLE) //
118+
public static final OptionKey<String> WarnOptions = new OptionKey<>("");
119+
117120
@Option(category = OptionCategory.INTERNAL, help = "Set the location of C API home. Overrides any environment variables or Java options.", stability = OptionStability.STABLE) //
118121
public static final OptionKey<String> CAPI = new OptionKey<>("");
119122

0 commit comments

Comments
 (0)