Skip to content

Commit a47def2

Browse files
committed
[GR-54951] Fix calculateProgramFullPath on Windows if program has no extension
1 parent e29d806 commit a47def2

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666

6767
public class GraalPythonMain extends AbstractLanguageLauncher {
6868

69+
private static final boolean IS_WINDOWS = System.getProperty("os.name") != null && System.getProperty("os.name").toLowerCase().contains("windows");
70+
6971
private static final String SHORT_HELP = "usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...\n" +
7072
"Try `python -h' for more information.";
7173

@@ -552,6 +554,25 @@ private String calculateProgramFullPath(String program, Function<Path, Boolean>
552554
return resolvedProgramName.toString();
553555
}
554556

557+
// On windows, the program name may be without the extension
558+
if (IS_WINDOWS) {
559+
String pathExtEnvvar = getEnv("PATHEXT");
560+
if (pathExtEnvvar != null) {
561+
// default extensions are defined
562+
String resolvedStr = resolvedProgramName.toString();
563+
if (resolvedStr.length() <= 3 || resolvedStr.charAt(resolvedStr.length() - 4) != '.') {
564+
// program has no file extension
565+
String[] pathExts = pathExtEnvvar.toLowerCase().split(";");
566+
for (String pathExt : pathExts) {
567+
resolvedProgramName = Path.of(resolvedStr + pathExt);
568+
if (isExecutable.apply(resolvedProgramName)) {
569+
return resolvedProgramName.toString();
570+
}
571+
}
572+
}
573+
}
574+
}
575+
555576
// next start is the char after the separator because we have "path0:path1" and
556577
// 'i' points to ':'
557578
previous = i + 1;
@@ -768,8 +789,7 @@ protected void launch(Builder contextBuilder) {
768789
contextBuilder.option("python.PyCachePrefix", cachePrefix);
769790
}
770791

771-
String osName = System.getProperty("os.name");
772-
if (osName != null && osName.toLowerCase().contains("windows")) {
792+
if (IS_WINDOWS) {
773793
contextBuilder.option("python.PosixModuleBackend", "java");
774794
}
775795

0 commit comments

Comments
 (0)