[Py4j-users] Interface troubles
Status: Beta
Brought to you by:
barthe
|
From: Alec C. <ar...@fa...> - 2011-03-16 18:36:45
|
Hi,
I've just started trying out py4j and am finding it really helpful so far. I
was able to implement a simple Java interface in Python, but I'm running into
trouble with more complicated interfaces.
Specifically, I'm having trouble when one interface has a function that returns
another interface, with both interfaces implemented in Python. The problem
seems to be that the python object is only treated as a Java interface when it
is passed as an argument, not when it is the return value.
I've included a simple example below. Is what I'm trying to do possible?
Thanks for any help,
Alec
Here's the python code:
--------------------------------------------------
from py4j.java_gateway import JavaGateway
class A(object):
class Java:
implements = ['py4j.examples.InterfaceA']
class B(object):
def foo(self):
return A() #this doesn't work
return gateway.jvm.py4j.examples.ClassA() #this does
class Java:
implements = ['py4j.examples.InterfaceB']
gateway = JavaGateway(start_callback_server=True)
gateway.entry_point.test(B())
And here's the Java code:
--------------------------------------------
public interface InterfaceA {
}
public interface InterfaceB {
public InterfaceA foo();
}
public class ClassA implements InterfaceA{
}
public class InterfaceExample {
public void test(InterfaceB b) {
InterfaceA a = b.foo();
}
public static void main(String[] args) {
GatewayServer server = new GatewayServer(new InterfaceExample());
server.start();
}
}
-------------------------------------
The error occurs in the line 'return A()':
py4j.protocol.Py4JJavaError: An error occurred while calling t.test.
: py4j.Py4JException: An exception was raised by the Python Proxy.
at py4j.Protocol.getReturnValue(Protocol.java:393)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:100)
at $Proxy0.foo(Unknown Source)
at py4j.examples.InterfaceExample.test(InterfaceExample.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:119)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:392)
at py4j.Gateway.invoke(Gateway.java:255)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:124)
at py4j.commands.CallCommand.execute(CallCommand.java:81)
at py4j.GatewayConnection.run(GatewayConnection.java:175)
at java.lang.Thread.run(Thread.java:662)
|