From: Danny Y. <dy...@hk...> - 2001-11-19 23:29:10
|
I've been experimenting with jythonc, and one of my experiments was to play around with Java interfaces. In the course of my experiments, I've been running my head into a strange AttributeError. Since I'm not too familiar with Jython, I'll report the steps I've done in some detail; forgive me if much of this is extraneous! The error message is at the very bottom, and this message traces what I've done to get the message. I have the following Java interface: ### [dyoo@tesuque bar]$ cat EvalInterface.java public interface EvalInterface { public int eval(int x); } ### I've written a few Jython files that extend this interface: ### [dyoo@tesuque bar]$ cat Doubler.py import EvalInterface class Doubler(EvalInterface): def __init__(self): pass def eval(self, x): return x + x [dyoo@tesuque bar]$ cat Squarer.py import EvalInterface class Squarer(EvalInterface): def __init__(self): pass def eval(self, x): return x * x ### and I've compiled my Jython files into a single .jar file: ### processing Doubler processing Squarer Required packages: Creating adapters: Creating .java files: Squarer module Squarer extends java.lang.Object implements EvalInterface Doubler module Doubler extends java.lang.Object implements EvalInterface Compiling .java to .class... Compiling with args: ['/usr/local/bin/jikes', '-classpath', '/home/dyoo/local/jython-2.1a3/jython.jar:.:/opt/jdk1.3.0_02/jre/lib/rt.jar:/home/dyoo/local/jython-2.1a3/jython.jar:./test.jar:./jpywork::/home/dyoo/local/jython-2.1a3/Tools/jythonc:/home/dyoo/bar/.:/home/dyoo/local/jython-2.1a3/Lib', './jpywork/Squarer.java', './jpywork/Doubler.java'] 0 Building archive: test.jar Tracking java dependencies: ### Once this was done, I adjusted my CLASSPATH to include this jar file: ### export CLASSPATH=.:/opt/jdk1.3.0_02/jre/lib/rt.jar:\ /home/dyoo/local/jython-2.1a3/jython.jar:./test.jar ### and wrote a small java test.java to see if everything worked. ### [dyoo@tesuque bar]$ cat foo.java public class foo { static public void main(String[] args) { EvalInterface f1 = new Square(); System.out.println(f1.eval(42)); EvalInterface f2 = new Double(); System.out.println(f2.eval(42)); } } ### And this worked! ### [dyoo@tesuque bar]$ java Test 1764 84 ### However, I ran into problems when I tried playing with my classes in Jython. To force Jython to use my jythonc-compiled classes, I made a new directory called "scratch". Here's the log: ### [dyoo@tesuque bar]$ mkdir scratch [dyoo@tesuque bar]$ cp test.jar scratch/ [dyoo@tesuque bar]$ cd scratch [dyoo@tesuque scratch]$ cp ../EvalInterface.class . [dyoo@tesuque scratch]$ echo $CLASSPATH .:/opt/jdk1.3.0_02/jre/lib/rt.jar:/home/dyoo/local/jython-2.1a3/jython.jar:./test.jar [dyoo@tesuque scratch]$ jython Jython 2.1a3 on java1.3.0_02 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> import Doubler >>> Doubler <jclass Doubler at 5868800> >>> d = Doubler() >>> d.eval(42) Traceback (innermost last): File "<console>", line 1, in ? AttributeError: abstract method "eval" not implemented ### I'm just scratching my head on this one. Are Jythonc-compiled classes reusable from Jython itself? Any help on this would be greatly appreciated. Thank you! |
From: Danny Y. <dy...@hk...> - 2001-11-19 23:35:58
|
Sorry, I had a few cut-n-paste errors. Here are some corrections: On Mon, 19 Nov 2001, Danny Yoo wrote: > > and I've compiled my Jython files into a single .jar file: > > ### The command I used was: ### [dyoo@tesuque bar]$ jythonc --jar test.jar Doubler.py Squarer.py ### > and wrote a small java test.java to see if everything worked. > > ### > [dyoo@tesuque bar]$ cat foo.java > public class foo { > static public void main(String[] args) { > EvalInterface f1 = new Square(); > System.out.println(f1.eval(42)); > EvalInterface f2 = new Double(); > System.out.println(f2.eval(42)); > } > } Wrong test file --- I meant: ### public class Test { static public void main(String[] args) { EvalInterface f1 = new Squarer(); System.out.println(f1.eval(42)); EvalInterface f2 = new Doubler(); System.out.println(f2.eval(42)); } } ### instead. (I had to rename my class from "Double" to "Doubler", since the name was conflicting with the java.lang.Double wrapper class.) Sorry about the typos! |
From: dman <ds...@ri...> - 2001-11-25 01:02:45
|
Hi Danny! On Mon, Nov 19, 2001 at 03:27:51PM -0800, Danny Yoo wrote: | I've been experimenting with jythonc, and one of my experiments was to | play around with Java interfaces. In the course of my experiments, I've | been running my head into a strange AttributeError. [implemented java interface with python class, compiled with jythonc ] | I'm just scratching my head on this one. Are Jythonc-compiled classes | reusable from Jython itself? Any help on this would be greatly | appreciated. Thank you! I just got jythonc to work again on my machine, so I tried out what you were doing (though I implmemented java.lang.Runnable instead). I got similar results, even when I added an "@sig" line to my python class. It does seem that the classes generated by jythonc are not directly usable in the interpreter. Instead, I guess just use the python source if the interpreter is being used directly, and the jythonc classes if you want to (appear to) do away with the interpreter. HTH, -D |
From: <bc...@wo...> - 2001-11-26 09:46:28
|
[Danny Yoo] >I've been experimenting with jythonc, and one of my experiments was to >play around with Java interfaces. In the course of my experiments, I've >been running my head into a strange AttributeError. > >Since I'm not too familiar with Jython, I'll report the steps I've done in >some detail; forgive me if much of this is extraneous! It is exactly the right amount of detail. Thank you for that. >The error message >is at the very bottom, and this message traces what I've done to get the >message. > >I have the following Java interface: [snipped a traditional use of jythonc which works] >And this worked! > >However, I ran into problems when I tried playing with my classes in >Jython. To force Jython to use my jythonc-compiled classes, I made a new >directory called "scratch". Here's the log: > >... >Jython 2.1a3 on java1.3.0_02 (JIT: null) >Type "copyright", "credits" or "license" for more information. >>>> import Doubler >>>> Doubler ><jclass Doubler at 5868800> >>>> d = Doubler() >>>> d.eval(42) >Traceback (innermost last): > File "<console>", line 1, in ? >AttributeError: abstract method "eval" not implemented >### > >I'm just scratching my head on this one. Are Jythonc-compiled classes >reusable from Jython itself? Short answer: no. It is because "Doubler" is partly a java class and partly a python module. To see Doubler function as a python module you can use this trick: import Doubler Doubler.moduleDictInit(Doubler.__dict__) d = Doubler.Doubler() print d.eval(42) There is no trick to make Doubler appear as a java class. The code that does that is located at the bottom half of Py.initProxy() and can't be called directly. regards, finn |