[Jython-checkins] jython (merge default -> default): Merge recent chdir fixes to trunk

jeff.allen jython-checkins at python.org
Mon Apr 13 01:24:48 CEST 2015


https://hg.python.org/jython/rev/b203c503b26d
changeset:   7658:b203c503b26d
parent:      7655:ea5fe0f38096
parent:      7657:b14c97e4486c
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Mon Apr 13 00:24:36 2015 +0100
summary:
  Merge recent chdir fixes to trunk

files:
  src/org/python/modules/posix/PosixModule.java |  32 +++++++--
  1 files changed, 25 insertions(+), 7 deletions(-)


diff --git a/src/org/python/modules/posix/PosixModule.java b/src/org/python/modules/posix/PosixModule.java
--- a/src/org/python/modules/posix/PosixModule.java
+++ b/src/org/python/modules/posix/PosixModule.java
@@ -49,6 +49,7 @@
 import org.python.core.PyList;
 import org.python.core.PyObject;
 import org.python.core.PyString;
+import org.python.core.PySystemState;
 import org.python.core.PyTuple;
 import org.python.core.imp;
 import org.python.core.Untraversable;
@@ -284,12 +285,23 @@
         "chdir(path)\n\n" +
         "Change the current working directory to the specified path.");
     public static void chdir(PyObject path) {
+        PySystemState sys = Py.getSystemState();
+        Path absolutePath = absolutePath(path);
         // stat raises ENOENT for us if path doesn't exist
-        Path absolutePath = absolutePath(path);
         if (!basicstat(path, absolutePath).isDirectory()) {
             throw Py.OSError(Errno.ENOTDIR, path);
         }
-        Py.getSystemState().setCurrentWorkingDir(absolutePath.toString());
+        if (os == OS.NT) {
+            // No symbolic links and preserve dos-like names (e.g. PROGRA~1)
+            sys.setCurrentWorkingDir(absolutePath.toString());
+        } else {
+            // Resolve symbolic links
+            try {
+                sys.setCurrentWorkingDir(absolutePath.toRealPath().toString());
+            } catch (IOException ioe) {
+                throw Py.OSError(ioe);
+            }
+        }
     }
 
     public static PyString __doc__chmod = new PyString(
@@ -1132,12 +1144,18 @@
         }
         try {
             Path path = Paths.get(pathStr);
-            if (!path.isAbsolute()) {
-                // Relative path: augment from current working directory.
-                path = Paths.get(Py.getSystemState().getCurrentWorkingDir()).resolve(path);
+            // Relative path: augment from current working directory.
+            path = Paths.get(Py.getSystemState().getCurrentWorkingDir()).resolve(path);
+            // In case of a root different from cwd, resolve does not guarantee absolute.
+            path = path.toAbsolutePath();
+            // Strip redundant navigation a/b/../c -> a/c
+            path = path.normalize();
+            // Prevent trailing slash (possibly Java bug), except when '/' or C:\
+            pathStr = path.toString();
+            if (pathStr.endsWith(path.getFileSystem().getSeparator()) && path.getNameCount()>0) {
+                path = Paths.get(pathStr.substring(0, pathStr.length()-1));
             }
-            // Strip redundant navigation a/b/../c -> a/c
-            return path.normalize();
+            return path;
         } catch (java.nio.file.InvalidPathException ex) {
             /*
              * Thrown on Windows for paths like foo/bar/<test>, where <test> is the literal text,

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list