Skip to content

Commit 464da1f

Browse files
author
Franziska Geiger
committed
- Added execution output stream to java system out
- Added argument testing to unit test
1 parent 1c6939b commit 464da1f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,21 @@ def test_uname(self):
5454
self.assertIsNotNone(uname.machine)
5555

5656
def test_execv(self):
57+
# test creates a shell script, which again creates a file, to ensure script execution
58+
# Both files are deleted again in the end
5759
import os
5860
import stat
5961
cwd = os.getcwd()
6062
new_file_path = os.path.join(cwd , 'myscript.sh')
61-
6263
with open(new_file_path, 'w') as script:
6364
script.write('#!/bin/sh\n')
64-
script.write("echo \"something echo\" > {}/test.txt".format(cwd))
65+
script.write("echo \"something echo with\" $1 > {}/test.txt\n".format(cwd))
66+
script.write('echo this is an output\n')
6567
assert os.path.isfile(cwd + '/myscript.sh')
66-
6768
st = os.stat(new_file_path)
6869
os.chmod(new_file_path, st.st_mode | stat.S_IEXEC)
69-
70-
os.execv(new_file_path, [new_file_path, 'Something'])
70+
os.execv(new_file_path, [new_file_path, 'the_input'])
7171
assert os.path.isfile(cwd + '/test.txt')
72-
7372
os.remove(new_file_path)
7473
os.remove(cwd + '/test.txt')
7574

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
import static com.oracle.truffle.api.TruffleFile.UNIX_PERMISSIONS;
5050
import static com.oracle.truffle.api.TruffleFile.UNIX_UID;
5151

52+
import java.io.BufferedReader;
5253
import java.io.IOException;
5354
import java.io.InputStream;
55+
import java.io.InputStreamReader;
5456
import java.io.OutputStream;
5557
import java.io.UnsupportedEncodingException;
5658
import java.lang.ProcessBuilder.Redirect;
@@ -303,11 +305,17 @@ Object doExecute(String path, PList args) {
303305
try {
304306
int size = args.getSequenceStorage().length();
305307
String[] cmd = new String[size];
306-
cmd[0] = path;
307-
for (int i = 1; i < size; i++) {
308+
for (int i = 0; i < size; i++) {
308309
cmd[i] = args.getSequenceStorage().getItemNormalized(i).toString();
309310
}
310-
new ProcessBuilder(cmd).start();
311+
Runtime rt = Runtime.getRuntime();
312+
Process pr = rt.exec(cmd);
313+
// retrieve output from executed script and print it
314+
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
315+
String line = "";
316+
while ((line = bfr.readLine()) != null) {
317+
System.out.println(line);
318+
}
311319
} catch (IOException e) {
312320
throw raise(PythonErrorType.ValueError, "Could not execute script '%s'", e.getMessage());
313321
}

0 commit comments

Comments
 (0)