1
- use super :: pyobject:: { DictProtocol , PyContext , PyFuncArgs , PyObjectRef , PyResult } ;
2
- use super :: vm:: VirtualMachine ;
3
- use std:: env;
1
+ use num_bigint:: ToBigInt ;
2
+ use obj:: objtype;
3
+ use pyobject:: { DictProtocol , PyContext , PyFuncArgs , PyObjectRef , PyResult , TypeProtocol } ;
4
+ use std:: rc:: Rc ;
5
+ use std:: { env, mem} ;
6
+ use vm:: VirtualMachine ;
4
7
5
8
/*
6
9
* The magic sys module.
@@ -20,6 +23,19 @@ fn getframe(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
20
23
}
21
24
}
22
25
26
+ fn sys_getrefcount ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
27
+ arg_check ! ( vm, args, required = [ ( object, None ) ] ) ;
28
+ let size = Rc :: strong_count ( & object) ;
29
+ Ok ( vm. ctx . new_int ( size. to_bigint ( ) . unwrap ( ) ) )
30
+ }
31
+
32
+ fn sys_getsizeof ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
33
+ arg_check ! ( vm, args, required = [ ( object, None ) ] ) ;
34
+ // TODO: implement default optional argument.
35
+ let size = mem:: size_of_val ( & object. borrow ( ) ) ;
36
+ Ok ( vm. ctx . new_int ( size. to_bigint ( ) . unwrap ( ) ) )
37
+ }
38
+
23
39
pub fn mk_module ( ctx : & PyContext ) -> PyObjectRef {
24
40
let path_list = match env:: var_os ( "PYTHONPATH" ) {
25
41
Some ( paths) => env:: split_paths ( & paths)
@@ -34,7 +50,13 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
34
50
modules. set_item ( & sys_name, sys_mod. clone ( ) ) ;
35
51
sys_mod. set_item ( "modules" , modules) ;
36
52
sys_mod. set_item ( "argv" , argv ( ctx) ) ;
53
+ sys_mod. set_item ( "getrefcount" , ctx. new_rustfunc ( sys_getrefcount) ) ;
54
+ sys_mod. set_item ( "getsizeof" , ctx. new_rustfunc ( sys_getsizeof) ) ;
55
+ let maxsize = ctx. new_int ( std:: usize:: MAX . to_bigint ( ) . unwrap ( ) ) ;
56
+ sys_mod. set_item ( "maxsize" , maxsize) ;
37
57
sys_mod. set_item ( "path" , path) ;
58
+ sys_mod. set_item ( "ps1" , ctx. new_str ( ">>>>> " . to_string ( ) ) ) ;
59
+ sys_mod. set_item ( "ps2" , ctx. new_str ( "..... " . to_string ( ) ) ) ;
38
60
sys_mod. set_item ( "_getframe" , ctx. new_rustfunc ( getframe) ) ;
39
61
sys_mod
40
62
}
0 commit comments