Skip to content

Commit ae2529b

Browse files
committed
Remove deprecated calls of 'Env.getTruffleFile'.
1 parent 35f66f2 commit ae2529b

15 files changed

+96
-39
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonFileDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class PythonFileDetector implements TruffleFile.FileTypeDetector {
4949
@Override
5050
public String findMimeType(TruffleFile file) throws IOException {
5151
String fileName = file.getName();
52-
if (fileName != null && fileName.endsWith(".py")) {
52+
if (fileName != null && fileName.endsWith(PythonLanguage.EXTENSION)) {
5353
return PythonLanguage.MIME_TYPE;
5454
}
5555
return null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
112112

113113
public static final String MIME_TYPE = "text/x-python";
114114
public static final String EXTENSION = ".py";
115+
public static final String[] DEFAULT_PYTHON_EXTENSIONS = new String[]{EXTENSION, ".pyc"};
115116

116117
public final Assumption singleContextAssumption = Truffle.getRuntime().createAssumption("Only a single context is active");
117118

@@ -461,7 +462,7 @@ public static Source newSource(PythonContext ctxt, String src, String name, bool
461462
SourceBuilder sourceBuilder = null;
462463
if (mayBeFile) {
463464
try {
464-
TruffleFile truffleFile = ctxt.getEnv().getTruffleFile(name);
465+
TruffleFile truffleFile = ctxt.getEnv().getInternalTruffleFile(name);
465466
if (truffleFile.exists()) {
466467
// XXX: (tfel): We don't know if the expression has anything to do with the
467468
// filename that's given. We would really have to compare the entire
@@ -551,4 +552,5 @@ protected void initializeMultiThreading(PythonContext context) {
551552
protected void initializeThread(PythonContext context, Thread thread) {
552553
super.initializeThread(context, thread);
553554
}
555+
554556
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,11 @@ private void addBuiltinsTo(PythonObject obj, PythonBuiltins builtinsForObj) {
598598
}
599599

600600
@TruffleBoundary
601-
private Source getSource(String basename, String prefix) {
601+
private Source getInternalSource(String basename, String prefix) {
602602
PythonContext ctxt = getContext();
603603
Env env = ctxt.getEnv();
604-
String suffix = env.getFileNameSeparator() + basename + ".py";
605-
TruffleFile file = env.getTruffleFile(prefix + suffix);
604+
String suffix = env.getFileNameSeparator() + basename + PythonLanguage.EXTENSION;
605+
TruffleFile file = env.getInternalTruffleFile(prefix + suffix);
606606
String errorMessage;
607607
try {
608608
return PythonLanguage.newSource(ctxt, file, basename);
@@ -619,7 +619,7 @@ private Source getSource(String basename, String prefix) {
619619

620620
private void loadFile(String s, String prefix) {
621621
Supplier<CallTarget> getCode = () -> {
622-
Source source = getSource(s, prefix);
622+
Source source = getInternalSource(s, prefix);
623623
return Truffle.getRuntime().createCallTarget((RootNode) getParser().parse(ParserMode.File, this, source, null));
624624
};
625625
RootCallTarget callTarget = (RootCallTarget) getLanguage().cacheCode(s, getCode);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
200200
String basename = name.substring(name.lastIndexOf('.') + 1);
201201
TruffleObject sulongLibrary;
202202
try {
203-
CallTarget callTarget = env.parseInternal(Source.newBuilder(LLVM_LANGUAGE, env.getTruffleFile(path)).build());
203+
CallTarget callTarget = env.parseInternal(Source.newBuilder(LLVM_LANGUAGE, getContext().getPublicTruffleFileRelaxed(path, ".bc")).build());
204204
sulongLibrary = (TruffleObject) callTarget.call();
205205
} catch (SecurityException | IOException e) {
206206
throw raise(ImportError, "cannot load %s: %m", path, e);
@@ -243,7 +243,7 @@ private void ensureCapiWasLoaded() {
243243
if (!ctxt.capiWasLoaded()) {
244244
Env env = ctxt.getEnv();
245245
CompilerDirectives.transferToInterpreterAndInvalidate();
246-
TruffleFile capiFile = env.getTruffleFile(getContext().getCoreHome() + env.getFileNameSeparator() + "capi.bc");
246+
TruffleFile capiFile = env.getInternalTruffleFile(getContext().getCoreHome() + env.getFileNameSeparator() + "capi.bc");
247247
Object capi = null;
248248
try {
249249
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ PNone add(VirtualFrame frame, Object[] args,
124124
for (Object arg : args) {
125125
String entry = castToString.execute(frame, arg);
126126
try {
127-
env.addToHostClassPath(env.getTruffleFile(entry));
127+
// Always allow accessing JAR files in the language home; folders are allowed
128+
// implicitly
129+
env.addToHostClassPath(getContext().getPublicTruffleFileRelaxed(entry, ".jar"));
128130
} catch (SecurityException e) {
129131
throw raise(TypeError, "invalid or unreadable classpath: '%s' - %m", entry, e);
130132
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ PMMap doFile(LazyPythonClass clazz, int fd, int length, @SuppressWarnings("unuse
115115
checkLength(length);
116116

117117
String path = getContext().getResources().getFilePath(fd);
118-
TruffleFile truffleFile = getContext().getEnv().getTruffleFile(path);
118+
TruffleFile truffleFile = getContext().getEnv().getPublicTruffleFile(path);
119119

120120
// TODO(fa) correctly honor access flags
121121
Set<StandardOpenOption> options = set(StandardOpenOption.READ, StandardOpenOption.WRITE);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void initialize(PythonCore core) {
103103
Env env = context.getEnv();
104104
String coreHome = context.getCoreHome();
105105
try {
106-
TruffleFile coreDir = env.getTruffleFile(coreHome);
106+
TruffleFile coreDir = env.getInternalTruffleFile(coreHome);
107107
TruffleFile docDir = coreDir.resolveSibling("doc");
108108
if (docDir.exists() || docDir.getParent() != null && (docDir = coreDir.getParent().resolveSibling("doc")).exists()) {
109109
builtinConstants.put(SpecialAttributeNames.__DOC__, new String(docDir.resolve("INTEROP.md").readAllBytes()));
@@ -172,7 +172,7 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
172172
boolean mimeType = isMimeType(langOrMimeType);
173173
String lang = mimeType ? findLanguageByMimeType(env, langOrMimeType) : langOrMimeType;
174174
raiseIfInternal(env, lang);
175-
SourceBuilder newBuilder = Source.newBuilder(lang, env.getTruffleFile(path));
175+
SourceBuilder newBuilder = Source.newBuilder(lang, env.getPublicTruffleFile(path));
176176
if (mimeType) {
177177
newBuilder = newBuilder.mimeType(langOrMimeType);
178178
}
@@ -192,7 +192,7 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, @Suppress
192192
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
193193
}
194194
try {
195-
return getContext().getEnv().parsePublic(Source.newBuilder(PythonLanguage.ID, env.getTruffleFile(path)).name(path).build()).call();
195+
return getContext().getEnv().parsePublic(Source.newBuilder(PythonLanguage.ID, env.getPublicTruffleFile(path)).name(path).build()).call();
196196
} catch (IOException e) {
197197
throw raise(OSError, "%s", e);
198198
} catch (RuntimeException e) {

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public abstract static class ChdirNode extends PythonBuiltinNode {
385385
PNone chdir(String spath) {
386386
Env env = getContext().getEnv();
387387
try {
388-
TruffleFile dir = env.getTruffleFile(spath).getAbsoluteFile();
388+
TruffleFile dir = env.getPublicTruffleFile(spath).getAbsoluteFile();
389389
env.setCurrentWorkingDirectory(dir);
390390
return PNone.NONE;
391391
} catch (UnsupportedOperationException | IllegalArgumentException | SecurityException e) {
@@ -512,7 +512,7 @@ Object setInheritableStd(@SuppressWarnings("unused") int fd, @SuppressWarnings("
512512
@Specialization(guards = "fd > 2")
513513
Object setInheritable(int fd, @SuppressWarnings("unused") Object inheritable) {
514514
String path = getResources().getFilePath(fd);
515-
TruffleFile f = getContext().getEnv().getTruffleFile(path);
515+
TruffleFile f = getContext().getEnv().getPublicTruffleFile(path);
516516
if (!f.exists()) {
517517
throw raise(OSError, "No such file or directory: '%s'", path);
518518
}
@@ -566,7 +566,7 @@ long fileTimeToSeconds(FileTime t) {
566566

567567
@TruffleBoundary
568568
Object stat(String path, boolean followSymlinks) {
569-
TruffleFile f = getContext().getEnv().getTruffleFile(path);
569+
TruffleFile f = getContext().getPublicTruffleFileRelaxed(path, PythonLanguage.DEFAULT_PYTHON_EXTENSIONS);
570570
LinkOption[] linkOptions = followSymlinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
571571
try {
572572
return unixStat(f, linkOptions);
@@ -818,7 +818,7 @@ public abstract static class ListdirNode extends PythonBuiltinNode {
818818
Object listdir(VirtualFrame frame, String path,
819819
@Cached PRaiseOSErrorNode raiseOS) {
820820
try {
821-
TruffleFile file = getContext().getEnv().getTruffleFile(path);
821+
TruffleFile file = getContext().getPublicTruffleFileRelaxed(path, PythonLanguage.DEFAULT_PYTHON_EXTENSIONS);
822822
Collection<TruffleFile> listFiles = file.list();
823823
Object[] filenames = listToArray(listFiles);
824824
return factory().createList(filenames);
@@ -852,7 +852,7 @@ public abstract static class ScandirIterNode extends PythonBinaryBuiltinNode {
852852
@Specialization
853853
Object doit(LazyPythonClass cls, String path) {
854854
try {
855-
TruffleFile file = getContext().getEnv().getTruffleFile(path);
855+
TruffleFile file = getContext().getEnv().getPublicTruffleFile(path);
856856
return factory().createScandirIterator(cls, path, file.newDirectoryStream());
857857
} catch (SecurityException | IOException e) {
858858
gotException.enter();
@@ -870,7 +870,7 @@ public abstract static class DirEntryNode extends PythonTernaryBuiltinNode {
870870
@Specialization
871871
Object doit(LazyPythonClass cls, String name, String path) {
872872
try {
873-
TruffleFile dir = getContext().getEnv().getTruffleFile(path);
873+
TruffleFile dir = getContext().getEnv().getPublicTruffleFile(path);
874874
TruffleFile file = dir.resolve(name);
875875
return factory().createDirEntry(cls, name, file);
876876
} catch (SecurityException | InvalidPathException e) {
@@ -921,7 +921,7 @@ Object open(VirtualFrame frame, String pathname, int flags, @SuppressWarnings("u
921921
Object open(VirtualFrame frame, String pathname, int flags, int fileMode, @SuppressWarnings("unused") PNone dir_fd) {
922922
Set<StandardOpenOption> options = flagsToOptions(flags);
923923
FileAttribute<Set<PosixFilePermission>>[] attributes = modeToAttributes(fileMode);
924-
TruffleFile truffleFile = getContext().getEnv().getTruffleFile(pathname);
924+
TruffleFile truffleFile = getContext().getPublicTruffleFileRelaxed(pathname, PythonLanguage.DEFAULT_PYTHON_EXTENSIONS);
925925
try {
926926
SeekableByteChannel fc = truffleFile.newByteChannel(options, attributes);
927927
return getResources().open(truffleFile, fc);
@@ -1098,7 +1098,7 @@ public abstract static class UnlinkNode extends PythonFileNode {
10981098
@Specialization
10991099
Object unlink(String path) {
11001100
try {
1101-
getContext().getEnv().getTruffleFile(path).delete();
1101+
getContext().getEnv().getPublicTruffleFile(path).delete();
11021102
} catch (RuntimeException | IOException e) {
11031103
gotException.enter();
11041104
throw raise(OSError, e);
@@ -1112,7 +1112,7 @@ Object unlink(VirtualFrame frame, Object pathLike,
11121112
@Cached CastToStringNode castToStringNode) {
11131113
try {
11141114
Object fsPathObj = callFspathNode.executeObject(frame, pathLike);
1115-
getContext().getEnv().getTruffleFile(castToStringNode.execute(frame, fsPathObj)).delete();
1115+
getContext().getEnv().getPublicTruffleFile(castToStringNode.execute(frame, fsPathObj)).delete();
11161116
} catch (RuntimeException | IOException e) {
11171117
gotException.enter();
11181118
throw raise(OSError, e);
@@ -1149,7 +1149,7 @@ Object mkdir(VirtualFrame frame, String path, @SuppressWarnings("unused") PNone
11491149
@Specialization
11501150
Object mkdir(VirtualFrame frame, String path, @SuppressWarnings("unused") int mode, @SuppressWarnings("unused") PNone dirFd) {
11511151
try {
1152-
getContext().getEnv().getTruffleFile(path).createDirectory();
1152+
getContext().getEnv().getPublicTruffleFile(path).createDirectory();
11531153
} catch (FileAlreadyExistsException e) {
11541154
throw raiseOSError(frame, OSErrorEnum.EEXIST, path);
11551155
} catch (RuntimeException | IOException e) {
@@ -1304,7 +1304,7 @@ Object chmod(String path, long mode, @SuppressWarnings("unused") PNone dir_fd, @
13041304
Object chmod(String path, long mode, @SuppressWarnings("unused") PNone dir_fd, boolean follow_symlinks) {
13051305
Set<PosixFilePermission> permissions = modeToPermissions(mode);
13061306
try {
1307-
TruffleFile truffleFile = getContext().getEnv().getTruffleFile(path);
1307+
TruffleFile truffleFile = getContext().getEnv().getPublicTruffleFile(path);
13081308
if (!follow_symlinks) {
13091309
truffleFile = truffleFile.getCanonicalFile(LinkOption.NOFOLLOW_LINKS);
13101310
} else {
@@ -1447,7 +1447,7 @@ private void setAtime(TruffleFile truffleFile, long mtime) {
14471447
}
14481448

14491449
private TruffleFile getFile(String path, boolean followSymlinks) {
1450-
TruffleFile truffleFile = getContext().getEnv().getTruffleFile(path);
1450+
TruffleFile truffleFile = getContext().getEnv().getPublicTruffleFile(path);
14511451
if (!followSymlinks) {
14521452
try {
14531453
truffleFile = truffleFile.getCanonicalFile(LinkOption.NOFOLLOW_LINKS);
@@ -1687,11 +1687,11 @@ Object rename(VirtualFrame frame, Object src, Object dst, @SuppressWarnings("unu
16871687

16881688
private Object rename(String src, String dst) {
16891689
try {
1690-
TruffleFile dstFile = getContext().getEnv().getTruffleFile(dst);
1690+
TruffleFile dstFile = getContext().getEnv().getPublicTruffleFile(dst);
16911691
if (dstFile.isDirectory()) {
16921692
throw raise(OSError, "%s is a directory", dst);
16931693
}
1694-
TruffleFile file = getContext().getEnv().getTruffleFile(src);
1694+
TruffleFile file = getContext().getEnv().getPublicTruffleFile(src);
16951695
file.move(dstFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
16961696
return PNone.NONE;
16971697
} catch (IOException e) {
@@ -1780,7 +1780,7 @@ boolean access(String path, int mode, Object dirFd, boolean effectiveIds, boolea
17801780
notImplementedBranch.enter();
17811781
throw raise(NotImplementedError);
17821782
}
1783-
TruffleFile f = getContext().getEnv().getTruffleFile(path);
1783+
TruffleFile f = getContext().getEnv().getPublicTruffleFile(path);
17841784
LinkOption[] linkOptions = followSymlinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
17851785
if (!f.exists(linkOptions)) {
17861786
return false;
@@ -1933,7 +1933,7 @@ String readlinkPString(PString str, PNone none) {
19331933
@Specialization
19341934
String readlink(String str, @SuppressWarnings("unused") PNone none) {
19351935
try {
1936-
return getContext().getEnv().getTruffleFile(str).getCanonicalFile().getPath();
1936+
return getContext().getEnv().getPublicTruffleFile(str).getCanonicalFile().getPath();
19371937
} catch (IOException e) {
19381938
throw raise(OSError, e);
19391939
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
166166
}
167167

168168
try {
169-
if (getContext().getEnv().getTruffleFile(cwd).exists()) {
169+
if (getContext().getEnv().getPublicTruffleFile(cwd).exists()) {
170170
pb.directory(new File(cwd));
171171
} else {
172172
throw raise(PythonBuiltinClassType.OSError, "working directory %s is not accessible", cwd);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ PNone setCompleter(PythonModule self, String path,
246246
@Cached("create()") ReadAttributeFromObjectNode readNode) {
247247
LocalData data = (LocalData) readNode.execute(self, DATA);
248248
try {
249-
BufferedReader reader = getContext().getEnv().getTruffleFile(path).newBufferedReader();
249+
BufferedReader reader = getContext().getEnv().getPublicTruffleFile(path).newBufferedReader();
250250
String line;
251251
while ((line = reader.readLine()) != null) {
252252
data.history.add(line);
@@ -274,7 +274,7 @@ PNone setCompleter(PythonModule self, String path,
274274
@Cached("create()") ReadAttributeFromObjectNode readNode) {
275275
LocalData data = (LocalData) readNode.execute(self, DATA);
276276
try {
277-
BufferedWriter writer = getContext().getEnv().getTruffleFile(path).newBufferedWriter(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
277+
BufferedWriter writer = getContext().getEnv().getPublicTruffleFile(path).newBufferedWriter(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
278278
for (String l : data.history) {
279279
writer.write(l);
280280
writer.newLine();

0 commit comments

Comments
 (0)