Skip to content

Commit b2b8ca7

Browse files
committed
[GR-14416] Get rid of remaining users of Java path and file separators
PullRequest: graalpython/555
2 parents 4eb077e + f2dab55 commit b2b8ca7

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public abstract static class ZipImporterNode extends PythonBinaryBuiltinNode {
9595
public PZipImporter createNew(LazyPythonClass cls, @SuppressWarnings("unused") Object path,
9696
@Cached("create()") ReadAttributeFromObjectNode readNode) {
9797
PythonModule module = getCore().lookupBuiltinModule(ZIPIMPORT_MODULE_NAME);
98-
return factory().createZipImporter(cls, (PDict) readNode.execute(module, ZIP_DIRECTORY_CACHE_NAME));
98+
return factory().createZipImporter(cls, (PDict) readNode.execute(module, ZIP_DIRECTORY_CACHE_NAME), getContext().getEnv().getFileNameSeparator());
9999
}
100100

101101
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/PZipImporter.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.oracle.graal.python.builtins.objects.zipimporter;
2727

2828
import java.io.BufferedReader;
29-
import java.io.File;
3029
import java.io.IOException;
3130
import java.io.InputStream;
3231
import java.io.InputStreamReader;
@@ -42,9 +41,6 @@
4241
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4342

4443
public class PZipImporter extends PythonBuiltinObject {
45-
46-
public static final String SEPARATOR = File.separator;
47-
4844
/**
4945
* pathname of the Zip archive
5046
*/
@@ -73,6 +69,11 @@ private static enum EntryType {
7369
IS_PACKAGE
7470
}
7571

72+
/**
73+
* The separatorChar used in the context for this importer
74+
*/
75+
private final String separator;
76+
7677
private static class SearchOrderEntry {
7778

7879
String suffix;
@@ -100,7 +101,7 @@ protected static class ModuleCodeData {
100101
/**
101102
* Defines how the source and module will be searched in archive.
102103
*/
103-
private static SearchOrderEntry[] searchOrder;
104+
private final SearchOrderEntry[] searchOrder;
104105

105106
/**
106107
* Module information
@@ -112,25 +113,28 @@ public static enum ModuleInfo {
112113
PACKAGE
113114
}
114115

115-
public PZipImporter(LazyPythonClass cls, PDict zipDirectoryCache) {
116+
public PZipImporter(LazyPythonClass cls, PDict zipDirectoryCache, String separator) {
116117
super(cls);
117118
this.archive = null;
118119
this.prefix = null;
120+
this.separator = separator;
119121
this.moduleZipDirectoryCache = zipDirectoryCache;
120-
if (searchOrder == null) { // define the order once
121-
searchOrder = defineSearchOrder();
122-
}
122+
this.searchOrder = defineSearchOrder();
123123
}
124124

125-
@CompilerDirectives.TruffleBoundary
126-
private static SearchOrderEntry[] defineSearchOrder() {
125+
private SearchOrderEntry[] defineSearchOrder() {
127126
return new SearchOrderEntry[]{
128-
new SearchOrderEntry(SEPARATOR + "__init__.py",
127+
new SearchOrderEntry(joinStrings(separator, "__init__.py"),
129128
EnumSet.of(EntryType.IS_PACKAGE, EntryType.IS_SOURCE)),
130129
new SearchOrderEntry(".py", EnumSet.of(EntryType.IS_SOURCE))
131130
};
132131
}
133132

133+
@TruffleBoundary
134+
private static String joinStrings(String a, String b) {
135+
return a + b;
136+
}
137+
134138
public PDict getZipDirectoryCache() {
135139
return moduleZipDirectoryCache;
136140
}
@@ -181,7 +185,7 @@ protected String getSubname(String fullname) {
181185

182186
@TruffleBoundary
183187
protected String makeFilename(String fullname) {
184-
return prefix + getSubname(fullname).replace('.', File.separatorChar);
188+
return prefix + getSubname(fullname).replace(".", separator);
185189
}
186190

187191
protected PTuple getEntry(String filenameAndSuffix) {
@@ -190,7 +194,7 @@ protected PTuple getEntry(String filenameAndSuffix) {
190194

191195
@TruffleBoundary
192196
protected String makePackagePath(String fullname) {
193-
return archive + SEPARATOR + prefix + getSubname(fullname);
197+
return archive + separator + prefix + getSubname(fullname);
194198
}
195199

196200
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
3030
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
3131

32-
import java.io.File;
3332
import java.io.IOException;
3433
import java.io.InputStream;
3534
import java.nio.file.StandardOpenOption;
@@ -221,7 +220,7 @@ private void initZipImporter(PZipImporter self, String path) {
221220
if (parentFile == null) {
222221
break;
223222
}
224-
prefix = tfile.getName() + PZipImporter.SEPARATOR + prefix;
223+
prefix = tfile.getName() + getContext().getEnv().getFileNameSeparator() + prefix;
225224
tfile = parentFile;
226225
}
227226

@@ -270,7 +269,7 @@ private void initZipImporter(PZipImporter self, String path) {
270269
}
271270

272271
PTuple tuple = factory().createTuple(new Object[]{
273-
tfile.getPath() + PZipImporter.SEPARATOR + entry.getName(),
272+
tfile.getPath() + getContext().getEnv().getFileNameSeparator() + entry.getName(),
274273
// for our implementation currently we don't need these
275274
// these properties to store there. Keeping them for
276275
// compatibility.
@@ -388,7 +387,7 @@ public String doit(PZipImporter self) {
388387
sb.append("???");
389388
} else if (prefix != null && !prefix.isEmpty()) {
390389
sb.append(archive);
391-
sb.append(File.pathSeparator);
390+
sb.append(getContext().getEnv().getPathSeparator());
392391
sb.append(prefix);
393392
} else {
394393
sb.append(archive);
@@ -478,7 +477,7 @@ public PBytes doit(PZipImporter self, String pathname) {
478477
String archive = self.getArchive();
479478
int len = archive.length();
480479
int index = 0;
481-
if (pathname.startsWith(archive) && pathname.charAt(len) == File.separatorChar) {
480+
if (pathname.startsWith(archive) && pathname.substring(len).startsWith(getContext().getEnv().getFileNameSeparator())) {
482481
index = len + 1;
483482
}
484483
String key = pathname.substring(index, pathname.length());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,8 @@ public PCode createCode(LazyPythonClass cls, RootCallTarget callTarget, Signatur
764764
filename, name, firstlineno, lnotab));
765765
}
766766

767-
public PZipImporter createZipImporter(LazyPythonClass cls, PDict zipDirectoryCache) {
768-
return trace(new PZipImporter(cls, zipDirectoryCache));
767+
public PZipImporter createZipImporter(LazyPythonClass cls, PDict zipDirectoryCache, String separator) {
768+
return trace(new PZipImporter(cls, zipDirectoryCache, separator));
769769
}
770770

771771
/*

0 commit comments

Comments
 (0)