@@ -68,7 +68,7 @@ func chdir(self py.Object, args py.Tuple) (py.Object, error) {
68
68
if len (args ) == 0 || len (args ) > 1 {
69
69
return nil , py .ExceptionNewf (py .TypeError , "One argument required" )
70
70
}
71
- if reflect . TypeOf (args [0 ]). String () == "py.String" {
71
+ if objectIsString (args [0 ]) {
72
72
dir , err := py .ReprAsString (args [0 ])
73
73
if err != nil {
74
74
return nil , py .ExceptionNewf (py .TypeError , "Failed to parse string" )
@@ -94,14 +94,14 @@ func getenv(self py.Object, args py.Tuple) (py.Object, error) {
94
94
return nil , py .ExceptionNewf (py .KeyError , "1 argument required, \" key\" " )
95
95
}
96
96
if len (args ) == 1 {
97
- if reflect . TypeOf (args [0 ]). String () == "py.String" {
97
+ if objectIsString (args [0 ]) {
98
98
key = args [0 ]
99
99
} else {
100
100
return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
101
101
}
102
102
default_ = py .None
103
103
} else if len (args ) == 2 {
104
- if reflect . TypeOf (args [0 ]). String () == "py.String" && reflect . TypeOf ( args [ 1 ]). String () == "py.String" {
104
+ if objectIsString (args [1 ]) {
105
105
key = args [0 ]
106
106
default_ = args [1 ]
107
107
} else {
@@ -123,32 +123,30 @@ func getpid(self py.Object, args py.Tuple) (py.Object, error) {
123
123
124
124
// Set the environment variable named key to the string value.
125
125
func putenv (self py.Object , args py.Tuple ) (py.Object , error ) {
126
- if len (args ) == 2 {
127
- if reflect .TypeOf (args [0 ]).String () == "py.String" && reflect .TypeOf (args [1 ]).String () == "py.String" {
128
- _k , err := py .ReprAsString (args [0 ])
129
- if err != nil {
130
- return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
131
- }
132
- key := strings .ReplaceAll (_k , "'" , "" ) // required
133
- _v , err := py .ReprAsString (args [1 ])
134
- if err != nil {
135
- return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
136
- }
137
- value := strings .ReplaceAll (_v , "'" , "" )
126
+ if len (args ) == 2 && objectIsString (args [0 ]) && objectIsString (args [1 ]) {
127
+ _k , err := py .ReprAsString (args [0 ])
128
+ if err != nil {
129
+ return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
130
+ }
131
+ key := strings .ReplaceAll (_k , "'" , "" ) // required
132
+ _v , err := py .ReprAsString (args [1 ])
133
+ if err != nil {
134
+ return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
135
+ }
136
+ value := strings .ReplaceAll (_v , "'" , "" )
138
137
139
- err = os .Setenv (key , value )
140
- if err != nil {
141
- return nil , py .ExceptionNewf (py .OSError , "Unable to set enviroment variable" )
142
- }
143
- return py .None , nil
138
+ err = os .Setenv (key , value )
139
+ if err != nil {
140
+ return nil , py .ExceptionNewf (py .OSError , "Unable to set enviroment variable" )
144
141
}
142
+ return py .None , nil
145
143
}
146
144
return nil , py .ExceptionNewf (py .TypeError , "Expected 2 arguments of type string" )
147
145
}
148
146
149
147
// Unset (delete) the environment variable named key.
150
148
func unsetenv (self py.Object , args py.Tuple ) (py.Object , error ) {
151
- if len (args ) == 1 {
149
+ if len (args ) == 1 && objectIsString ( args [ 0 ]) {
152
150
if reflect .TypeOf (args [0 ]).String () == "py.String" {
153
151
_k , err := py .ReprAsString (args [0 ])
154
152
if err != nil {
@@ -165,11 +163,12 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
165
163
return nil , py .ExceptionNewf (py .TypeError , "Expected 1 argument of type string" )
166
164
}
167
165
168
- // os._exit() immediate program termination; unline sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
166
+ // os._exit() immediate program termination; unlike sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
169
167
func _exit (self py.Object , args py.Tuple ) (py.Object , error ) { // can never return
170
168
if len (args ) == 0 {
171
169
os .Exit (0 )
172
- } else if len (args ) == 1 {
170
+ }
171
+ if len (args ) == 1 && objectIsInt (args [0 ]) {
173
172
_ec , err := py .GetInt (args [0 ])
174
173
if err != nil {
175
174
os .Exit (1 )
@@ -186,21 +185,17 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
186
185
187
186
// os.system(command string) this function runs a shell command and directs the output to standard output.
188
187
func system (self py.Object , args py.Tuple ) (py.Object , error ) {
189
- if len (args ) == 0 {
188
+ if len (args ) == 0 && ! ( objectIsString ( args [ 0 ])) {
190
189
return nil , py .ExceptionNewf (py .TypeError , "Expected 1 or more arguments all of type string" )
191
190
}
192
191
var cargs []string
193
- if reflect .TypeOf (args [0 ]).String () == "py.String" {
194
- _carg , err := py .ReprAsString (args [0 ])
195
- if err != nil {
196
- return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
197
- }
198
- carg := strings .ReplaceAll (_carg , "'" , "" ) // required
199
-
200
- cargs = strings .Split (carg , " " )
201
- } else {
202
- return nil , py .ExceptionNewf (py .TypeError , "Expected 1 or more arguments all of type string" )
192
+ _carg , err := py .ReprAsString (args [0 ])
193
+ if err != nil {
194
+ return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
203
195
}
196
+ carg := strings .ReplaceAll (_carg , "'" , "" ) // required
197
+
198
+ cargs = strings .Split (carg , " " )
204
199
command := exec .Command (cargs [0 ], cargs [1 :]... )
205
200
outb , err := command .Output ()
206
201
if err != nil {
@@ -209,3 +204,18 @@ func system(self py.Object, args py.Tuple) (py.Object, error) {
209
204
fmt .Println (string (outb )) // FIXME - use gpython Stdout
210
205
return py .Int (0 ), nil
211
206
}
207
+
208
+ // https://github.com/go-python/gpython/pull/169#discussion_r828767552 - type assertion
209
+ func objectIsString (arg py.Object ) bool {
210
+ if _ , ok := arg .(py.String ); ok {
211
+ return true
212
+ }
213
+ return false
214
+ }
215
+
216
+ func objectIsInt (arg py.Object ) bool {
217
+ if _ , ok := arg .(py.Int ); ok {
218
+ return true
219
+ }
220
+ return false
221
+ }
0 commit comments