Skip to content

Commit 9d048f2

Browse files
committed
Add posix.readlink
1 parent 976c249 commit 9d048f2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/module/PosixTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -34,6 +34,7 @@
3434
import java.io.IOException;
3535
import java.nio.file.Files;
3636
import java.nio.file.Path;
37+
import java.nio.file.Paths;
3738

3839
import org.junit.After;
3940
import org.junit.Before;
@@ -179,4 +180,16 @@ public void printToFile() throws IOException {
179180
"print('hello', file=f)");
180181
assertEquals("hello\n", new String(Files.readAllBytes(tmpfile)));
181182
}
183+
184+
@Test
185+
public void readlink() throws IOException {
186+
Path symlinkPath = tmpfile.getParent().resolve(tmpfile.getFileName() + "__symlink");
187+
try {
188+
Path symlink = Files.createSymbolicLink(symlinkPath, Paths.get(tmpfile.toUri()));
189+
assertPrints(tmpfile.toString() + "\n", "import posix\n" +
190+
"print(posix.readlink('" + symlink.toString() + "'))\n");
191+
} finally {
192+
Files.deleteIfExists(symlinkPath);
193+
}
194+
}
182195
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,4 +1752,23 @@ protected GetTerminalSizeNode create() {
17521752
return PosixModuleBuiltinsFactory.GetTerminalSizeNodeFactory.create();
17531753
}
17541754
}
1755+
1756+
@Builtin(name = "readlink", fixedNumOfPositionalArgs = 1, keywordArguments = {"dirFd"}, doc = "readlink(path, *, dir_fd=None) -> path\n" +
1757+
"\nReturn a string representing the path to which the symbolic link points.\n")
1758+
@GenerateNodeFactory
1759+
abstract static class ReadlinkNode extends PythonBinaryBuiltinNode {
1760+
@Specialization
1761+
String readlinkPString(PString str, PNone none) {
1762+
return readlink(str.getValue(), none);
1763+
}
1764+
1765+
@Specialization
1766+
String readlink(String str, @SuppressWarnings("unused") PNone none) {
1767+
try {
1768+
return getContext().getEnv().getTruffleFile(str).getCanonicalFile().getPath();
1769+
} catch (IOException e) {
1770+
throw raise(OSError, e.getMessage());
1771+
}
1772+
}
1773+
}
17551774
}

0 commit comments

Comments
 (0)