@@ -67,22 +67,20 @@ func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
67
67
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
- } else {
71
- if reflect .TypeOf (args [0 ]).String () == "py.String" {
72
- dir , err := py .ReprAsString (args [0 ])
73
- if err != nil {
74
- return nil , py .ExceptionNewf (py .TypeError , "Failed to parse string" )
75
- }
76
- dir = strings .ReplaceAll (dir , "'" , "" )
77
- err = os .Chdir (dir )
78
- if err != nil {
79
- return nil , py .ExceptionNewf (py .OSError , "Couldn't change cwd; " + err .Error ())
80
- }
81
- return py .None , nil
82
- } else {
83
- return nil , py .ExceptionNewf (py .TypeError , "Expected string argument at position 0" )
70
+ }
71
+ if reflect .TypeOf (args [0 ]).String () == "py.String" {
72
+ dir , err := py .ReprAsString (args [0 ])
73
+ if err != nil {
74
+ return nil , py .ExceptionNewf (py .TypeError , "Failed to parse string" )
75
+ }
76
+ dir = strings .ReplaceAll (dir , "'" , "" )
77
+ err = os .Chdir (dir )
78
+ if err != nil {
79
+ return nil , py .ExceptionNewf (py .OSError , "Couldn't change cwd; " + err .Error ())
84
80
}
81
+ return py .None , nil
85
82
}
83
+ return nil , py .ExceptionNewf (py .TypeError , "Expected string argument at position 0" )
86
84
}
87
85
88
86
// get a enviroment variable by key
@@ -94,29 +92,28 @@ func getenv(self py.Object, args py.Tuple) (py.Object, error) {
94
92
95
93
if len (args ) > 2 {
96
94
return nil , py .ExceptionNewf (py .KeyError , "1 argument required, \" key\" " )
97
- } else {
98
- if len (args ) == 1 {
99
- if reflect .TypeOf (args [0 ]).String () == "py.String" {
100
- key = args [0 ]
101
- } else {
102
- return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
103
- }
104
- default_ = py .None
105
- } else if len (args ) == 2 {
106
- if reflect .TypeOf (args [0 ]).String () == "py.String" && reflect .TypeOf (args [1 ]).String () == "py.String" {
107
- key = args [0 ]
108
- default_ = args [1 ]
109
- } else {
110
- return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
111
- }
95
+ }
96
+ if len (args ) == 1 {
97
+ if reflect .TypeOf (args [0 ]).String () == "py.String" {
98
+ key = args [0 ]
99
+ } else {
100
+ return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
112
101
}
113
- var res py.Object // hold the result value
114
- res , err = getEnvVariables ().M__getitem__ (key )
115
- if err != nil {
116
- return default_ , nil
102
+ default_ = py .None
103
+ } else if len (args ) == 2 {
104
+ if reflect .TypeOf (args [0 ]).String () == "py.String" && reflect .TypeOf (args [1 ]).String () == "py.String" {
105
+ key = args [0 ]
106
+ default_ = args [1 ]
107
+ } else {
108
+ return nil , py .ExceptionNewf (py .TypeError , "Expected argument of type string" )
117
109
}
118
- return res , nil
119
110
}
111
+ var res py.Object // hold the result value
112
+ res , err = getEnvVariables ().M__getitem__ (key )
113
+ if err != nil {
114
+ return default_ , nil
115
+ }
116
+ return res , nil
120
117
}
121
118
122
119
// get the current process' pid
@@ -144,12 +141,9 @@ func putenv(self py.Object, args py.Tuple) (py.Object, error) {
144
141
return nil , py .ExceptionNewf (py .OSError , "Unable to set enviroment variable" )
145
142
}
146
143
return py .None , nil
147
- } else {
148
- return nil , py .ExceptionNewf (py .TypeError , "Expected 2 arguments of type string" )
149
144
}
150
- } else {
151
- return nil , py .ExceptionNewf (py .TypeError , "Expected 2 arguments of type string" )
152
145
}
146
+ return nil , py .ExceptionNewf (py .TypeError , "Expected 2 arguments of type string" )
153
147
}
154
148
155
149
// Unset (delete) the environment variable named key.
@@ -166,19 +160,15 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
166
160
return nil , py .ExceptionNewf (py .OSError , "Unable to unset enviroment variable" )
167
161
}
168
162
return py .None , nil
169
- } else {
170
- return nil , py .ExceptionNewf (py .TypeError , "Expected 1 argument of type string" )
171
163
}
172
- } else {
173
- return nil , py .ExceptionNewf (py .TypeError , "Expected 1 argument of type string" )
174
164
}
165
+ return nil , py .ExceptionNewf (py .TypeError , "Expected 1 argument of type string" )
175
166
}
176
167
177
168
// os._exit() immediate program termination; unline sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
178
169
func _exit (self py.Object , args py.Tuple ) (py.Object , error ) { // can never return
179
170
if len (args ) == 0 {
180
171
os .Exit (0 )
181
- return nil , nil
182
172
} else if len (args ) == 1 {
183
173
_ec , err := py .GetInt (args [0 ])
184
174
if err != nil {
@@ -189,36 +179,33 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
189
179
os .Exit (1 )
190
180
}
191
181
os .Exit (exit_code )
192
- return nil , nil
193
- } else {
194
- os .Exit (1 )
195
- return nil , nil
196
182
}
183
+ os .Exit (1 )
184
+ return nil , nil
197
185
}
198
186
199
187
// os.system(command string) this function runs a shell command and directs the output to standard output.
200
188
func system (self py.Object , args py.Tuple ) (py.Object , error ) {
201
189
if len (args ) == 0 {
202
190
return nil , py .ExceptionNewf (py .TypeError , "Expected 1 or more arguments all of type string" )
203
- } else {
204
- var cargs []string
205
- if reflect .TypeOf (args [0 ]).String () == "py.String" {
206
- _carg , err := py .ReprAsString (args [0 ])
207
- if err != nil {
208
- return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
209
- }
210
- carg := strings .ReplaceAll (_carg , "'" , "" ) // required
211
-
212
- cargs = strings .Split (carg , " " )
213
- } else {
214
- return nil , py .ExceptionNewf (py .TypeError , "Expected 1 or more arguments all of type string" )
215
- }
216
- command := exec .Command (cargs [0 ])
217
- outb , err := command .Output ()
191
+ }
192
+ var cargs []string
193
+ if reflect .TypeOf (args [0 ]).String () == "py.String" {
194
+ _carg , err := py .ReprAsString (args [0 ])
218
195
if err != nil {
219
- return nil , py .ExceptionNewf (py .OSError , err . Error ())
196
+ return nil , py .ExceptionNewf (py .TypeError , "Unable to parse string" ) // this will never execute
220
197
}
221
- fmt .Println (string (outb ))
222
- return py .Int (0 ), nil
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" )
203
+ }
204
+ command := exec .Command (cargs [0 ])
205
+ outb , err := command .Output ()
206
+ if err != nil {
207
+ return nil , py .ExceptionNewf (py .OSError , err .Error ())
223
208
}
209
+ fmt .Println (string (outb ))
210
+ return py .Int (0 ), nil
224
211
}
0 commit comments