[Jython-checkins] jython: Corrections to chdir for special cases. Fixes #2307.

jeff.allen jython-checkins at python.org
Fri Apr 10 17:35:58 CEST 2015


https://hg.python.org/jython/rev/60972a7a26c5
changeset:   7654:60972a7a26c5
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Fri Apr 10 10:36:38 2015 +0100
summary:
  Corrections to chdir for special cases. Fixes #2307.

In PosixModule.absolutePath() and .chdir() use Path.normalize instead
of toRealPath (to preserve DOS names); use Path.resolve() to deal with
path '\'.

files:
  src/org/python/modules/posix/PosixModule.java |  29 +++++----
  1 files changed, 15 insertions(+), 14 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
@@ -289,11 +289,7 @@
         if (!basicstat(path, absolutePath).isDirectory()) {
             throw Py.OSError(Errno.ENOTDIR, path);
         }
-        try {
-            Py.getSystemState().setCurrentWorkingDir(absolutePath.toRealPath().toString());
-        } catch (IOException ioe) {
-            throw Py.OSError(ioe);
-        }
+        Py.getSystemState().setCurrentWorkingDir(absolutePath.toString());
     }
 
     public static PyString __doc__chmod = new PyString(
@@ -1122,7 +1118,8 @@
     }
 
     /**
-     * Return the absolute form of path.
+     * Return the absolute, normalised form of path, equivalent to Python os.path.abspath(), except
+     * that it is an error for pathObj to be an empty string or unacceptable in the file system.
      *
      * @param pathObj a PyObject, raising a TypeError if an invalid path type
      * @return an absolute path String
@@ -1130,20 +1127,24 @@
     private static Path absolutePath(PyObject pathObj) {
         String pathStr = asPath(pathObj);
         if (pathStr.equals("")) {
-            // Otherwise this path will get prefixed with the current working directory,
-            // which is both wrong and very surprising!
+            // Returning current working directory would be wrong in our context (chdir, etc.).
             throw Py.OSError(Errno.ENOENT, pathObj);
         }
         try {
-            Path path = FileSystems.getDefault().getPath(pathStr);
+            Path path = Paths.get(pathStr);
             if (!path.isAbsolute()) {
-                path = FileSystems.getDefault().getPath(Py.getSystemState().getCurrentWorkingDir(), pathStr);
+                // Relative path: augment from current working directory.
+                path = Paths.get(Py.getSystemState().getCurrentWorkingDir()).resolve(path);
             }
-            return path.toAbsolutePath();
+            // Strip redundant navigation a/b/../c -> a/c
+            return path.normalize();
         } catch (java.nio.file.InvalidPathException ex) {
-            // Thrown on Windows for paths like foo/bar/<test>, where <test> is the literal text, not a metavariable :)
-            // NOTE: In this case on CPython, Windows throws the Windows-specific internal error WindowsError [Error 123],
-            // but it seems excessive to duplicate this error hierarchy
+            /*
+             * Thrown on Windows for paths like foo/bar/<test>, where <test> is the literal text,
+             * not a metavariable :) NOTE: CPython, Windows throws the Windows-specific internal
+             * error WindowsError [Error 123], but it seems excessive to duplicate this error
+             * hierarchy.
+             */
             throw Py.OSError(Errno.EINVAL, pathObj);
         }
     }

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


More information about the Jython-checkins mailing list