Index: src/org/python/core/APIReader.java =================================================================== --- src/org/python/core/APIReader.java (revision 5632) +++ src/org/python/core/APIReader.java (working copy) @@ -29,8 +29,9 @@ private int version = -1; public APIReader(byte[] data) throws IOException { - ClassReader r = new ClassReader(data); - r.accept(this, 0); + if (data.length < 8) throw new IOException(); + ClassReader r = new ClassReader(data); + r.accept(this, 0); } public AnnotationVisitor visitAnnotation(String desc, boolean visible) { Index: src/org/python/core/imp.java =================================================================== --- src/org/python/core/imp.java (revision 5632) +++ src/org/python/core/imp.java (working copy) @@ -423,7 +423,62 @@ return createFromPyClass(name, stream, false, sourceName, compiledName); } + + // + // Load source or class file from Applet using getResourceAsStream + // + static PyObject loadFromSourceAppletResource(String name, String modName, String entry) { + Class thisClass = imp.class; + String appletprefix = "jar:"; + String path = entry.substring(appletprefix.length()) + "/"; + + // + // First see if we're loading a package by looking for __init__.py in a directory whose + // name is the module name + // + String sourceName = name + "/__init__.py"; + String compiledName = name + "/__init__$py.class"; + + InputStream is = thisClass.getResourceAsStream(path + sourceName); + boolean pkg = (is != null); + + if (!pkg) { + sourceName = name + ".py"; + compiledName = name + "$py.class"; + } + else { + PyModule m = addModule(modName); + PyObject filename = new PyString(path); + m.__dict__.__setitem__("__path__", new PyList(new PyObject[] { filename })); + m.__dict__.__setitem__("__file__", filename); + } + + // Look for compiled file first + InputStream compiledIs = thisClass.getResourceAsStream(path + compiledName); + if (compiledIs != null) { + Py.writeDebug(IMPORT_LOG, "trying precompiled " + (path + compiledName)); + PyObject ret = createFromPyClass(modName, + compiledIs, + true, + path + sourceName, + path + compiledName); + if (ret != null) return ret; + } + + // Look for source file + InputStream sourceIs = thisClass.getResourceAsStream(path + sourceName); + if (sourceIs != null) { + Py.writeDebug(IMPORT_LOG, "trying source " + (path + sourceName)); + return createFromSource(modName, sourceIs, path + sourceName); + } + + return null; + } + static PyObject loadFromSource(PySystemState sys, String name, String modName, String entry) { + if (entry.toString().startsWith("jar:")) + return loadFromSourceAppletResource(name, modName, entry); + String dirName = sys.getPath(entry); String sourceName = "__init__.py"; String compiledName = "__init__$py.class"; Index: src/org/python/core/PySystemState.java =================================================================== --- src/org/python/core/PySystemState.java (revision 5632) +++ src/org/python/core/PySystemState.java (working copy) @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.URLDecoder; import java.security.AccessControlException; @@ -36,6 +37,8 @@ public static final String JYTHON_JAR = "jython.jar"; private static final String JAR_URL_PREFIX = "jar:file:"; + private static final String JAR_URL_PREFIX_HTTP = "jar:http:"; + private static final String JAR_URL_PREFIX_HTTPS = "jar:https:"; private static final String JAR_SEPARATOR = "!"; public static PyString version = new PyString(Version.getVersion()); @@ -728,19 +731,25 @@ private static PyList initPath(Properties props, boolean standalone, String jarFileName) { PyList path = new PyList(); - addPaths(path, props.getProperty("python.path", "")); - if (prefix != null) { + if (jarFileName.startsWith("jar:")) { + // jar: is a special path prefix meaning that we should use getResourceAsStream to + // look in the jar file + path.append(new PyString("jar:/Lib")); + } + else { + addPaths(path, props.getProperty("python.path", "")); + if (prefix != null) { String libpath = new File(prefix.toString(), "Lib").toString(); path.append(new PyString(libpath)); - } - if (standalone) { + } + if (standalone) { // standalone jython: add the /Lib directory inside JYTHON_JAR to the path addPaths(path, jarFileName + "/Lib"); - } - + } + } return path; } - + /** * Check if we are in standalone mode. * @@ -750,11 +759,20 @@ */ private static boolean isStandalone(String jarFileName) { boolean standalone = false; + String testfile = "Lib/os.py"; + if (jarFileName != null) { + if (jarFileName.startsWith("jar:")) { + // We're running as an applet: see if we can load the test file from our jar + Class thisClass = PySystemState.class; + InputStream is = thisClass.getResourceAsStream("/" + testfile); + standalone = (is != null); + } + else { JarFile jarFile = null; try { jarFile = new JarFile(jarFileName); - JarEntry jarEntry = jarFile.getJarEntry("Lib/os.py"); + JarEntry jarEntry = jarFile.getJarEntry(testfile); standalone = jarEntry != null; } catch (IOException ioe) { } finally { @@ -765,6 +783,7 @@ } } } + } } return standalone; } @@ -786,7 +805,14 @@ if (urlString.startsWith(JAR_URL_PREFIX) && jarSeparatorIndex > 0) { jarFileName = urlString.substring(JAR_URL_PREFIX.length(), jarSeparatorIndex); } + else if ((urlString.startsWith(JAR_URL_PREFIX_HTTP) || + urlString.startsWith(JAR_URL_PREFIX_HTTP)) && jarSeparatorIndex > 0) { + // looks like an http URL to our own jar; set path to "jar:" so subsequent code + // knows to use getResourceAsStream instead of file I/O: + jarFileName = "jar:"; + } } catch (Exception e) { + } } return jarFileName; Index: src/org/python/core/Py.java =================================================================== --- src/org/python/core/Py.java (revision 5632) +++ src/org/python/core/Py.java (working copy) @@ -1823,7 +1823,7 @@ public static final int DEBUG = 3; public static void maybeWrite(String type, String msg, int level) { - if (level <= Options.verbose) { + if (true || level <= Options.verbose) { System.err.println(type + ": " + msg); } }