From: <fwi...@us...> - 2009-04-22 03:03:10
|
Revision: 6252 http://jython.svn.sourceforge.net/jython/?rev=6252&view=rev Author: fwierzbicki Date: 2009-04-22 03:03:00 +0000 (Wed, 22 Apr 2009) Log Message: ----------- Applied patch from http://bugs.jython.org/issue1188: Patch against trunk to handle SecurityExceptions. This should make it much easier to run Jython in GAE! Thanks to James Robinson for the patch! Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS trunk/jython/NEWS trunk/jython/src/org/python/core/BytecodeLoader.java trunk/jython/src/org/python/core/PyTraceback.java trunk/jython/src/org/python/core/SyspathJavaLoader.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/core/packagecache/PathPackageManager.java trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/ACKNOWLEDGMENTS 2009-04-22 03:03:00 UTC (rev 6252) @@ -82,6 +82,7 @@ Sean McGrath Clark Updike Leonardo Soto + James Robinson Local Variables: mode: indented-text Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/NEWS 2009-04-22 03:03:00 UTC (rev 6252) @@ -2,6 +2,7 @@ Jython 2.5.0 rc 1 Bugs fixed (new numbering due to move to Roundup) + - [ 1188 ] Patch against trunk to handle SecurityExceptions - [ 1271 ] Bean property accessors in derived class overide methods in base class - [ 1264 ] 'is not' test exhibits incorrect behaviour when wrapping Java objects - [ 1295 ] Setting a write-only bean property causes a NPE Modified: trunk/jython/src/org/python/core/BytecodeLoader.java =================================================================== --- trunk/jython/src/org/python/core/BytecodeLoader.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/core/BytecodeLoader.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -1,7 +1,8 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.security.SecureClassLoader; +import java.net.URL; +import java.net.URLClassLoader; import java.util.List; import org.objectweb.asm.ClassReader; @@ -30,7 +31,8 @@ if (cur != null) { loader.addParent(cur); } - } catch (SecurityException e) {} + } catch (SecurityException e) { + } } return loader.loadClassFromBytes(name, data); } @@ -71,11 +73,12 @@ } } - public static class Loader extends SecureClassLoader { + public static class Loader extends URLClassLoader { private List<ClassLoader> parents = Generic.list(); public Loader() { + super(new URL[0]); parents.add(imp.getSyspathJavaLoader()); } @@ -115,7 +118,6 @@ } Class<?> c = defineClass(name, data, 0, data.length, getClass().getProtectionDomain()); resolveClass(c); - Compiler.compileClass(c); return c; } } Modified: trunk/jython/src/org/python/core/PyTraceback.java =================================================================== --- trunk/jython/src/org/python/core/PyTraceback.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/core/PyTraceback.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -50,9 +50,13 @@ */ private String getLine(String filename, int lineno) { RelativeFile file = new RelativeFile(filename); - if (!file.isFile() || !file.canRead()) { - // XXX: We should run through sys.path until the filename is found - return null; + try { + if (!file.isFile() || !file.canRead()) { + // XXX: We should run through sys.path until the filename is found + return null; + } + } catch (SecurityException e) { + return null; // If we don't have read access to the file, return null } PyFile pyFile; Modified: trunk/jython/src/org/python/core/SyspathJavaLoader.java =================================================================== --- trunk/jython/src/org/python/core/SyspathJavaLoader.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/core/SyspathJavaLoader.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -105,13 +105,16 @@ // Search the sys.path for a .class file matching the named class. try { return Class.forName(name); - } catch(ClassNotFoundException e) {} + } catch(ClassNotFoundException e) { + } // The current class loader may be null (e.g., when Jython is loaded // from the boot classpath); try the system class loader. try { return Class.forName(name, true, ClassLoader.getSystemClassLoader()); - } catch(ClassNotFoundException e) {} + } catch(ClassNotFoundException e) { + } catch (SecurityException se) { + } Class<?> c = findLoadedClass(name); if(c != null) { @@ -143,11 +146,13 @@ if (file == null) { continue; } - size = (int)file.length(); try { + size = (int)file.length(); fis = new FileInputStream(file); } catch (FileNotFoundException e) { continue; + } catch(SecurityException e) { + continue; } } try { @@ -163,7 +168,8 @@ } finally { try { fis.close(); - } catch (IOException e) {} + } catch (IOException e) { + } } } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/core/imp.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -214,6 +214,10 @@ } FileOutputStream fop = null; try { + SecurityManager man = System.getSecurityManager(); + if (man != null) { + man.checkWrite(compiledFilename); + } fop = new FileOutputStream(compiledFilename); fop.write(compiledSource); fop.close(); @@ -223,6 +227,11 @@ Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '" + compiledFilename + "' due to " + exc); return null; + } catch(SecurityException exc) { + // If we can't write the cache file, just log and continue + Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '" + + compiledFilename + "' due to " + exc); + return null; } finally { if(fop != null) { try { @@ -463,8 +472,12 @@ File sourceFile = new File(dir, sourceName); File compiledFile = new File(dir, compiledName); - boolean pkg = dir.isDirectory() && caseok(dir, name) && (sourceFile.isFile() + boolean pkg = false; + try { + pkg = dir.isDirectory() && caseok(dir, name) && (sourceFile.isFile() || compiledFile.isFile()); + } catch (SecurityException e) { + } if (!pkg) { Py.writeDebug(IMPORT_LOG, "trying source " + dir.getPath()); sourceName = name + ".py"; @@ -479,28 +492,33 @@ m.__dict__.__setitem__("__path__", new PyList(new PyObject[] {filename})); } - if (sourceFile.isFile() && caseok(sourceFile, sourceName)) { - long pyTime = sourceFile.lastModified(); - if (compiledFile.isFile() && caseok(compiledFile, compiledName)) { - Py.writeDebug(IMPORT_LOG, "trying precompiled " + compiledFile.getPath()); - long classTime = compiledFile.lastModified(); - if (classTime >= pyTime) { - PyObject ret = createFromPyClass(modName, makeStream(compiledFile), true, - displaySourceName, displayCompiledName, pyTime); - if (ret != null) { - return ret; + try { + if (sourceFile.isFile() && caseok(sourceFile, sourceName)) { + long pyTime = sourceFile.lastModified(); + if (compiledFile.isFile() && caseok(compiledFile, compiledName)) { + Py.writeDebug(IMPORT_LOG, "trying precompiled " + compiledFile.getPath()); + long classTime = compiledFile.lastModified(); + if (classTime >= pyTime) { + PyObject ret = createFromPyClass(modName, makeStream(compiledFile), true, + displaySourceName, displayCompiledName, pyTime); + if (ret != null) { + return ret; + } } + return createFromSource(modName, makeStream(sourceFile), displaySourceName, + compiledFile.getPath()); } + return createFromSource(modName, makeStream(sourceFile), displaySourceName, + compiledFile.getPath(), pyTime); } - return createFromSource(modName, makeStream(sourceFile), displaySourceName, - compiledFile.getPath(), pyTime); - } - // If no source, try loading precompiled - Py.writeDebug(IMPORT_LOG, "trying precompiled with no source " + compiledFile.getPath()); - if (compiledFile.isFile() && caseok(compiledFile, compiledName)) { - return createFromPyClass(modName, makeStream(compiledFile), true, displaySourceName, - displayCompiledName); + // If no source, try loading precompiled + Py.writeDebug(IMPORT_LOG, "trying precompiled with no source " + compiledFile.getPath()); + if (compiledFile.isFile() && caseok(compiledFile, compiledName)) { + return createFromPyClass(modName, makeStream(compiledFile), true, displaySourceName, + displayCompiledName); + } + } catch (SecurityException e) { } return null; } Modified: trunk/jython/src/org/python/core/packagecache/PathPackageManager.java =================================================================== --- trunk/jython/src/org/python/core/packagecache/PathPackageManager.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/core/packagecache/PathPackageManager.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -41,20 +41,24 @@ String dir = path.pyget(i).__str__().toString(); File f = new RelativeFile(dir, child); - if (f.isDirectory() && imp.caseok(f, name)) { - /* - * Figure out if we have a directory a mixture of python and - * java or just an empty directory (which means Java) or a - * directory with only Python source (which means Python). - */ - PackageExistsFileFilter m = new PackageExistsFileFilter(); - f.listFiles(m); - boolean exists = m.packageExists(); - if (exists) { - Py.writeComment("import", "java package as '" - + f.getAbsolutePath() + "'"); + try { + if (f.isDirectory() && imp.caseok(f, name)) { + /* + * Figure out if we have a directory a mixture of python and + * java or just an empty directory (which means Java) or a + * directory with only Python source (which means Python). + */ + PackageExistsFileFilter m = new PackageExistsFileFilter(); + f.listFiles(m); + boolean exists = m.packageExists(); + if (exists) { + Py.writeComment("import", "java package as '" + + f.getAbsolutePath() + "'"); + } + return exists; } - return exists; + } catch (SecurityException se) { + return false; } } return false; Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-04-22 02:23:54 UTC (rev 6251) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-04-22 03:03:00 UTC (rev 6252) @@ -91,11 +91,12 @@ prefix = ""; while (true) { File fullPathFile = new File(sys.getPath(pathFile.getPath())); - if (fullPathFile.exists()) { - if (fullPathFile.isFile()) { - archive = pathFile.getPath(); - } - break; + try { + if (fullPathFile.isFile()) { + archive = pathFile.getPath(); + break; + } + } catch (SecurityException se) { } // back up one path element This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |