Skip to content

Commit 1f78cdb

Browse files
committed
1 parent 144ad9b commit 1f78cdb

File tree

1 file changed

+52
-65
lines changed

1 file changed

+52
-65
lines changed

os/os.module.go

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,20 @@ func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
6767
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")
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())
8480
}
81+
return py.None, nil
8582
}
83+
return nil, py.ExceptionNewf(py.TypeError, "Expected string argument at position 0")
8684
}
8785

8886
// get a enviroment variable by key
@@ -94,29 +92,28 @@ func getenv(self py.Object, args py.Tuple) (py.Object, error) {
9492

9593
if len(args) > 2 {
9694
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")
112101
}
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")
117109
}
118-
return res, nil
119110
}
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
120117
}
121118

122119
// get the current process' pid
@@ -144,12 +141,9 @@ func putenv(self py.Object, args py.Tuple) (py.Object, error) {
144141
return nil, py.ExceptionNewf(py.OSError, "Unable to set enviroment variable")
145142
}
146143
return py.None, nil
147-
} else {
148-
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
149144
}
150-
} else {
151-
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
152145
}
146+
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
153147
}
154148

155149
// Unset (delete) the environment variable named key.
@@ -166,19 +160,15 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
166160
return nil, py.ExceptionNewf(py.OSError, "Unable to unset enviroment variable")
167161
}
168162
return py.None, nil
169-
} else {
170-
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 argument of type string")
171163
}
172-
} else {
173-
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 argument of type string")
174164
}
165+
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 argument of type string")
175166
}
176167

177168
// os._exit() immediate program termination; unline sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
178169
func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never return
179170
if len(args) == 0 {
180171
os.Exit(0)
181-
return nil, nil
182172
} else if len(args) == 1 {
183173
_ec, err := py.GetInt(args[0])
184174
if err != nil {
@@ -189,36 +179,33 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
189179
os.Exit(1)
190180
}
191181
os.Exit(exit_code)
192-
return nil, nil
193-
} else {
194-
os.Exit(1)
195-
return nil, nil
196182
}
183+
os.Exit(1)
184+
return nil, nil
197185
}
198186

199187
// os.system(command string) this function runs a shell command and directs the output to standard output.
200188
func system(self py.Object, args py.Tuple) (py.Object, error) {
201189
if len(args) == 0 {
202190
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])
218195
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
220197
}
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())
223208
}
209+
fmt.Println(string(outb))
210+
return py.Int(0), nil
224211
}

0 commit comments

Comments
 (0)