Issue1373

classification
Title: Jython ClassLoader getResource does not work
Type: Severity: major
Components: Core Versions: 2.5.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: amak, boisgera, c.cerbo, ddaniels, fwierzbicki, jaraco, jdeolive, pjenvey, zyasoft
Priority: Keywords: patch

Created on 2009-06-11.09:16:40 by boisgera, last changed 2010-09-14.15:50:16 by jaraco.

Files
File name Uploaded Description Edit Remove
pck.jar boisgera, 2009-06-11.09:16:39
implement_systpathjavaloader_getresource.patch jdeolive, 2009-11-05.02:45:23
bug1373.jar jdeolive, 2009-11-05.02:46:24 jar required for test case, located in Lib/tests
jython-dev.jar c.cerbo, 2010-07-07.23:09:09
Messages
msg4809 (view) Author: (boisgera) Date: 2009-06-11.09:16:39
Resource files in jars that are added via sys.path won't be found by
getRessource. Consider the pck.jar file that contains pck/Main.java and
the resource pck/Main.txt:

    >>> import sys
    >>> sys.path.append("pck.jar")
    >>> import pck
    *sys-package-mgr*: processing new jar,
'/home/boisgera/SANDBOX/JYTHON-BUGS/RESOURCE/pck.jar'
    >>> pck.Main.getResource("Main.txt") is None
    True
    >>> pck.Main.getClassLoader()
    org.python.core.SyspathJavaLoader@13d21d6

On the other hand, if 'pck.jar' is already in the CLASSPATH, I end up with:

    >>> import pck
    >>> pck.Main.getResource("Main.txt")
   
jar:file:/home/boisgera/SANDBOX/JYTHON-BUGS/RESOURCE/pck.jar!/pck/Main.txt
    >>> pck.Main.getClassLoader()
    sun.misc.Launcher$AppClassLoader@17182c1

I guess that 'findResource' should be implemented in SyspathJavaLoader
for this to work. The method 'getResourceAsStream' being already
implemented, I guess that most of the work has already been done ... right ?
msg4846 (view) Author: Philip Jenvey (pjenvey) Date: 2009-06-21.23:14:25
Yea it looks like all the work is done it just needs a reshuffling of 
implemented methods
msg5301 (view) Author: Justin Deoliveira (jdeolive) Date: 2009-11-05.02:45:23
Included in the patch is a test case which requires a jar identical to the 
pck.jar file attached to this issue, but compiled under java 5 and renamed 
to bug1373.jar. I ran regrtest with the patch applied and saw no failures.
msg5528 (view) Author: Costantino Cerbo (c.cerbo) Date: 2010-02-15.13:18:52
This problem persists also with the new SyspathJavaLoader but the attached patch is obsolete. 

I've written a new patch:

Index: src/org/python/core/SyspathJavaLoader.java
===================================================================
--- src/org/python/core/SyspathJavaLoader.java	(revision 6980)
+++ src/org/python/core/SyspathJavaLoader.java	(working copy)
@@ -134,7 +134,7 @@
                 ZipEntry ze = archive.getEntry(entryRes);
                 if (ze != null) {
                 	try {
-						return new URL("jar:" + entry.__str__().toString() + "!/" + entryRes);
+						return new URL("jar:file:" + entry.__str__().toString() + "!/" + entryRes);
 					} catch (MalformedURLException e) {
 						throw new RuntimeException(e);
 					}
@@ -143,7 +143,11 @@
             }
             String dir = sys.getPath(entry.__str__().toString());
             try {
-				return new File(dir, res).toURI().toURL();
+				File resource = new File(dir, res);
+				if (!resource.exists()) {
+					continue;
+				}
+				return resource.toURI().toURL();
 			} catch (MalformedURLException e) {
 				throw new RuntimeException(e);
 			}


The unit test may be "recycled"
msg5880 (view) Author: Jason R. Coombs (jaraco) Date: 2010-07-07.21:45:11
I seem to have encountered this bug as well. This may be the issue I've brought up a couple of times in the past (going back to 2.1) being unable to have sys.path-added modules load their own resources.

In the past, I've used jclasspath (http://www.fiber-space.de/jynx/doc/jclasspath.html) to work around these issues (by appending the necessary classpath to the system Classpath, which requires bypassing java attribute accessibility rules).

I'm eager to see if this patch corrects these issues. Would it be possible to compile a .class file so I can inject it in my jython.jar and test, and if so would someone be willing to do so?
msg5881 (view) Author: Costantino Cerbo (c.cerbo) Date: 2010-07-07.23:09:09
Try with this jar. It contains the compiled patch.
msg5882 (view) Author: Costantino Cerbo (c.cerbo) Date: 2010-07-07.23:10:05
I meant jython-dev.jar, that I've just attached.
msg5898 (view) Author: Jason R. Coombs (jaraco) Date: 2010-07-15.01:57:27
I'm trying to test the compiled patch, but I'm not sure how. When I copy jython-dev.jar to my Jython home directory, I get a traceback (below). If I just inject the SyspathJavaLoader.class into the stock 2.5.1 jython.jar, I get only an 'Exception in thread "main"' error (and the program exits).

Any suggestions?

Exception in thread "main" java.lang.NoClassDefFoundError: org/jruby/ext/posix/POSIXHandler
        at org.python.modules.Setup.<clinit>(Setup.java:62)
        at org.python.core.PySystemState.initBuiltins(PySystemState.java:1000)
        at org.python.core.PySystemState.doInitialize(PySystemState.java:837)
        at org.python.core.PySystemState.initialize(PySystemState.java:756)
        at org.python.core.PySystemState.initialize(PySystemState.java:706)
        at org.python.core.PySystemState.initialize(PySystemState.java:699)
        at org.python.util.jython.run(jython.java:150)
        at org.python.util.jython.main(jython.java:129)
Caused by: java.lang.ClassNotFoundException: org.jruby.ext.posix.POSIXHandler
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
        ... 8 more
msg5909 (view) Author: Jim Baker (zyasoft) Date: 2010-07-20.04:29:37
Fixed by r7080
msg5912 (view) Author: Jason R. Coombs (jaraco) Date: 2010-07-20.11:55:35
Thanks to everyone for getting this patch applied. Is there someplace I can pick up a nightly build so I can test this against our failing case?
msg6069 (view) Author: Jason R. Coombs (jaraco) Date: 2010-09-14.15:50:16
Confirmed fixed in 2.5.2b2. Thanks to everyone for making this happen.
History
Date User Action Args
2010-09-14 15:50:16jaracosetmessages: + msg6069
2010-09-08 16:12:23ddanielssetnosy: + ddaniels
2010-07-20 11:55:36jaracosetmessages: + msg5912
2010-07-20 04:29:39zyasoftsetstatus: open -> closed
resolution: fixed
messages: + msg5909
2010-07-15 01:57:29jaracosetmessages: + msg5898
2010-07-07 23:10:05c.cerbosetmessages: + msg5882
2010-07-07 23:09:09c.cerbosetfiles: + jython-dev.jar
messages: + msg5881
2010-07-07 21:45:12jaracosetnosy: + jaraco
messages: + msg5880
2010-04-26 01:30:18zyasoftsettitle: Jython ClassLoader getRessource does not work -> Jython ClassLoader getResource does not work
2010-04-26 01:26:10zyasoftsetassignee: zyasoft
nosy: + zyasoft
2010-02-15 13:19:05c.cerbosetseverity: normal -> major
versions: + 2.5.1, - 25rc4
2010-02-15 13:18:54c.cerbosetnosy: + c.cerbo
messages: + msg5528
2009-11-10 19:13:00fwierzbickisetnosy: + fwierzbicki
2009-11-10 17:02:17amaksetnosy: + amak
2009-11-05 02:46:24jdeolivesetfiles: + bug1373.jar
2009-11-05 02:45:25jdeolivesetfiles: + implement_systpathjavaloader_getresource.patch
nosy: + jdeolive
messages: + msg5301
keywords: + patch
2009-06-21 23:14:25pjenveysetnosy: + pjenvey
messages: + msg4846
2009-06-11 09:16:40boisgeracreate