Skip to content

Commit 302784d

Browse files
committed
sysmod.argv added + snippet
1 parent 2f424eb commit 302784d

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

tests/snippets/sysmod_argv.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
3+
print(sys.argv[1])
4+

vm/src/sysmodule.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
// use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
1+
use std::env;
2+
use super::pyobject::{DictProtocol, PyContext, PyObjectRef, PyFuncArgs, PyResult};
3+
use super::vm::VirtualMachine;
24

35
/*
46
* The magic sys module.
57
*/
6-
use std::env;
78

8-
use super::pyobject::{DictProtocol, PyContext, PyObjectRef};
9+
fn argv(ctx: &PyContext) -> PyObjectRef {
10+
let argv: Vec<PyObjectRef> = env::args().map(|x| ctx.new_str(x)).collect();
11+
ctx.new_list(argv)
12+
}
13+
914

1015
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
1116
let path_list = match env::var_os("PYTHONPATH") {
@@ -20,6 +25,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
2025
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
2126
modules.set_item(&sys_name, sys_mod.clone());
2227
sys_mod.set_item(&"modules".to_string(), modules);
28+
sys_mod.set_item(&"argv".to_string(), argv(ctx));
2329
sys_mod.set_item(&"path".to_string(), path);
2430
sys_mod
2531
}

0 commit comments

Comments
 (0)