Justin,
> PythonInterpreter interp=new PythonInterpreter();
> interp.exec("import jarray");
> interp.exec("x=jarray.array([1,2,3],'i')");
> PyArray x=(PyArray)interp.get("x");
> Class c=PyArray.char2class('i');
> Object o=x.__tojava__(c);
> System.out.println(o.toString());
>
The problem is you are asking PyArray to return an object of the type
'int' but PyArray's __tojava__ doesn't know anything about this type.
Instead you have two options:
(1) Ask for a generic Object.
(2) Ask for the specific array type.
If you choose to use (2), the following will create the correctly typed
array:
Class c = Array.newInstance(PyArray.char2class('i'), 0).getClass();
<note>
I am not sure of another way to get an 'int[]' Class instance. Does
anyone else? I'm surely missing something though this works.
</note>
> but it generates the result 'Error' which doesn't look
> promising (why is no exception thrown?)
You are really getting back the Py.NoConversion instance. You would see
an exception if you tried to cast to some other type but since you don't
you do not see an exception.
In order to iterate the array dynamically I think you'll need to use the
java.lang.reflect.Array class again to get the values, such as:
for(int i=0; i<Array.getLength(o); i++) {
System.out.println(Array.getInt(o, i));
}
if you choose not statically cast the Object returned from
PyArray.__tojava__(). If you choose to do it dynamically you might need
some sort of switch in order to figure out what Array.getX() method to
use or just use Array.get() to get an Object back.
Hope this helps,
brian
|