From: Oti <oh...@ya...> - 2002-05-29 20:58:56
|
Hello Samuele, please find below my second approach, hopefully closer to your requirements. I'd like to hear your comments before I flush the SF package manager with more code. This approach: - keeps the precedence 'python over java' - does not load any classes - IMHO is at the highest level possible - passes test/test_import.py from the CPython 2.1.3 distribution - passes test/dynamic_import.py - allows package imports, too - enables fixing a bug The idea is still the same: If (for whatever reasons) a java package was neither scanned nor added with sys.add_package(), we add it dynamically, after all other imports failed. There is a unittest attached (dynamic_import.py) which I expect to fail with standard jython 2.1 (ImportError: no module named dynamic), while it should succeed with the changes to imp.java and __builtin__.java attached and described below. In order to run this test, please add dynamic_import.jar to the classpath, but do not add dynamic_extension.jar. Both jars have to be in the same directory, though. As a second step, you could disable the package scan (patch No. 525092; safely leave the .py files untouched) and delete the cachedir. Again the test should pass! Thank you very much for your time reviewing this. Best wishes, Oti. imp.java ( 'Release_2_1' = revision 2.59 ) ------------------------------------------ - add the constant: String NO_MODULE_NAMED = "no module named "; - replace the two "N/no module named" strings with this constant: throw Py.ImportError(NO_MODULE_NAMED + name); - add the two methods isJavaClass() and isJavaPackage() __builtin__.java ( 'Release_2_1' = revision 2.41 ) -------------------------------------------------- There are only changes to class ImportFunctions: - in method __call__(), replace load() with dynamicLoad() - add the methods: dynamicLoad(), findJavaPackage(), getFirstFrom(), eliminateNoneModules() While developing the above I was able to fix a funny bug. Consider the following situation: 1) somewhere on the classpath, there is aClass.class in package p1.p2: /p1 +-- /p2 +- aClass.class 2) somewhere on sys.path, there is aModule.py in directory /m1, and /m1 has an empty subdirectory /p1: /m1 +-- aModule.py contains 'from p1.p2 import aClass' +-- __init__.py empty +-- /p1 empty directory Now 'import m1.aModule' with the embedded 'from p1.p2 import aClass' fails, while a plain 'from p1.p2 import aClass' succeeds. There is a unittest attached (blocking_import.py) which exposes exactly this situation. In order to run it, please copy javac.py from the /Tools/jythonc directory to the /Lib directory. I expect it to fail with standard jython 2.1 (ImportError: No module named p2), while it should succeed with the fixes to PyJavaPackage.java described below. However, I have no experience in importing java classes from sys.path, so this might break some of your tests ? PyJavaPackage.java ( 'Release_2_1' = revision 2.17 ) ---------------------------------------------------- in method __findattr__(), replace: if (__mgr__.packageExists(__name__,name)) { __mgr__.notifyPackageImport(__name__,name); return addPackage(name); } with if (__mgr__.packageExists(__name__,name)) { //check if it really is a java package String testPackage = ""; if (__name__ != null && __name__.length() > 0) testPackage = __name__ + "." + name; else testPackage = name; if (imp.isJavaPackage(testPackage)) { __mgr__.notifyPackageImport(__name__,name); return addPackage(name); } else { Py.writeDebug("import", "'" + testPackage + "' is not a java package."); } } __________________________________________________ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com |