Skip to content

Commit e1caf30

Browse files
committed
JythonScriptLanguage: decode more PyObject types
It seems that for PyInteger at least, even though it is a PyObject, the approach of calling __tojava__(getType().getProxyType()) does not work. So to be safe, let's hardcode the types we _know_ we can support -- PyBoolean, PyInteger, PyFloat and PyString -- before PyObject generally. Hopefully, this will prevent warnings such as: [WARNING] Ignoring unsupported output: result [org.python.core.PyInteger] This change also updates the JythonTest#testBasic() method, since the result is now an Integer rather than a PyInteger.
1 parent 454c663 commit e1caf30

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/main/java/org/scijava/plugins/scripting/jython/JythonScriptLanguage.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
import javax.script.ScriptEngine;
3535

36+
import org.python.core.PyBoolean;
37+
import org.python.core.PyFloat;
38+
import org.python.core.PyInteger;
3639
import org.python.core.PyNone;
3740
import org.python.core.PyObject;
3841
import org.python.core.PyString;
@@ -71,15 +74,24 @@ public ScriptEngine getScriptEngine() {
7174
@Override
7275
public Object decode(final Object object) {
7376
if (object instanceof PyNone) return null;
77+
if (object instanceof PyBoolean) {
78+
return ((PyBoolean) object).getBooleanValue();
79+
}
80+
if (object instanceof PyInteger) {
81+
return ((PyInteger) object).getValue();
82+
}
83+
if (object instanceof PyFloat) {
84+
return ((PyFloat) object).getValue();
85+
}
86+
if (object instanceof PyString) {
87+
return ((PyString) object).getString();
88+
}
7489
if (object instanceof PyObject) {
7590
// Unwrap Python objects when they wrap Java ones.
7691
final PyObject pyObj = (PyObject) object;
7792
final Class<?> javaType = pyObj.getType().getProxyType();
7893
if (javaType != null) return pyObj.__tojava__(javaType);
7994
}
80-
if (object instanceof PyString) {
81-
return ((PyString) object).getString();
82-
}
8395
return object;
8496
}
8597

src/test/java/org/scijava/plugins/scripting/jython/JythonTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertNull;
36+
import static org.junit.Assert.assertSame;
3637

3738
import java.io.IOException;
3839
import java.util.concurrent.ExecutionException;
@@ -64,8 +65,8 @@ public void testBasic() throws InterruptedException, ExecutionException,
6465
final String script = "1 + 2";
6566
final ScriptModule m = scriptService.run("add.py", script, true).get();
6667
final Object result = m.getReturnValue();
67-
// NB: Result is of type org.python.core.PyInteger.
68-
assertEquals("3", result.toString());
68+
assertSame(Integer.class, result.getClass());
69+
assertEquals(3, result);
6970
}
7071

7172
@Test

0 commit comments

Comments
 (0)