@@ -9,13 +9,13 @@ package os
9
9
import (
10
10
"os"
11
11
"os/exec"
12
+ "runtime"
12
13
"strings"
13
14
14
15
"github.com/go-python/gpython/py"
15
16
)
16
17
17
18
func init () {
18
-
19
19
methods := []* py.Method {
20
20
py .MustNewMethod ("getcwd" , getCwd , 0 , "Get the current working directory" ),
21
21
py .MustNewMethod ("getcwdb" , getCwdb , 0 , "Get the current working directory in a byte slice" ),
@@ -172,18 +172,22 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
172
172
173
173
// os.system(command string) this function runs a shell command and directs the output to standard output.
174
174
func system (self py.Object , args py.Tuple ) (py.Object , error ) {
175
- if len (args ) == 0 && ! ( objectIsString ( args [ 0 ])) {
175
+ if len (args ) != 1 {
176
176
return nil , py .ExceptionNewf (py .TypeError , "missing one required argument: 'command:str'" )
177
177
}
178
178
var cargs []string
179
- _carg , err := py . ReprAsString ( args [0 ])
180
- if err != nil {
181
- return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
179
+ _carg , ok := args [0 ].(py. String )
180
+ if ! ok {
181
+ return nil , py .ExceptionNewf (py .TypeError , "str expected (pos 1), not" + args [ 0 ]. Type (). Name )
182
182
}
183
- carg := strings .ReplaceAll (_carg , "'" , "" ) // required
183
+ carg := strings .ReplaceAll (string ( _carg ) , "'" , "" )
184
184
185
- cargs = strings .Split (carg , " " )
186
- command := exec .Command (cargs [0 ], cargs [1 :]... )
185
+ var command * exec.Cmd
186
+ if runtime .GOOS != "windows" {
187
+ command = exec .Command ("/bin/sh" , "-c" , carg )
188
+ } else {
189
+ command = exec .Command (cargs [0 ], cargs [1 :]... )
190
+ }
187
191
outb , err := command .CombinedOutput () // - commbinedoutput to get both stderr and stdout -
188
192
if err != nil {
189
193
return nil , py .ExceptionNewf (py .OSError , err .Error ())
0 commit comments