From: Humbel O. <Otm...@bi...> - 2001-09-10 12:31:36
|
Hello, when there is an 'invalid' .jar file in the classpath from where java can load classes, but from which the jython package manager cannot determine packages (other people have told us about similar problems with the jar utility here), I would nonetheless like to be able to import single java classes. The problem manifests as follows (package CH.obj.Libraries.Servlet lives inside the offending bisonInfra.jar): [appl|pwe] > jython *sys-package-mgr*: processing modified jar, 'E:\java\lib\bisonInfra.jar' *sys-package-mgr*: skipping bad jar, 'e:\java\lib\bisonInfra.jar' Jython 2.0 on java1.3.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> from CH.obj.Libraries.Servlet import ServletSessionContext Traceback (innermost last): File "<console>", line 1, in ? ImportError: No module named Servlet >>> from java.lang import Class >>> clazz = Class.forName( "CH.obj.Libraries.Servlet.ServletSessionContext" ) >>> clazz <jclass CH.obj.Libraries.Servlet.ServletSessionContext at 7698425> >>> For an 'intuitive' Jython user like me, there is no obvious reason why this import should fail ? My motto is: Jython can import everything java can load ! With the following lines added just at the beginning of method importFromAs in org/python/core/imp.java (from the 2.0 codebase): public static void importFromAs(String mod, String[] names, String[] asnames, PyFrame frame) { if ( names.length==1 && asnames.length==names.length && asnames[0].equals(names[0]) ) { // this is a candidate for simple java class import String packageName = mod; String fullClassName = packageName + "." + names[0]; try { Class.forName( fullClassName ); PySystemState.add_package( packageName ); } catch( Throwable t ) {} } // rest of method left untouched // ... } the problem above would be solved. These lines do not at all claim to be perfect, they are thought just to show that it could be done: [appl|pwe] > jython *sys-package-mgr*: processing modified jar, 'E:\java\lib\bisonInfra.jar' *sys-package-mgr*: skipping bad jar, 'e:\java\lib\bisonInfra.jar' Jython 2.0 on java1.3.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> from CH.obj.Libraries.Servlet import ServletSessionContext >>> Please let me know what you think. Do not hesitate to blame me if this is ... Have fun, Oti. |
From: <bc...@wo...> - 2001-09-12 18:13:43
|
[Humbel Otmar] >Hello, > >when there is an 'invalid' .jar file in the classpath from where java can >load classes, but from which the jython package manager cannot determine >packages (other people have told us about similar problems with the jar >utility here), I would nonetheless like to be able to import single java >classes. > >The problem manifests as follows (package CH.obj.Libraries.Servlet lives >inside the offending bisonInfra.jar): > > >[appl|pwe] > jython >*sys-package-mgr*: processing modified jar, 'E:\java\lib\bisonInfra.jar' >*sys-package-mgr*: skipping bad jar, 'e:\java\lib\bisonInfra.jar' >Jython 2.0 on java1.3.0 (JIT: null) >Type "copyright", "credits" or "license" for more information. >>>> from CH.obj.Libraries.Servlet import ServletSessionContext >Traceback (innermost last): > File "<console>", line 1, in ? >ImportError: No module named Servlet >>>> from java.lang import Class >>>> clazz = Class.forName( "CH.obj.Libraries.Servlet.ServletSessionContext" >) >>>> clazz ><jclass CH.obj.Libraries.Servlet.ServletSessionContext at 7698425> >>>> > > >For an 'intuitive' Jython user like me, there is no obvious reason why this >import should fail ? I agree. >My motto is: Jython can import everything java can load ! I agree again. I hope you at least understand why importing java packages fails. >With the following lines added just at the beginning of method importFromAs Please add this as a patch to the SF patch manager. That way we will not forget it. >in org/python/core/imp.java (from the 2.0 codebase): > > public static void importFromAs(String mod, String[] names, String[] >asnames, PyFrame frame) { > if ( names.length==1 && asnames.length==names.length && >asnames[0].equals(names[0]) ) { > // this is a candidate for simple java class import > String packageName = mod; > String fullClassName = packageName + "." + names[0]; > try { > Class.forName( fullClassName ); > PySystemState.add_package( packageName ); > } catch( Throwable t ) {} > } > // rest of method left untouched > // ... > } > >the problem above would be solved. These lines do not at all claim to be >perfect, they are thought just to show that it could be done: Of the top of my head, I kinda like it. It a pragmatic solution to a huge problem which we (I fear) will never resolve perfectly. regards, finn |