From: <fwi...@us...> - 2009-01-22 02:21:14
|
Revision: 5956 http://jython.svn.sourceforge.net/jython/?rev=5956&view=rev Author: fwierzbicki Date: 2009-01-22 02:21:08 +0000 (Thu, 22 Jan 2009) Log Message: ----------- Fix for http://bugs.jython.org/issue1239. When jars where added to sys.path, they did not always trigger package caching, leading to unexpected import failures. This is Charlie Grove's fix, which was much better than mine. Modified Paths: -------------- trunk/jython/Lib/test/test_classpathimporter.py trunk/jython/src/org/python/core/SyspathJavaLoader.java trunk/jython/src/org/python/core/imp.java Added Paths: ----------- trunk/jython/Lib/test/bug1239.jar Added: trunk/jython/Lib/test/bug1239.jar =================================================================== (Binary files differ) Property changes on: trunk/jython/Lib/test/bug1239.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/jython/Lib/test/test_classpathimporter.py =================================================================== --- trunk/jython/Lib/test/test_classpathimporter.py 2009-01-21 23:32:55 UTC (rev 5955) +++ trunk/jython/Lib/test/test_classpathimporter.py 2009-01-22 02:21:08 UTC (rev 5956) @@ -37,6 +37,17 @@ sys.path = ['__pyclasspath__/Lib'] self.setClassLoaderAndCheck("classimport_Lib.jar", "__pyclasspath__/Lib") + # I don't like the checked in jar file bug1239.jar. The *one* thing I + # liked about the tests in bugtests/ is that you could create a java file, + # compile it, jar it and destroy the jar when done. Maybe when we move to + # JDK 6 and can use JSR-199 to do runtime compiling, we can go back to + # that. Anyway, see http://bugs.jython.org/issue1239. In short, jars added + # with sys.path.append where not getting scanned if they start with a top + # level package we already have, like the "org" in org.python.* + def test_bug1239(self): + sys.path.append("Lib/test/bug1239.jar") + import org.test403javapackage.test403 + def test_main(): test_support.run_unittest(ClasspathImporterTestCase) Modified: trunk/jython/src/org/python/core/SyspathJavaLoader.java =================================================================== --- trunk/jython/src/org/python/core/SyspathJavaLoader.java 2009-01-21 23:32:55 UTC (rev 5955) +++ trunk/jython/src/org/python/core/SyspathJavaLoader.java 2009-01-22 02:21:08 UTC (rev 5956) @@ -50,7 +50,7 @@ PyList path = sys.path; for (int i = 0; i < path.__len__(); i++) { - PyObject entry = path.__getitem__(i); + PyObject entry = replacePathItem(sys, i, path); if (entry instanceof SyspathArchive) { SyspathArchive archive = (SyspathArchive) entry; ZipEntry ze = archive.getEntry(entryRes); @@ -65,8 +65,7 @@ } String dir = sys.getPath(entry.__str__().toString()); try { - return new BufferedInputStream(new FileInputStream(new File( - dir, res))); + return new BufferedInputStream(new FileInputStream(new File(dir, res))); } catch (IOException e) { continue; } @@ -75,11 +74,28 @@ return null; } + static PyObject replacePathItem(PySystemState sys, int idx, PyList paths) { + PyObject path = paths.__getitem__(idx); + if (path instanceof SyspathArchive) { + // already an archive + return path; + } + + try { + // this has the side affect of adding the jar to the PackageManager during the + // initialization of the SyspathArchive + path = new SyspathArchive(sys.getPath(path.toString())); + } catch (Exception e) { + return path; + } + paths.__setitem__(idx, path); + return path; + } + // override from abstract base class - protected Class loadClass(String name, boolean resolve) + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { - // First, if the Python runtime system has a default class loader, - // defer to it. + // First, if the Python runtime system has a default class loader, defer to it. PySystemState sys = Py.getSystemState(); ClassLoader classLoader = sys.getClassLoader(); if (classLoader != null) { @@ -97,44 +113,43 @@ return Class.forName(name, true, ClassLoader.getSystemClassLoader()); } catch(ClassNotFoundException e) {} - Class c = findLoadedClass(name); + Class<?> c = findLoadedClass(name); if(c != null) { return c; } PyList path = sys.path; for(int i = 0; i < path.__len__(); i++) { - InputStream fis = null; - File file = null; - int size = 0; - PyObject entry = path.__getitem__(i); + + InputStream fis; + int size; + PyObject entry = replacePathItem(sys, i, path); if(entry instanceof SyspathArchive) { SyspathArchive archive = (SyspathArchive)entry; String entryname = name.replace('.', SLASH_CHAR) + ".class"; ZipEntry ze = archive.getEntry(entryname); - if(ze != null) { - try { - fis = archive.getInputStream(ze); - size = (int)ze.getSize(); - } catch(IOException exc) { - ; - } + if(ze == null) { + continue; } + try { + fis = archive.getInputStream(ze); + size = (int)ze.getSize(); + } catch (IOException exc) { + continue; + } } else { String dir = entry.__str__().toString(); - file = getFile(dir, name); - if(file != null) { - size = (int)file.length(); - try { - fis = new FileInputStream(file); - } catch(FileNotFoundException e) { - ; - } + File file = getFile(dir, name); + if (file == null) { + continue; } + size = (int)file.length(); + try { + fis = new FileInputStream(file); + } catch (FileNotFoundException e) { + continue; + } } - if(fis == null) { - continue; - } try { byte[] buffer = new byte[size]; int nread = 0; @@ -143,14 +158,12 @@ } fis.close(); return loadClassFromBytes(name, buffer); - } catch(IOException e) { - continue; + } catch (IOException e) { + } finally { try { fis.close(); - } catch(IOException e) { - continue; - } + } catch (IOException e) {} } } @@ -173,9 +186,9 @@ return new RelativeFile(dir, accum + ".class"); } - private Class loadClassFromBytes(String name, byte[] data) { + private Class<?> loadClassFromBytes(String name, byte[] data) { // System.err.println("loadClassFromBytes("+name+", byte[])"); - Class c = defineClass(name, data, 0, data.length); + Class<?> c = defineClass(name, data, 0, data.length); resolveClass(c); Compiler.compileClass(c); return c; Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-01-21 23:32:55 UTC (rev 5955) +++ trunk/jython/src/org/python/core/imp.java 2009-01-22 02:21:08 UTC (rev 5956) @@ -318,41 +318,12 @@ return importer; } - static PyObject replacePathItem(PySystemState sys, PyObject path) { - if (path instanceof SyspathArchive) { - // already an archive - return null; - } - - try { - // this has the side affect of adding the jar to the PackageManager - // during the initialization of the SyspathArchive - return new SyspathArchive(sys.getPath(path.toString())); - } catch (Exception e) { - return null; - } - } - static PyObject find_module(String name, String moduleName, PyList path) { PyObject loader = Py.None; PySystemState sys = Py.getSystemState(); PyObject metaPath = sys.meta_path; - /* - * Needed to convert all entries on the path to SyspathArchives if - * necessary. - */ - PyList ppath = path == null ? sys.path : path; - for (int i = 0; i < ppath.__len__(); i++) { - PyObject p = ppath.__getitem__(i); - PyObject q = replacePathItem(sys, p); - if (q == null) { - continue; - } - ppath.__setitem__(i, q); - } - for (PyObject importer : metaPath.asIterable()) { PyObject findModule = importer.__getattr__("find_module"); loader = findModule.__call__(new PyObject[] { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |