Repository: RustPython/RustPython (language: rust)

Issue:
     request for os.system function. 
## Feature

<!-- What Python feature is missing from RustPython? Give a short description of the feature and how you ran into its absence. -->

## Python Documentation or reference to CPython source code

<!-- Give a link to the feature in the CPython documentation (https://docs.python.org/3/) in order to assist in its implementation. -->



Discussion / hints:
RustPython already supports basic os.system function. Check if you can import `os` module in your environment.
i see not working in windows.

>>>>> os.system
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'system'

On Mon, Oct 23, 2023 at 6:30 AM Jeong, YunWon ***@***.***>
wrote:

> RustPython already supports basic os.system function. Check if you can
> import os module in your environment.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/RustPython/RustPython/issues/5100#issuecomment-1774266405>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AEBCZFLDZSAQK2QWTVXCNG3YAW6TXAVCNFSM6AAAAAA6LEKJTKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZUGI3DMNBQGU>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>


Fix patch:
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 5d70327d38..24637d5a90 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -142,6 +142,7 @@ pub(super) mod _os {
         protocol::PyIterReturn,
         recursion::ReprGuard,
         types::{IterNext, Iterable, PyStructSequence, Representable, SelfIter},
+        utils::ToCString,
         vm::VirtualMachine,
         AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
     };
@@ -1028,6 +1029,14 @@ pub(super) mod _os {
         })
     }
 
+    #[cfg(any(unix, windows))]
+    #[pyfunction]
+    fn system(command: PyStrRef, vm: &VirtualMachine) -> PyResult<i32> {
+        let cstr = command.to_cstring(vm)?;
+        let x = unsafe { libc::system(cstr.as_ptr()) };
+        Ok(x)
+    }
+
     #[derive(FromArgs)]
     struct UtimeArgs {
         path: OsPath,
diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs
index ed4754dae2..1b843bab60 100644
--- a/vm/src/stdlib/posix.rs
+++ b/vm/src/stdlib/posix.rs
@@ -901,13 +901,6 @@ pub mod module {
         nix::unistd::pipe2(oflags).map_err(|err| err.into_pyexception(vm))
     }
 
-    #[pyfunction]
-    fn system(command: PyStrRef, vm: &VirtualMachine) -> PyResult<i32> {
-        let cstr = command.to_cstring(vm)?;
-        let x = unsafe { libc::system(cstr.as_ptr()) };
-        Ok(x)
-    }
-
     fn _chmod(
         path: OsPath,
         dir_fd: DirFd<0>,


Test patch:
diff --git a/extra_tests/snippets/stdlib_os.py b/extra_tests/snippets/stdlib_os.py
index 849fc238a8..ab40582fd8 100644
--- a/extra_tests/snippets/stdlib_os.py
+++ b/extra_tests/snippets/stdlib_os.py
@@ -502,9 +502,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
             assert set(collected_files) == set(expected_files)
 
 # system()
-if "win" not in sys.platform:
-    assert os.system('ls') == 0
-    assert os.system('{') != 0
+if os.name in ('posix', 'nt'):
+    assert os.system('echo test') == 0
+    assert os.system('&') != 0
 
     for arg in [None, 1, 1.0, TabError]:
         assert_raises(TypeError, os.system, arg)