Skip to content

Commit 17147e2

Browse files
committed
[GR-44824] Support sys.argv in standalone module, native mode
PullRequest: graalpython/2901
2 parents c0112a1 + 0d6fc49 commit 17147e2

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_standalone.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def get_gp():
7070
print(
7171
"Standalone module tests require a GraalVM installation including graalpy, java, javac and native-image",
7272
"Please point the JAVA_HOME environment variable to such a GraalVM root.",
73-
"__graalpython__.home : " + java_home,
74-
"native-image exists: " + str(os.path.exists(ni)),
73+
"__graalpython__.home : " + java_home,
74+
"native-image exists: " + str(os.path.exists(ni)),
7575
"javac exists: " + str(os.path.exists(jc)),
7676
"graalpy exits: " + str(os.path.exists(graalpy)),
7777
"java exists: " + str(os.path.exists(java)),
@@ -114,7 +114,7 @@ def test_polyglot_app():
114114

115115
java_home, graalpy, java = get_gp()
116116
env = get_env(java_home)
117-
117+
118118
with tempfile.TemporaryDirectory() as tmpdir:
119119

120120
target_dir = os.path.join(tmpdir, "polyglot_app_test")
@@ -166,7 +166,8 @@ def test_native_executable_one_file():
166166

167167
source_file = os.path.join(tmpdir, "hello.py")
168168
with open(source_file, 'w') as f:
169-
f.write("print('hello world')")
169+
f.write("import sys\n")
170+
f.write("print('hello world, argv[1:]:', sys.argv[1:])")
170171

171172
target_file = os.path.join(tmpdir, "hello")
172173
cmd = [graalpy, "-m", "standalone", "--verbose", "native", "-m", source_file, "-o", target_file]
@@ -177,12 +178,12 @@ def test_native_executable_one_file():
177178
print(p.stderr.decode(errors='backslashreplace'))
178179
assert "Bundling Python resources into" in out
179180

180-
cmd = [target_file]
181-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
181+
cmd = [target_file, "arg1", "arg2"]
182+
p = subprocess.run(" ".join(cmd), env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
182183
out = p.stdout.decode(errors='backslashreplace')
183184
print(out)
184185
print(p.stderr.decode(errors='backslashreplace'))
185-
assert "hello world" in out
186+
assert "hello world, argv[1:]: " + str(cmd[1:]) in out
186187

187188
@unittest.skipUnless(is_enabled, "ENABLE_STANDALONE_UNITTESTS is not true")
188189
def test_native_executable_one_file_venv():

graalpython/lib-graalpython/modules/standalone/templates/Py2BinLauncher.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@
5858
import java.util.Set;
5959
import java.util.SortedMap;
6060
import java.util.TreeMap;
61+
import java.util.stream.Stream;
6162
import java.util.zip.ZipEntry;
6263
import java.util.zip.ZipInputStream;
6364

6465
import org.graalvm.nativeimage.ImageInfo;
66+
import org.graalvm.nativeimage.ProcessProperties;
6567
import org.graalvm.polyglot.Context;
6668
import org.graalvm.polyglot.PolyglotException;
6769
import org.graalvm.polyglot.Source;
@@ -90,6 +92,7 @@ public static void main(String[] args) throws IOException {
9092
.allowAllAccess(true)
9193
.allowIO(true)
9294
.fileSystem(vfs)
95+
.arguments("python", Stream.concat(Stream.of(getProgramName()), Stream.of(args)).toArray(String[]::new))
9396
.option("python.PosixModuleBackend", "java")
9497
.option("python.NativeModules", "false")
9598
.option("python.DontWriteBytecodeFlag", "true")
@@ -100,7 +103,7 @@ public static void main(String[] args) throws IOException {
100103
.option("python.ForceImportSite", "true")
101104
.option("python.RunViaLauncher", "true")
102105
.option("python.Executable", vfs.resourcePathToPlatformPath(VENV_PREFIX) + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.cmd" : "/bin/python"))
103-
.option("python.InputFilePath", vfs.resourcePathToPlatformPath(PROJ_PREFIX))
106+
.option("python.InputFilePath", vfs.resourcePathToPlatformPath(PROJ_PREFIX))
104107
.option("python.PythonHome", vfs.resourcePathToPlatformPath(HOME_PREFIX))
105108
.option("python.CheckHashPycsMode", "never");
106109
if(ImageInfo.inImageRuntimeCode()) {
@@ -119,4 +122,15 @@ public static void main(String[] args) throws IOException {
119122
}
120123
}
121124
}
125+
126+
private static String getProgramName() {
127+
if (ImageInfo.inImageRuntimeCode()) {
128+
if (ProcessProperties.getArgumentVectorBlockSize() > 0) {
129+
return ProcessProperties.getArgumentVectorProgramName();
130+
} else {
131+
return ProcessProperties.getExecutableName();
132+
}
133+
}
134+
return "";
135+
}
122136
}

0 commit comments

Comments
 (0)