jpython casting to abstract java class
Douglas du Boulay
ddb at crystal.uwa.edu.au
Mon Jun 26 03:55:05 EDT 2000
Hi,
I have a small(ish) code fragment below which fails to
cast a python-subclass of an abstract-java-base-class back to that
java-base-class when it is passed as input to another
java (constuctor) method.
There also seems to be that no checking that all the abstract
methods of the abstract base class are implemented.
Is there anything I can do to make this work (it works fine in
jpython1.0.3,
but is broken in jpython1.1 +latest CVS version + Finn Bock's errata-07,
all with java 1.1.7)
thanks
doug
-------------- next part --------------
<pre>
#!/usr/bin/env jpython
# file: testMyClass.py
import sys
sys.add_package('parser')
from parser import PyReadInterface,PyReader
class reader(PyReadInterface): # PyReadInterface is an abstract class
"@sig class reader extends PyReadInterface"
def __init__(self,other):
self.other=other
def read(self,n):
"@sig public String read(int n)"
return String(self.other.read(n))
def close(self):
"@sig public void close(void)"
self.other.close()
class MyClass:
def __init__(self,fpin=sys.stdin):
self.locals={}
self.fpin=fpin
def read(self,n):
return self.fpin.readline()
def close(self):
pass
def test(self):
k=reader(self)
m = PyReader(k) # reader should be reduced to PyReadInterface
# unfortunately it isn't and we get:
#TypeError:parser.PyReader(): 1st arg can't be coerced to parser.PyReadInterface
# print k
# print dir(k)
# print k.__class__
# print k.__class__.__dict__
# print k.__class__.__name__
# print k.__class__.__bases__
# print k.__class__.__bases__[0]
# l = k.__class__.__tojava__(k.__class__.__bases__[0])
# print l
# print l.__class__
# print l.__class__.__name__
# print super.__tojava__(k)
print "didn't raise an exception! maybe its working now?"
if __name__ == '__main__':
MyClass().test()
# endOfFile: testMyClass.py
/* file: parser/PyReadInterface.java */
package parser;
public abstract class PyReadInterface {
public abstract void close();
public abstract String read(int n) ;
}
/* endOfFile: parser/PyReadInterface.java */
/* file: parser/PyReader.java */
package parser;
// simple wrapper for a python class
// so that it can deliver characters to
// the tokenmanager
// python classes should inherit from PyReadInterface
public class PyReader extends java.io.Reader {
public PyReadInterface py;
public PyReader(PyReadInterface py) {
this.py=py;
}
public int read(char[] buffer,int n,int m) {
String ss=py.read(m);
if (ss.length()==0) return -1;
ss.getChars(0,ss.length(),buffer,n);
return ss.length();
}
public void close() {
py.close();
}
}
/* endOfFile: parser/PyReader.java */
</pre>
More information about the Python-list
mailing list