Skip to content

Commit 2205fec

Browse files
Merge pull request RustPython#1524 from dan-fritchman/1175-extend-os
Add os.system
2 parents 92be5de + 2df54f3 commit 2205fec

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

tests/snippets/stdlib_os.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,18 @@ def __exit__(self, exc_type, exc_val, exc_tb):
313313
with os.scandir() as dir_iter:
314314
collected_files = [dir_entry.name for dir_entry in dir_iter]
315315
assert set(collected_files) == set(expected_files)
316+
317+
# system()
318+
if "win" not in sys.platform:
319+
assert os.system('ls') == 0
320+
assert os.system('{') != 0
321+
322+
for arg in [None, 1, 1.0, TabError]:
323+
try:
324+
os.system(arg)
325+
except TypeError:
326+
pass
327+
else:
328+
raise AssertionError(f'os.system failed to raise TypeError with arg {arg}')
329+
330+

vm/src/stdlib/os.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,17 @@ fn os_chdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
942942
env::set_current_dir(path.as_str()).map_err(|err| convert_io_error(vm, err))
943943
}
944944

945+
#[cfg(unix)]
946+
fn os_system(command: PyStringRef, _vm: &VirtualMachine) -> PyResult<i32> {
947+
use libc::system;
948+
use std::ffi::CString;
949+
950+
let rstr = command.as_str();
951+
let cstr = CString::new(rstr).unwrap();
952+
let x = unsafe { system(cstr.as_ptr()) };
953+
Ok(x)
954+
}
955+
945956
#[cfg(unix)]
946957
fn os_chmod(
947958
path: PyStringRef,
@@ -1292,6 +1303,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12921303
fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef {
12931304
let ctx = &vm.ctx;
12941305
extend_module!(vm, module, {
1306+
"access" => ctx.new_rustfunc(os_access),
1307+
"chmod" => ctx.new_rustfunc(os_chmod),
12951308
"getppid" => ctx.new_rustfunc(os_getppid),
12961309
"getgid" => ctx.new_rustfunc(os_getgid),
12971310
"getegid" => ctx.new_rustfunc(os_getegid),
@@ -1301,16 +1314,8 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
13011314
"setgid" => ctx.new_rustfunc(os_setgid),
13021315
"setpgid" => ctx.new_rustfunc(os_setpgid),
13031316
"setuid" => ctx.new_rustfunc(os_setuid),
1304-
"access" => ctx.new_rustfunc(os_access),
1305-
"O_DSYNC" => ctx.new_int(libc::O_DSYNC),
1306-
"O_NDELAY" => ctx.new_int(libc::O_NDELAY),
1307-
"O_NOCTTY" => ctx.new_int(libc::O_NOCTTY),
1308-
"O_CLOEXEC" => ctx.new_int(libc::O_CLOEXEC),
1309-
"chmod" => ctx.new_rustfunc(os_chmod),
1317+
"system" => ctx.new_rustfunc(os_system),
13101318
"ttyname" => ctx.new_rustfunc(os_ttyname),
1311-
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
1312-
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
1313-
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
13141319
"EX_OK" => ctx.new_int(exitcode::OK as i8),
13151320
"EX_USAGE" => ctx.new_int(exitcode::USAGE as i8),
13161321
"EX_DATAERR" => ctx.new_int(exitcode::DATAERR as i8),
@@ -1327,6 +1332,13 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
13271332
"EX_PROTOCOL" => ctx.new_int(exitcode::PROTOCOL as i8),
13281333
"EX_NOPERM" => ctx.new_int(exitcode::NOPERM as i8),
13291334
"EX_CONFIG" => ctx.new_int(exitcode::CONFIG as i8),
1335+
"O_DSYNC" => ctx.new_int(libc::O_DSYNC),
1336+
"O_NDELAY" => ctx.new_int(libc::O_NDELAY),
1337+
"O_NOCTTY" => ctx.new_int(libc::O_NOCTTY),
1338+
"O_CLOEXEC" => ctx.new_int(libc::O_CLOEXEC),
1339+
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
1340+
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
1341+
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
13301342
});
13311343

13321344
#[cfg(not(target_os = "redox"))]

0 commit comments

Comments
 (0)