Issue with calling Java function with varargs from jython

jython
imagej
python
varargs
#1

Hi,

I have an issue with calling Java function with varargs from jython. I use the Jython version shipping with up to date Fiji (2.7.0).

My Java function expects Object… as parameter. When calling it from jython everything goes fine unless one of the parameter is a list. In that case the script complains that the Java function expects a single parameter but received more. I put a toy example below to illustrate. If anyone has ideas to circumvent that issue, I’d be really grateful.

In practice I am trying to write a small library in Java to wrap some image analysis functions. The goal is to make the function easy to use for people getting started with scripting. Basically I try to avoid java import in the script as well as conversions or unecessary syntax that could be confusing to beginners. Ideally I’d like user to pass for instance an image and a list of numbers (representing the size of a voxel or a filter size for instance) and the jython list would be interpreted on the Java side as List<Float> or Float[]. Any advice to go in that direction would be super welcome.

Thanks!

// java side
public class myClass{
    public void test( Object... args ){
        for(Object object : args)
            System.out.println( object.toString() );
    }
}
# jython side
import myClass
myClass().test("hello", 1 ) # this works fine
myClass().test( [ 1, 1 ] ) # this works fine too
myClass().test("hello", [ 1 , 1 ] ) # this crashes with: "TypeError: test(): expected 1 args; got 2"
myClass().test( [ "hello", [ 1 , 1 ] ] ) # seems to work too

PS: I’ve seen that putting all the parameters in a list would be workaround. By chance does anyone know if jython 2.7.1 would allow to avoid these additionnal bracket?

ScriptService.run(ScriptFile,..,..) only with script parameters?
#2

By the way I also posted the question on the jython-users mailing list.

Also replacing jython-shaded-2.7.0.jar by jython-shaded-2.7.1.jar in ImageJ jars directory doesn’t seem to make any difference. I downloaded the jar from http://maven.imagej.net/ .

#3

Hi @Benoit,

the jython script engine has some problems with vararg methods in Java. Here’s some related discussion:

https://github.com/scijava/scripting-jython/issues/8


(Relatedly, the Py4J project seems to have the same issue with varargs, discussed here on stackoverflow and in this py4j pull request.)

1 Like
#4

Hi @imagejan,

Thanks for the feedback. I posted the issue on the jython-users mailing list. no feedback so far but will report here if I receive any.

For the problem pointed out by @haesleinhuepf topic (also reported there on the forum ), I could discuss it with him (we are sitting in front of each other :wink: ) and I really like the implemented solution, which is to pass the parameters as a list a of key/value pair.

inputs = ['parameter1', value1,'paramter2', value2]
scriptService.run( myScriptFile, True, inputs)

This would also work if none of the value is a jython list

scriptService.run( myScriptFile, True, 'parameter1', value1,'paramter2', value2)