Skip to content

Commit 2a033ab

Browse files
committed
add os._exit()
1 parent d2f75ec commit 2a033ab

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

main

5.78 MB
Binary file not shown.

os/os.module.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func init() {
2121
py.MustNewMethod("getpid", getpid, 0, "Return the current process id."),
2222
py.MustNewMethod("putenv", putenv, 0, "Set the environment variable named key to the string value."),
2323
py.MustNewMethod("unsetenv", unsetenv, 0, "Unset (delete) the environment variable named key."),
24+
py.MustNewMethod("_exit", _exit, 0, "Immediate program termination."),
2425
}
2526

2627
globals := py.StringDict{
@@ -169,3 +170,25 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
169170
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 argument of type string")
170171
}
171172
}
173+
174+
// os._exit() immediate program termination; unline sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
175+
func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never return
176+
if len(args) == 0 {
177+
os.Exit(0)
178+
return nil, nil
179+
} else if len(args) == 1 {
180+
_ec, err := py.GetInt(args[0])
181+
if err != nil {
182+
os.Exit(1)
183+
}
184+
exit_code, err := _ec.GoInt()
185+
if err != nil {
186+
os.Exit(1)
187+
}
188+
os.Exit(exit_code)
189+
return nil, nil
190+
} else {
191+
os.Exit(1)
192+
return nil, nil
193+
}
194+
}

os/os.test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ def _fail(test_name):
109109
print()
110110
print(bcolors.ENDC+bcolors.BOLD+bcolors.HEADER+"----- Results -----")
111111
print(bcolors.OKGREEN+bcolors.BOLD, "PASSED: "+str(passed_tests)+" out of "+str(passed_tests+failed))
112-
print(bcolors.FAIL+bcolors.BOLD, "FAILED: "+str(failed)+" out of "+str(passed_tests+failed))
112+
print(bcolors.FAIL+bcolors.BOLD, "FAILED: "+str(failed)+" out of "+str(passed_tests+failed))
113+
print()
114+
print(bcolors.ENDC+bcolors.HEADER+bcolors.BOLD+"Running an extra test... | os._exit(0)")
115+
print(bcolors.OKCYAN+"If the program quits without any further output, the test is a pass...")
116+
os._exit(0)
117+
print(bcolors.ENDC+bcolors.BOLD+bcolors.FAIL+"Test failed!")

0 commit comments

Comments
 (0)