Skip to content

Commit 2a4d4dc

Browse files
committed
Unwrap Python objects when they wrap Java ones
This avoids, e.g., PyObjectDerived objects coming out of the script engine after script execution.
1 parent 1603a0e commit 2a4d4dc

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.script.ScriptEngine;
3535

3636
import org.python.core.PyNone;
37+
import org.python.core.PyObject;
3738
import org.scijava.plugin.Plugin;
3839
import org.scijava.script.AdaptedScriptLanguage;
3940
import org.scijava.script.ScriptLanguage;
@@ -59,7 +60,14 @@ public ScriptEngine getScriptEngine() {
5960

6061
@Override
6162
public Object decode(final Object object) {
62-
return object instanceof PyNone ? null : object;
63+
if (object instanceof PyNone) return null;
64+
if (object instanceof PyObject) {
65+
// Unwrap Python objects when they wrap Java ones.
66+
final PyObject pyObj = (PyObject) object;
67+
final Class<?> javaType = pyObj.getType().getProxyType();
68+
if (javaType != null) return pyObj.__tojava__(javaType);
69+
}
70+
return object;
6371
}
6472

6573
}

0 commit comments

Comments
 (0)