Skip to content

Commit 5e09b1e

Browse files
committed
Fixed handling of relative paths, symlinks and errors in emulated chdir()
1 parent cfd6959 commit 5e09b1e

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
@@ -357,9 +357,11 @@ class ChdirTests(unittest.TestCase):
357357

358358
def setUp(self):
359359
self.old_wd = os.getcwd()
360+
os.mkdir(TEST_FULL_PATH1)
360361

361362
def tearDown(self):
362363
os.chdir(self.old_wd)
364+
os.rmdir(TEST_FULL_PATH1)
363365

364366
def test_chdir(self):
365367
os.chdir(TEMP_DIR)
@@ -368,6 +370,27 @@ def test_chdir(self):
368370
os.chdir(os.fsencode(self.old_wd))
369371
self.assertEqual(self.old_wd, os.getcwd())
370372

373+
def test_chdir_relative(self):
374+
os.chdir(TEMP_DIR)
375+
tmp_dir = os.getcwd()
376+
os.chdir(TEST_FILENAME1)
377+
self.assertEqual(os.path.join(tmp_dir, TEST_FILENAME1), os.getcwd())
378+
379+
def test_chdir_relative_symlink(self):
380+
os.symlink(TEST_FULL_PATH1, TEST_FULL_PATH2, target_is_directory=True)
381+
try:
382+
os.chdir(TEMP_DIR)
383+
os.chdir(TEST_FILENAME2)
384+
finally:
385+
os.remove(TEST_FULL_PATH2)
386+
387+
def test_chdir_not_a_dir(self):
388+
os.close(os.open(TEST_FULL_PATH2, os.O_WRONLY | os.O_CREAT))
389+
try:
390+
self.assertRaises(NotADirectoryError, os.chdir, TEST_FULL_PATH2)
391+
finally:
392+
os.unlink(TEST_FULL_PATH2)
393+
371394
def test_chdir_fd(self):
372395
os.chdir(TEMP_DIR)
373396
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
@@ -960,7 +960,15 @@ public void fchdir(int fd,
960960
}
961961

962962
private void chdirStr(String pathStr, BranchProfile errorBranch) throws PosixException {
963-
TruffleFile truffleFile = getTruffleFile(pathStr);
963+
TruffleFile truffleFile = getTruffleFile(pathStr).getAbsoluteFile();
964+
if (!truffleFile.exists()) {
965+
errorBranch.enter();
966+
throw posixException(OSErrorEnum.ENOENT);
967+
}
968+
if (!truffleFile.isDirectory()) {
969+
errorBranch.enter();
970+
throw posixException(OSErrorEnum.ENOTDIR);
971+
}
964972
try {
965973
context.getEnv().setCurrentWorkingDirectory(truffleFile);
966974
} catch (IllegalArgumentException ignored) {

0 commit comments

Comments
 (0)