From: <fwi...@us...> - 2009-07-13 16:32:48
|
Revision: 6536 http://jython.svn.sourceforge.net/jython/?rev=6536&view=rev Author: fwierzbicki Date: 2009-07-13 16:32:44 +0000 (Mon, 13 Jul 2009) Log Message: ----------- Fix for http://bugs.jython.org/issue645615 "cannot import through symbolic links". Now only explicitely enforcing case sensative imports on Windows and Mac. In the presense of both case insensative systems and symlinks there may be a performance penalty, as we walk the directory looking for exact matches in this case. This is the same method used in CPython. Modified Paths: -------------- trunk/jython/Lib/test/test_import_jy.py trunk/jython/NEWS trunk/jython/build.xml trunk/jython/src/org/python/core/imp.java Added Paths: ----------- trunk/jython/src/org/python/core/util/PlatformUtil.java Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2009-07-13 15:29:30 UTC (rev 6535) +++ trunk/jython/Lib/test/test_import_jy.py 2009-07-13 16:32:44 UTC (rev 6536) @@ -164,6 +164,28 @@ def test_sys_modules_deletion(self): self.assertRaises(ZeroDivisionError, __import__, 'test.module_deleter') + #XXX: this is probably a good test to push upstream to CPython. + if hasattr(os, "symlink"): + def test_symlinks(self): + # Ensure imports work over symlinks. Did not work in Jython from + # 2.1 to 2.5.0, fixed in 2.5.1 See + # http://bugs.jython.org/issue645615. + sym = test_support.TESTFN+"1" + try: + os.mkdir(test_support.TESTFN) + init = os.path.join(test_support.TESTFN, "__init__.py") + fp = open(init, 'w') + fp.write("test = 'imported'") + fp.close() + os.symlink(test_support.TESTFN, sym) + module = os.path.basename(sym) + module_obj = __import__(module) + self.assertEquals(module_obj.test, 'imported') + + finally: + shutil.rmtree(test_support.TESTFN) + test_support.unlink(sym) + def test_main(): test_support.run_unittest(MislabeledImportTestCase, OverrideBuiltinsImportTestCase, Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-07-13 15:29:30 UTC (rev 6535) +++ trunk/jython/NEWS 2009-07-13 16:32:44 UTC (rev 6536) @@ -7,6 +7,7 @@ - Setting __javaname__ in classes subclassing Java classes or implementing Java interfaces sets the name of the produced proxy class. Bugs Fixed + - [ 645615 ] cannot import through symbolic links - [ 1366 ] parsing of lamda expression fails - [ 1365 ] continuation lines fail in interactive interpreter - [ 1377 ] Event names shadowed by a field name on Java types leads to a NPE Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-07-13 15:29:30 UTC (rev 6535) +++ trunk/jython/build.xml 2009-07-13 16:32:44 UTC (rev 6536) @@ -158,6 +158,7 @@ <pathelement path="${extlibs.dir}/asm-3.1.jar" /> <pathelement path="${extlibs.dir}/asm-commons-3.1.jar" /> <pathelement path="${extlibs.dir}/constantine-0.4.jar" /> + <pathelement path="${extlibs.dir}/jna.jar"/> <pathelement path="${extlibs.dir}/jna-posix.jar"/> </path> Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-07-13 15:29:30 UTC (rev 6535) +++ trunk/jython/src/org/python/core/imp.java 2009-07-13 16:32:44 UTC (rev 6536) @@ -11,6 +11,7 @@ import org.python.compiler.Module; import org.python.core.util.FileUtil; +import org.python.core.util.PlatformUtil; /** * Utility functions for "import" support. @@ -527,12 +528,26 @@ } public static boolean caseok(File file, String filename) { - if (Options.caseok) { + if (Options.caseok || !PlatformUtil.isCaseInsensitive()) { return true; } try { File canFile = new File(file.getCanonicalPath()); - return filename.regionMatches(0, canFile.getName(), 0, filename.length()); + boolean match = filename.regionMatches(0, canFile.getName(), 0, filename.length()); + if (!match) { + //possibly a symlink. Get parent and look for exact match in listdir() + //This is what CPython does in the case of Mac OS X and Cygwin. + //XXX: This will be a performance hit, maybe jdk7 nio2 can give us a better + // method? + File parent = file.getParentFile(); + String[] children = parent.list(); + for (String c: children) { + if (c.equals(filename)) { + return true; + } + } + } + return match; } catch (IOException exc) { return false; } Added: trunk/jython/src/org/python/core/util/PlatformUtil.java =================================================================== --- trunk/jython/src/org/python/core/util/PlatformUtil.java (rev 0) +++ trunk/jython/src/org/python/core/util/PlatformUtil.java 2009-07-13 16:32:44 UTC (rev 6536) @@ -0,0 +1,24 @@ +// Copyright (c) 2009 Jython project +package org.python.core.util; + +// Note: Sun does not sponsor the jna project, even though the package name +// might imply otherwise. +import com.sun.jna.Platform; + +/** + * Methods for testing the platform/operating system that we are on. + */ +public class PlatformUtil { + + /** + * @return True if the operating system we are on is case insensitive, + * where case insensitive means that a file that is stored as FOO + * can be accessed as (for example) FoO. + */ + //Currently we just check to see if we are on windows or macs, which are + //commonly (though not always!) case insensitive. There are certainly cases + //where this is not sufficient, like the case of mounted filesystems. + public static boolean isCaseInsensitive() { + return Platform.isMac() || Platform.isWindows(); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |