@@ -68,7 +68,7 @@ func chdir(self py.Object, args py.Tuple) (py.Object, error) {
6868 if len (args ) == 0 || len (args ) > 1 {
6969 return nil , py .ExceptionNewf (py .TypeError , "One argument required" )
7070 }
71- if reflect . TypeOf (args [0 ]). String () == "py.String" {
71+ if objectIsString (args [0 ]) {
7272 dir , err := py .ReprAsString (args [0 ])
7373 if err != nil {
7474 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) {
9494 return nil , py .ExceptionNewf (py .KeyError , "1 argument required, \" key\" " )
9595 }
9696 if len (args ) == 1 {
97- if reflect . TypeOf (args [0 ]). String () == "py.String" {
97+ if objectIsString (args [0 ]) {
9898 key = args [0 ]
9999 } else {
100100 return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
101101 }
102102 default_ = py .None
103103 } 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 ]) {
105105 key = args [0 ]
106106 default_ = args [1 ]
107107 } else {
@@ -123,32 +123,30 @@ func getpid(self py.Object, args py.Tuple) (py.Object, error) {
123123
124124// Set the environment variable named key to the string value.
125125func 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 , "'" , "" )
138137
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" )
144141 }
142+ return py .None , nil
145143 }
146144 return nil , py .ExceptionNewf (py .TypeError , "Expected 2 arguments of type string" )
147145}
148146
149147// Unset (delete) the environment variable named key.
150148func unsetenv (self py.Object , args py.Tuple ) (py.Object , error ) {
151- if len (args ) == 1 {
149+ if len (args ) == 1 && objectIsString ( args [ 0 ]) {
152150 if reflect .TypeOf (args [0 ]).String () == "py.String" {
153151 _k , err := py .ReprAsString (args [0 ])
154152 if err != nil {
@@ -165,11 +163,12 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
165163 return nil , py .ExceptionNewf (py .TypeError , "Expected 1 argument of type string" )
166164}
167165
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.
169167func _exit (self py.Object , args py.Tuple ) (py.Object , error ) { // can never return
170168 if len (args ) == 0 {
171169 os .Exit (0 )
172- } else if len (args ) == 1 {
170+ }
171+ if len (args ) == 1 && objectIsInt (args [0 ]) {
173172 _ec , err := py .GetInt (args [0 ])
174173 if err != nil {
175174 os .Exit (1 )
@@ -186,21 +185,17 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
186185
187186// os.system(command string) this function runs a shell command and directs the output to standard output.
188187func system (self py.Object , args py.Tuple ) (py.Object , error ) {
189- if len (args ) == 0 {
188+ if len (args ) == 0 && ! ( objectIsString ( args [ 0 ])) {
190189 return nil , py .ExceptionNewf (py .TypeError , "Expected 1 or more arguments all of type string" )
191190 }
192191 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
203195 }
196+ carg := strings .ReplaceAll (_carg , "'" , "" ) // required
197+
198+ cargs = strings .Split (carg , " " )
204199 command := exec .Command (cargs [0 ], cargs [1 :]... )
205200 outb , err := command .Output ()
206201 if err != nil {
@@ -209,3 +204,18 @@ func system(self py.Object, args py.Tuple) (py.Object, error) {
209204 fmt .Println (string (outb )) // FIXME - use gpython Stdout
210205 return py .Int (0 ), nil
211206}
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