From: <zy...@us...> - 2010-08-15 14:09:17
|
Revision: 7094 http://jython.svn.sourceforge.net/jython/?rev=7094&view=rev Author: zyasoft Date: 2010-08-15 14:09:10 +0000 (Sun, 15 Aug 2010) Log Message: ----------- Define the package name for a class dynamically loaded from sys.path. Fixes #1455. Thanks to "stan" for the patch! Modified Paths: -------------- trunk/jython/Lib/test/test_zipimport_jy.py trunk/jython/src/org/python/core/SyspathJavaLoader.java Modified: trunk/jython/Lib/test/test_zipimport_jy.py =================================================================== --- trunk/jython/Lib/test/test_zipimport_jy.py 2010-08-14 21:00:19 UTC (rev 7093) +++ trunk/jython/Lib/test/test_zipimport_jy.py 2010-08-15 14:09:10 UTC (rev 7094) @@ -1,5 +1,6 @@ import unittest import sys +import java.lang.Package from test import test_support @@ -8,6 +9,8 @@ self.orig_path = sys.path sys.path.insert(0, test_support.findfile("syspath_import.jar")) + # TODO confirm that package is unloaded via a phantom ref or something like that + def tearDown(self): sys.path = self.orig_path @@ -15,6 +18,12 @@ from syspathonly import Syspath self.assertEquals(Syspath.staticCall(), "result") + def test_package_defined(self): + from syspathonly import Syspath + package = Syspath().class.package + self.assert_(isinstance(package, java.lang.Package)) + self.assertEquals(package.name, 'syspathonly') + def test_load_pkg_from_syspath(self): import syspathpkg self.assertEquals(syspathpkg.__name__, 'syspathpkg') Modified: trunk/jython/src/org/python/core/SyspathJavaLoader.java =================================================================== --- trunk/jython/src/org/python/core/SyspathJavaLoader.java 2010-08-14 21:00:19 UTC (rev 7093) +++ trunk/jython/src/org/python/core/SyspathJavaLoader.java 2010-08-15 14:09:10 UTC (rev 7094) @@ -84,6 +84,19 @@ return null; } } + + protected Package definePackageForClass(String name) { + int lastDotIndex = name.lastIndexOf('.'); + if (lastDotIndex < 0) { + return null; + } + String pkgname = name.substring(0, lastDotIndex); + Package pkg = getPackage(pkgname); + if (pkg == null) { + pkg = definePackage(pkgname, null, null, null, null, null, null, null); + } + return pkg; + } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { @@ -106,6 +119,7 @@ buffer = getBytesFromDir(dir, name); } if (buffer != null) { + definePackageForClass(name); return defineClass(name, buffer, 0, buffer.length); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |