Skip to content

Commit 0fef85a

Browse files
committed
Implement sys.displayhook
1 parent 0d1c6c3 commit 0fef85a

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

src/shell.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustpython_vm::readline::{Readline, ReadlineResult};
66
use rustpython_vm::{
77
exceptions::{print_exception, PyBaseExceptionRef},
88
obj::objtype,
9-
pyobject::{ItemProtocol, PyResult},
9+
pyobject::PyResult,
1010
scope::Scope,
1111
VirtualMachine,
1212
};
@@ -19,19 +19,10 @@ enum ShellExecResult {
1919

2020
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResult {
2121
match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
22-
Ok(code) => {
23-
match vm.run_code_obj(code, scope.clone()) {
24-
Ok(value) => {
25-
// Save non-None values as "_"
26-
if !vm.is_none(&value) {
27-
let key = "_";
28-
scope.globals.set_item(key, value, vm).unwrap();
29-
}
30-
ShellExecResult::Ok
31-
}
32-
Err(err) => ShellExecResult::PyErr(err),
33-
}
34-
}
22+
Ok(code) => match vm.run_code_obj(code, scope.clone()) {
23+
Ok(_val) => ShellExecResult::Ok,
24+
Err(err) => ShellExecResult::PyErr(err),
25+
},
3526
Err(CompileError {
3627
error: CompileErrorType::Parse(ParseErrorType::EOF),
3728
..

vm/src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine
618618
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
619619
}
620620

621-
#[derive(Debug, FromArgs)]
621+
#[derive(Debug, Default, FromArgs)]
622622
pub struct PrintOptions {
623623
#[pyarg(keyword_only, default = "None")]
624624
sep: Option<PyStringRef>,

vm/src/frame.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,10 @@ impl ExecutingFrame<'_> {
647647
bytecode::Instruction::Continue => self.unwind_blocks(vm, UnwindReason::Continue),
648648
bytecode::Instruction::PrintExpr => {
649649
let expr = self.pop_value();
650-
if !expr.is(&vm.get_none()) {
651-
let repr = vm.to_repr(&expr)?;
652-
// TODO: implement sys.displayhook
653-
if let Ok(ref print) = vm.get_attribute(vm.builtins.clone(), "print") {
654-
vm.invoke(print, vec![repr.into_object()])?;
655-
}
656-
}
650+
651+
let displayhook = vm.get_attribute(vm.sys_module.clone(), "displayhook")?;
652+
vm.invoke(&displayhook, vec![expr])?;
653+
657654
Ok(None)
658655
}
659656
bytecode::Instruction::LoadBuildClass => {

vm/src/obj/objbool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn get_py_int(obj: &PyObjectRef) -> &PyInt {
187187
&obj.payload::<PyInt>().unwrap()
188188
}
189189

190-
#[derive(Debug, Copy, Clone, PartialEq)]
190+
#[derive(Debug, Default, Copy, Clone, PartialEq)]
191191
pub struct IntoPyBool {
192192
value: bool,
193193
}

vm/src/sysmodule.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::sync::Arc;
22
use std::{env, mem};
33

4+
use crate::builtins;
45
use crate::frame::FrameRef;
5-
use crate::function::OptionalArg;
6+
use crate::function::{Args, OptionalArg, PyFuncArgs};
67
use crate::obj::objstr::PyStringRef;
78
use crate::pyhash::PyHashInfo;
89
use crate::pyobject::{
@@ -202,6 +203,24 @@ fn sys_exit(code: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
202203
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
203204
}
204205

206+
fn sys_audit(_args: PyFuncArgs) {
207+
// TODO: sys.audit implementation
208+
}
209+
210+
fn sys_displayhook(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
211+
// Save non-None values as "_"
212+
if vm.is_none(&obj) {
213+
return Ok(());
214+
}
215+
// set to none to avoid recursion while printing
216+
vm.set_attr(&vm.builtins, "_", vm.get_none())?;
217+
// TODO: catch encoding errors
218+
let repr = vm.to_repr(&obj)?.into_object();
219+
builtins::builtin_print(Args::new(vec![repr]), Default::default(), vm)?;
220+
vm.set_attr(&vm.builtins, "_", obj)?;
221+
Ok(())
222+
}
223+
205224
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
206225
let ctx = &vm.ctx;
207226

@@ -394,6 +413,9 @@ settrace() -- set the global debug tracing function
394413
"base_exec_prefix" => ctx.new_str(base_exec_prefix.to_owned()),
395414
"exit" => ctx.new_function(sys_exit),
396415
"abiflags" => ctx.new_str("".to_owned()),
416+
"audit" => ctx.new_function(sys_audit),
417+
"displayhook" => ctx.new_function(sys_displayhook),
418+
"__displayhook__" => ctx.new_function(sys_displayhook),
397419
});
398420

399421
modules.set_item("sys", module.clone(), vm).unwrap();

0 commit comments

Comments
 (0)