100
100
import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .ConvertPathlikeObjectNodeGen ;
101
101
import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .StatNodeFactory ;
102
102
import com .oracle .graal .python .builtins .objects .PNone ;
103
+ import com .oracle .graal .python .builtins .objects .bytes .BytesNodes ;
103
104
import com .oracle .graal .python .builtins .objects .bytes .BytesNodes .ToBytesNode ;
104
105
import com .oracle .graal .python .builtins .objects .bytes .PByteArray ;
105
106
import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
@@ -288,34 +289,36 @@ public void postInitialize(PythonCore core) {
288
289
((PDict ) environAttr ).setDictStorage (environ .getDictStorage ());
289
290
}
290
291
291
- @ Builtin (name = "execv" , minNumOfPositionalArgs = 2 )
292
+ @ Builtin (name = "execv" , minNumOfPositionalArgs = 3 , declaresExplicitSelf = true )
292
293
@ GenerateNodeFactory
293
294
public abstract static class ExecvNode extends PythonBuiltinNode {
294
295
296
+ @ Child private BytesNodes .ToBytesNode toBytes = BytesNodes .ToBytesNode .create ();
297
+
295
298
@ Specialization
296
- Object execute (String path , PList args ) {
297
- return doExecute (path , args );
299
+ Object execute (PythonModule thisModule , String path , PList args ) {
300
+ return doExecute (thisModule , path , args );
298
301
}
299
302
300
303
@ Specialization
301
- Object execute (PString path , PTuple args ) {
302
- return execute (path .getValue (), args );
304
+ Object execute (PythonModule thisModule , PString path , PTuple args ) {
305
+ return execute (thisModule , path .getValue (), args );
303
306
}
304
307
305
308
@ Specialization
306
- Object execute (String path , PTuple args ) {
309
+ Object execute (PythonModule thisModule , String path , PTuple args ) {
307
310
// in case of execl the PList happens to be in the tuples first entry
308
311
Object list = GetItemDynamicNode .getUncached ().execute (args .getSequenceStorage (), 0 );
309
- return doExecute (path , list instanceof PList ? (PList ) list : args );
312
+ return doExecute (thisModule , path , list instanceof PList ? (PList ) list : args );
310
313
}
311
314
312
315
@ Specialization
313
- Object execute (PString path , PList args ) {
314
- return doExecute (path .getValue (), args );
316
+ Object execute (PythonModule thisModule , PString path , PList args ) {
317
+ return doExecute (thisModule , path .getValue (), args );
315
318
}
316
319
317
320
@ TruffleBoundary
318
- Object doExecute (String path , PSequence args ) {
321
+ Object doExecute (PythonModule thisModule , String path , PSequence args ) {
319
322
try {
320
323
if (!getContext ().isExecutableAccessAllowed ()) {
321
324
throw raise (OSError , "executable access denied" );
@@ -328,22 +331,32 @@ Object doExecute(String path, PSequence args) {
328
331
for (int i = 0 ; i < size ; i ++) {
329
332
cmd [i ] = GetItemDynamicNode .getUncached ().execute (args .getSequenceStorage (), i ).toString ();
330
333
}
331
- Runtime rt = Runtime .getRuntime ();
332
- Process pr = rt .exec (cmd );
333
- // retrieve output from executed script
334
+ PDict environ = (PDict ) thisModule .getAttribute ("environ" );
335
+ ProcessBuilder builder = new ProcessBuilder (cmd );
336
+ Map <String , String > environment = builder .environment ();
337
+ environ .entries ().forEach (entry -> {
338
+ environment .put (new String (toBytes .execute (null , entry .key )), new String (toBytes .execute (null , entry .value )));
339
+ });
340
+ Process pr = builder .start ();
334
341
BufferedReader bfr = new BufferedReader (new InputStreamReader (pr .getInputStream ()));
335
342
OutputStream stream = getContext ().getEnv ().out ();
336
343
String line = "" ;
337
344
while ((line = bfr .readLine ()) != null ) {
338
345
stream .write (line .getBytes ());
346
+ stream .write ("\n " .getBytes ());
347
+ }
348
+ BufferedReader stderr = new BufferedReader (new InputStreamReader (pr .getErrorStream ()));
349
+ OutputStream errStream = getContext ().getEnv ().err ();
350
+ line = "" ;
351
+ while ((line = stderr .readLine ()) != null ) {
352
+ errStream .write (line .getBytes ());
353
+ errStream .write ("\n " .getBytes ());
339
354
}
340
-
341
355
try {
342
356
pr .waitFor ();
343
357
} catch (InterruptedException e ) {
344
358
throw new IOException (e );
345
359
}
346
-
347
360
throw new PythonExitException (this , pr .exitValue ());
348
361
} catch (IOException e ) {
349
362
throw raise (PythonErrorType .ValueError , "Could not execute script '%s'" , e .getMessage ());
@@ -362,7 +375,6 @@ String cwd() {
362
375
return "" ;
363
376
}
364
377
}
365
-
366
378
}
367
379
368
380
@ Builtin (name = "chdir" , minNumOfPositionalArgs = 1 )
0 commit comments