Skip to content

Commit 9c658b6

Browse files
pfalcondpgeorge
authored andcommitted
unix, windows: Add _os.system() call.
system() is the basic function to support automation of tasks, so have it available builtin, for example, for bootstrapping rest of micropython environment.
1 parent a37656c commit 9c658b6

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

unix/modos.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sys/stat.h>
3030
#include <unistd.h>
3131
#include <errno.h>
32+
#include <stdlib.h>
3233

3334
#include "mpconfig.h"
3435
#include "misc.h"
@@ -77,9 +78,21 @@ STATIC mp_obj_t mod_os_unlink(mp_obj_t path_in) {
7778
}
7879
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_unlink_obj, mod_os_unlink);
7980

81+
STATIC mp_obj_t mod_os_system(mp_obj_t cmd_in) {
82+
const char *cmd = mp_obj_str_get_str(cmd_in);
83+
84+
int r = system(cmd);
85+
86+
RAISE_ERRNO(r, errno);
87+
88+
return MP_OBJ_NEW_SMALL_INT(r);
89+
}
90+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_system_obj, mod_os_system);
91+
8092
STATIC const mp_map_elem_t mp_module_os_globals_table[] = {
8193
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__os) },
8294
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mod_os_stat_obj },
95+
{ MP_OBJ_NEW_QSTR(MP_QSTR_system), (mp_obj_t)&mod_os_system_obj },
8396
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink),(mp_obj_t)&mod_os_unlink_obj},
8497
};
8598

unix/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Q(flush)
3636

3737
Q(_os)
3838
Q(stat)
39+
Q(system)
3940
Q(unlink)
4041

4142
Q(ffi)

0 commit comments

Comments
 (0)