Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49f8552

Browse files
committed
Implement sys.getrefcount, sys.ps1, sys.ps2 and sys.getsizeof
1 parent9cba8d2 commit49f8552

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

‎src/main.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clap::{App, Arg};
1212
use rustpython_parser::parser;
1313
use rustpython_vm::obj::objstr;
1414
use rustpython_vm::print_exception;
15-
use rustpython_vm::pyobject::{PyObjectRef,PyResult};
15+
use rustpython_vm::pyobject::{AttributeProtocol,PyObjectRef,PyResult};
1616
use rustpython_vm::VirtualMachine;
1717
use rustpython_vm::{compile, import};
1818
use rustyline::error::ReadlineError;
@@ -167,7 +167,11 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
167167
// Err(_) => ">>>>> ".to_string(),
168168
//};
169169

170-
match rl.readline(">>>>> "){
170+
// We can customize the prompt:
171+
let ps1 = objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
172+
let ps2 = objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
173+
174+
match rl.readline(&ps1){
171175
Ok(line) =>{
172176
input.push_str(&line);
173177
input.push_str("\n");
@@ -184,7 +188,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
184188
// Ok(value) => objstr::get_value(&value),
185189
// Err(_) => "..... ".to_string(),
186190
//};
187-
match rl.readline("..... "){
191+
match rl.readline(&ps2){
188192
Ok(line) =>{
189193
if line.len() ==0{
190194
ifshell_exec(vm,&input, vars.clone()){

‎vm/src/sysmodule.rs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
usesuper::pyobject::{DictProtocol,PyContext,PyFuncArgs,PyObjectRef,PyResult};
2-
usesuper::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;
47

58
/*
69
* The magic sys module.
@@ -20,6 +23,19 @@ fn getframe(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
2023
}
2124
}
2225

26+
fnsys_getrefcount(vm:&mutVirtualMachine,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+
fnsys_getsizeof(vm:&mutVirtualMachine,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+
2339
pubfnmk_module(ctx:&PyContext) ->PyObjectRef{
2440
let path_list =match env::var_os("PYTHONPATH"){
2541
Some(paths) => env::split_paths(&paths)
@@ -34,7 +50,13 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
3450
modules.set_item(&sys_name, sys_mod.clone());
3551
sys_mod.set_item("modules", modules);
3652
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);
3757
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()));
3860
sys_mod.set_item("_getframe", ctx.new_rustfunc(getframe));
3961
sys_mod
4062
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp