Merge pull request #1524 from dan-fritchman/1175-extend-os

Add os.system
This commit is contained in:
Windel Bouwman
2019-10-14 20:15:28 +02:00
committed by GitHub
2 changed files with 36 additions and 9 deletions

View File

@@ -313,3 +313,18 @@ with TestWithTempDir() as tmpdir:
with os.scandir() as dir_iter:
collected_files = [dir_entry.name for dir_entry in dir_iter]
assert set(collected_files) == set(expected_files)
# system()
if "win" not in sys.platform:
assert os.system('ls') == 0
assert os.system('{') != 0
for arg in [None, 1, 1.0, TabError]:
try:
os.system(arg)
except TypeError:
pass
else:
raise AssertionError(f'os.system failed to raise TypeError with arg {arg}')

View File

@@ -942,6 +942,17 @@ fn os_chdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
env::set_current_dir(path.as_str()).map_err(|err| convert_io_error(vm, err))
}
#[cfg(unix)]
fn os_system(command: PyStringRef, _vm: &VirtualMachine) -> PyResult<i32> {
use libc::system;
use std::ffi::CString;
let rstr = command.as_str();
let cstr = CString::new(rstr).unwrap();
let x = unsafe { system(cstr.as_ptr()) };
Ok(x)
}
#[cfg(unix)]
fn os_chmod(
path: PyStringRef,
@@ -1292,6 +1303,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef {
let ctx = &vm.ctx;
extend_module!(vm, module, {
"access" => ctx.new_rustfunc(os_access),
"chmod" => ctx.new_rustfunc(os_chmod),
"getppid" => ctx.new_rustfunc(os_getppid),
"getgid" => ctx.new_rustfunc(os_getgid),
"getegid" => ctx.new_rustfunc(os_getegid),
@@ -1301,16 +1314,8 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
"setgid" => ctx.new_rustfunc(os_setgid),
"setpgid" => ctx.new_rustfunc(os_setpgid),
"setuid" => ctx.new_rustfunc(os_setuid),
"access" => ctx.new_rustfunc(os_access),
"O_DSYNC" => ctx.new_int(libc::O_DSYNC),
"O_NDELAY" => ctx.new_int(libc::O_NDELAY),
"O_NOCTTY" => ctx.new_int(libc::O_NOCTTY),
"O_CLOEXEC" => ctx.new_int(libc::O_CLOEXEC),
"chmod" => ctx.new_rustfunc(os_chmod),
"system" => ctx.new_rustfunc(os_system),
"ttyname" => ctx.new_rustfunc(os_ttyname),
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
"EX_OK" => ctx.new_int(exitcode::OK as i8),
"EX_USAGE" => ctx.new_int(exitcode::USAGE as i8),
"EX_DATAERR" => ctx.new_int(exitcode::DATAERR as i8),
@@ -1327,6 +1332,13 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
"EX_PROTOCOL" => ctx.new_int(exitcode::PROTOCOL as i8),
"EX_NOPERM" => ctx.new_int(exitcode::NOPERM as i8),
"EX_CONFIG" => ctx.new_int(exitcode::CONFIG as i8),
"O_DSYNC" => ctx.new_int(libc::O_DSYNC),
"O_NDELAY" => ctx.new_int(libc::O_NDELAY),
"O_NOCTTY" => ctx.new_int(libc::O_NOCTTY),
"O_CLOEXEC" => ctx.new_int(libc::O_CLOEXEC),
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
});
#[cfg(not(target_os = "redox"))]