Thread: [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) |
From: Barthelemy D. <ba...@cs...> - 2011-03-16 19:27:27
|
Hi Alec, Thanks for reporting this bug and for the test case, it was very helpful! I just pushed a fix to the git repository and I added your test case in the unit test suite. Don't hesitate to post again if you find another bug! Barthélémy On 03/16/2011 02:20 PM, Alec Chapman wrote: > 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) > > > > ------------------------------------------------------------------------------ > Colocation vs. Managed Hosting > A question and answer guide to determining the best fit > for your organization - today and in the future. > http://p.sf.net/sfu/internap-sfd2d > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |
From: Alec C. <ar...@fa...> - 2011-03-16 21:53:33
|
Hi Barthélémy, Thanks for the fix! It's working great now. Alec Quoting Barthelemy Dagenais <ba...@cs...>: > Hi Alec, > > Thanks for reporting this bug and for the test case, it was very helpful! > > I just pushed a fix to the git repository and I added your test case in > the unit test suite. > > Don't hesitate to post again if you find another bug! > > Barthélémy > > On 03/16/2011 02:20 PM, Alec Chapman wrote: > > 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) > > > > > > > > > ------------------------------------------------------------------------------ > > Colocation vs. Managed Hosting > > A question and answer guide to determining the best fit > > for your organization - today and in the future. > > http://p.sf.net/sfu/internap-sfd2d > > _______________________________________________ > > Py4j-users mailing list > > Py4...@li... > > https://lists.sourceforge.net/lists/listinfo/py4j-users > > > ------------------------------------------------------------------------------ > Colocation vs. Managed Hosting > A question and answer guide to determining the best fit > for your organization - today and in the future. > http://p.sf.net/sfu/internap-sfd2d > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users > |