Skip to content

Commit ed29cdc

Browse files
committed
Fix regression in chdir when switching drives.
Fix WindowsChdirTestCase.test_windows_getcwd_ensures_drive_letter in test.test_chdir, completing #2307. (Previously-failing tests pass on both Windows and Linux.)
1 parent 6056fba commit ed29cdc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/org/python/modules/posix/PosixModule.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,12 +1144,18 @@ private static Path absolutePath(PyObject pathObj) {
11441144
}
11451145
try {
11461146
Path path = Paths.get(pathStr);
1147-
if (!path.isAbsolute()) {
1148-
// Relative path: augment from current working directory.
1149-
path = Paths.get(Py.getSystemState().getCurrentWorkingDir()).resolve(path);
1150-
}
1147+
// Relative path: augment from current working directory.
1148+
path = Paths.get(Py.getSystemState().getCurrentWorkingDir()).resolve(path);
1149+
// In case of a root different from cwd, resolve does not guarantee absolute.
1150+
path = path.toAbsolutePath();
11511151
// Strip redundant navigation a/b/../c -> a/c
1152-
return path.normalize();
1152+
path = path.normalize();
1153+
// Prevent trailing slash (possibly Java bug), except when '/' or C:\
1154+
pathStr = path.toString();
1155+
if (pathStr.endsWith(path.getFileSystem().getSeparator()) && path.getNameCount()>0) {
1156+
path = Paths.get(pathStr.substring(0, pathStr.length()-1));
1157+
}
1158+
return path;
11531159
} catch (java.nio.file.InvalidPathException ex) {
11541160
/*
11551161
* Thrown on Windows for paths like foo/bar/<test>, where <test> is the literal text,

0 commit comments

Comments
 (0)