Hi Kevin,
Thank you for the example code. It illustrates my confusion perfectly. I
hope this sort of newbie mistake gets posted to the FAQ with your example
solution.
Andres Corrada-Emmanuel
Senior Research Fellow
Center for Intelligent Information Retrieval
University of Massachusetts, Amherst
On Thu, 25 Oct 2001, Kevin Butler wrote:
> You can do it just fine, but you have to set 'respectJavaAccessibility=false' in the jython registry (and this requires some permission in the Java VM - usually not a problem for Java applications, but applets may have problems).
>
> The reason that you still need to set this flag, even though you are generating code in the appropriate package , is that jythonc generates java code that uses the Python interpreter classes to do the method/attribute lookup. This means that the Python classes are trying to access the protected attribute, rather than the class that you generated.
>
> Example from my generated code, attempting to access the '_package' variable (package protected):
>
> Py.printComma(frame.getlocal(0).__getattr__("_package"));
>
> Note that it isn't doing the reflection itself, but is delegating to PyObject to do the reflection.
>
> My code & makefile attached below.
>
> kb
>
> ---Attribute.java
> package test;
>
> public class Attribute
> {
> private int _private = 1;
> int _package = 2;
> protected int _protected = 3;
> public int _public = 4;
> }
>
>
> ---testAttribute.py
> from test import Attribute
> class X( Attribute ):
> def go( self ):
> print self._package, self._private, self._protected, self._public
>
> X().go()
>
> ---Makefile
> testAttribute:
> javac -d . Attribute.java
> jythonc --package test testAttribute.py
> java -classpath .\;./jpywork\;w:/tools/jython/jython.jar test.testAttribute
>
> ---command:
> make testAttribute
>
> ---output:
> javac -d . Attribute.java
> jythonc --package test testAttribute.py
> processing testAttribute
>
> Required packages:
> test
>
> Creating adapters:
>
> Creating .java files:
> testAttribute module
> X extends test.Attribute
>
> Compiling .java to .class...
> Compiling with args: ['D:\\jdk1.3\\bin\\javac', '-classpath', '"w:/tools/jython/jython.jar;.;w:\\classes;w:\\lib\\ldapjdk.jar;w:\\lib\\xerces.jar;w:\\lib\\ldap.jar;w:\\lib\\providerutil.jar;w:\\lib\\jaas.jar;w:\\lib\\jndi.jar;w:\\lib\\ldapbp.jar;w:\\lib\\jsse-export\\jnet.jar;w:\\lib\\jsse-export\\jsse.jar;w:\\jars\\servlet.jar;w:\\;.\\jpywork;;w:\\tools\\jython\\Tools\\jythonc;e:\\work\\kb\\test\\jython\\.;w:\\tools\\jython\\Lib;w:\\src\\com\\pipeline\\test\\bom;w:\\src\\com\\pipeline\\test;d:\\python;w:\\tools\\jython"', '.\\jpywork\\test\\testAttribute.java']
> 0 Note: Some input files use or override a deprecated API.
> Note: Recompile with -deprecation for details.
>
> java -classpath .\;./jpywork\;w:/tools/jython/jython.jar test.testAttribute
> 2 1 3 4
>
>
> The '2 1 3 4' is the output of the command.
>
> It looks like it works to me - note that I have 'respectJavaAccessibility=false' in my jython registry.
>
> If I have 'respect...=true', I get:
>
> Java Traceback:
>
> at org.python.core.Py.AttributeError(Py.java:90)
> at org.python.core.PyObject.__getattr__(PyObject.java:692)
> at test.testAttribute$_PyInner.go$1(testAttribute.java:43)
> at test.testAttribute$_PyInner.call_function(testAttribute.java:32)
> at org.python.core.PyTableCode.call(PyTableCode.java:198)
> at org.python.core.PyTableCode.call(PyTableCode.java:253)
> at org.python.core.PyFunction.__call__(PyFunction.java:170)
> at org.python.core.PyInstance.invoke(PyInstance.java:236)
> at test.testAttribute$_PyInner.main$3(testAttribute.java:59)
> at test.testAttribute$_PyInner.call_function(testAttribute.java:36)
> at org.python.core.PyTableCode.call(PyTableCode.java:198)
> at org.python.core.PyCode.call(PyCode.java:13)
> at org.python.core.imp.createFromCode(imp.java:165)
> at org.python.core.Py.runMain(Py.java:818)
> at test.testAttribute.main(testAttribute.java:110)
> Traceback (innermost last):
> File "E:\work\kb\test\jython\testAttribute.py", line 0, in main
> File "E:\work\kb\test\jython\testAttribute.py", line 0, in go
> AttributeError: instance of 'X' has no attribute '_package'
>
> kb
>
> Andres Corrada-Emmanuel wrote:
> >
> > Hello,
> >
> > My problem has evolved to this thread. Let me recapitulate my intent:
> >
> > To create a Java class using Python code. The Python class subclasses from
> > a class in some package "edu.umass.cs.ciir.metamorph".
> >
> > I compile the Python class to a .class file using jythonc with the
> > package switch. The resultant .java file has the correct package
> > declaration:
> >
> > package edu.umass.cs.ciir.metamorph;
> >
> > at the top of the file.
> >
> > My Python class tries to access a package-accesible super attribute but
> > fails. If I make that attribute public it succeeds. This is
> > unsatisfactory. I'm trying to use someone elses code and I cannot change
> > the attribute properties.
> >
> > Can I still use Jython or do I need to abandon this line of approach and
> > resign myself to having to program in Java directly?
> >
> > Andres Corrada-Emmanuel
> > Senior Research Fellow
> > Center for Intelligent Information Retrieval
> > University of Massachusetts, Amherst
> >
> > _______________________________________________
> > Jython-users mailing list
> > Jython-users@...
> > https://lists.sourceforge.net/lists/listinfo/jython-users
>
|