[Jython-checkins] jython: JSR223 sets sys.argv if FILENAME and/or ARGV set in script context. Fixes
jim.baker
jython-checkins at python.org
Thu Jan 7 00:36:28 EST 2016
https://hg.python.org/jython/rev/e1ae43e28fbf
changeset: 7856:e1ae43e28fbf
user: Jim Baker <jim.baker at rackspace.com>
date: Wed Jan 06 22:36:07 2016 -0700
summary:
JSR223 sets sys.argv if FILENAME and/or ARGV set in script context. Fixes #1738, #1739
files:
src/org/python/jsr223/PyScriptEngine.java | 17 +++
tests/java/org/python/jsr223/ScriptEngineTest.java | 43 ++++++++++
2 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/src/org/python/jsr223/PyScriptEngine.java b/src/org/python/jsr223/PyScriptEngine.java
--- a/src/org/python/jsr223/PyScriptEngine.java
+++ b/src/org/python/jsr223/PyScriptEngine.java
@@ -37,6 +37,23 @@
interp.setOut(context.getWriter());
interp.setErr(context.getErrorWriter());
interp.setLocals(new PyScriptEngineScope(this, context));
+
+ // set sys.argv if FILENAME, ARGV attributes are defined
+ String filename = (String) context.getAttribute(ScriptEngine.FILENAME);
+ String[] argv = (String[]) context.getAttribute(ScriptEngine.ARGV);
+ if (argv != null || filename != null) {
+ PyList pyargv = new PyList();
+ if (filename != null) {
+ pyargv.append(Py.java2py(filename));
+ }
+ if (argv != null) {
+ for (int i = 0; i < argv.length; i++) {
+ pyargv.append(Py.java2py(argv[i]));
+ }
+ }
+ interp.getSystemState().argv = pyargv;
+ }
+
return interp.eval(code).__tojava__(Object.class);
} catch (PyException pye) {
throw scriptException(pye);
diff --git a/tests/java/org/python/jsr223/ScriptEngineTest.java b/tests/java/org/python/jsr223/ScriptEngineTest.java
--- a/tests/java/org/python/jsr223/ScriptEngineTest.java
+++ b/tests/java/org/python/jsr223/ScriptEngineTest.java
@@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.math.BigInteger;
+import java.util.Arrays;
import javax.script.Bindings;
import javax.script.Compilable;
@@ -30,6 +31,42 @@
assertNull(pythonEngine.eval("x = 5"));
assertEquals(5, pythonEngine.eval("x"));
assertEquals("sample.py", pythonEngine.eval("__file__"));
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("sample.py"), pythonEngine.eval("sys.argv"));
+ }
+
+ public void testEvalStringArgv() throws ScriptException {
+ ScriptEngineManager manager = new ScriptEngineManager();
+ ScriptEngine pythonEngine = manager.getEngineByName("python");
+ ScriptContext context = pythonEngine.getContext();
+ context.setAttribute(ScriptEngine.FILENAME, "sample.py", ScriptContext.ENGINE_SCOPE);
+ context.setAttribute(ScriptEngine.ARGV, new String[] {"foo", "bar"}, ScriptContext.ENGINE_SCOPE);
+ assertNull(pythonEngine.eval("x = 5"));
+ assertEquals(5, pythonEngine.eval("x"));
+ assertEquals("sample.py", pythonEngine.eval("__file__"));
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("sample.py", "foo", "bar"), pythonEngine.eval("sys.argv"));
+ }
+
+ public void testEvalStringNoFilenameWithArgv() throws ScriptException {
+ ScriptEngineManager manager = new ScriptEngineManager();
+ ScriptEngine pythonEngine = manager.getEngineByName("python");
+ ScriptContext context = pythonEngine.getContext();
+ context.setAttribute(ScriptEngine.ARGV, new String[] {"foo", "bar"}, ScriptContext.ENGINE_SCOPE);
+ assertNull(pythonEngine.eval("x = 5"));
+ assertEquals(5, pythonEngine.eval("x"));
+ boolean gotExpectedException = false;
+ try {
+ pythonEngine.eval("__file__");
+ } catch (ScriptException e) {
+ assertTrue(e.getMessage().startsWith("NameError: "));
+ gotExpectedException = true;
+ }
+ if (!gotExpectedException) {
+ fail("Excepted __file__ to be undefined");
+ }
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("foo", "bar"), pythonEngine.eval("sys.argv"));
}
public void testSyntaxError() {
@@ -83,6 +120,8 @@
CompiledScript five = ((Compilable)pythonEngine).compile("5");
assertEquals(5, five.eval());
assertEquals("sample.py", pythonEngine.eval("__file__"));
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("sample.py"), pythonEngine.eval("sys.argv"));
}
public void testEvalReader() throws ScriptException {
@@ -93,6 +132,8 @@
assertNull(pythonEngine.eval(new StringReader("x = 5")));
assertEquals(5, pythonEngine.eval(new StringReader("x")));
assertEquals("sample.py", pythonEngine.eval("__file__"));
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("sample.py"), pythonEngine.eval("sys.argv"));
}
public void testCompileEvalReader() throws ScriptException {
@@ -103,6 +144,8 @@
CompiledScript five = ((Compilable)pythonEngine).compile(new StringReader("5"));
assertEquals(5, five.eval());
assertEquals("sample.py", pythonEngine.eval("__file__"));
+ pythonEngine.eval("import sys");
+ assertEquals(Arrays.asList("sample.py"), pythonEngine.eval("sys.argv"));
}
public void testBindings() throws ScriptException {
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list