Skip to content

Commit 7417192

Browse files
committed
[GR-29124] Fixed handling of relative paths, symlinks and errors in emulated chdir()
PullRequest: graalpython/1586
2 parents 8042c33 + 5e09b1e commit 7417192

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_posix.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,11 @@ class ChdirTests(unittest.TestCase):
388388

389389
def setUp(self):
390390
self.old_wd = os.getcwd()
391+
os.mkdir(TEST_FULL_PATH1)
391392

392393
def tearDown(self):
393394
os.chdir(self.old_wd)
395+
os.rmdir(TEST_FULL_PATH1)
394396

395397
def test_chdir(self):
396398
os.chdir(TEMP_DIR)
@@ -399,6 +401,27 @@ def test_chdir(self):
399401
os.chdir(os.fsencode(self.old_wd))
400402
self.assertEqual(self.old_wd, os.getcwd())
401403

404+
def test_chdir_relative(self):
405+
os.chdir(TEMP_DIR)
406+
tmp_dir = os.getcwd()
407+
os.chdir(TEST_FILENAME1)
408+
self.assertEqual(os.path.join(tmp_dir, TEST_FILENAME1), os.getcwd())
409+
410+
def test_chdir_relative_symlink(self):
411+
os.symlink(TEST_FULL_PATH1, TEST_FULL_PATH2, target_is_directory=True)
412+
try:
413+
os.chdir(TEMP_DIR)
414+
os.chdir(TEST_FILENAME2)
415+
finally:
416+
os.remove(TEST_FULL_PATH2)
417+
418+
def test_chdir_not_a_dir(self):
419+
os.close(os.open(TEST_FULL_PATH2, os.O_WRONLY | os.O_CREAT))
420+
try:
421+
self.assertRaises(NotADirectoryError, os.chdir, TEST_FULL_PATH2)
422+
finally:
423+
os.unlink(TEST_FULL_PATH2)
424+
402425
def test_chdir_fd(self):
403426
os.chdir(TEMP_DIR)
404427
with open(self.old_wd, 0) as fd:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,15 @@ public void fchdir(int fd,
10131013
}
10141014

10151015
private void chdirStr(String pathStr, BranchProfile errorBranch) throws PosixException {
1016-
TruffleFile truffleFile = getTruffleFile(pathStr);
1016+
TruffleFile truffleFile = getTruffleFile(pathStr).getAbsoluteFile();
1017+
if (!truffleFile.exists()) {
1018+
errorBranch.enter();
1019+
throw posixException(OSErrorEnum.ENOENT);
1020+
}
1021+
if (!truffleFile.isDirectory()) {
1022+
errorBranch.enter();
1023+
throw posixException(OSErrorEnum.ENOTDIR);
1024+
}
10171025
try {
10181026
context.getEnv().setCurrentWorkingDirectory(truffleFile);
10191027
} catch (IllegalArgumentException ignored) {

0 commit comments

Comments
 (0)