mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Implement sys.displayhook
This commit is contained in:
5 files changed
+34
-24
No files matched your search
+5
-14
@@ -6,7 +6,7 @@ use rustpython_vm::readline::{Readline, ReadlineResult};
|
||||
use rustpython_vm::{
|
||||
exceptions::{print_exception, PyBaseExceptionRef},
|
||||
obj::objtype,
|
||||
pyobject::{ItemProtocol, PyResult},
|
||||
pyobject::PyResult,
|
||||
scope::Scope,
|
||||
VirtualMachine,
|
||||
};
|
||||
@@ -19,19 +19,10 @@ enum ShellExecResult {
|
||||
|
||||
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResult {
|
||||
match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
|
||||
Ok(code) => {
|
||||
match vm.run_code_obj(code, scope.clone()) {
|
||||
Ok(value) => {
|
||||
// Save non-None values as "_"
|
||||
if !vm.is_none(&value) {
|
||||
let key = "_";
|
||||
scope.globals.set_item(key, value, vm).unwrap();
|
||||
}
|
||||
ShellExecResult::Ok
|
||||
}
|
||||
Err(err) => ShellExecResult::PyErr(err),
|
||||
}
|
||||
}
|
||||
Ok(code) => match vm.run_code_obj(code, scope.clone()) {
|
||||
Ok(_val) => ShellExecResult::Ok,
|
||||
Err(err) => ShellExecResult::PyErr(err),
|
||||
},
|
||||
Err(CompileError {
|
||||
error: CompileErrorType::Parse(ParseErrorType::EOF),
|
||||
..
|
||||
|
||||
+1
-1
@@ -618,7 +618,7 @@ pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine
|
||||
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
|
||||
}
|
||||
|
||||
#[derive(Debug, FromArgs)]
|
||||
#[derive(Debug, Default, FromArgs)]
|
||||
pub struct PrintOptions {
|
||||
#[pyarg(keyword_only, default = "None")]
|
||||
sep: Option<PyStringRef>,
|
||||
|
||||
+4
-7
@@ -647,13 +647,10 @@ impl ExecutingFrame<'_> {
|
||||
bytecode::Instruction::Continue => self.unwind_blocks(vm, UnwindReason::Continue),
|
||||
bytecode::Instruction::PrintExpr => {
|
||||
let expr = self.pop_value();
|
||||
if !expr.is(&vm.get_none()) {
|
||||
let repr = vm.to_repr(&expr)?;
|
||||
// TODO: implement sys.displayhook
|
||||
if let Ok(ref print) = vm.get_attribute(vm.builtins.clone(), "print") {
|
||||
vm.invoke(print, vec![repr.into_object()])?;
|
||||
}
|
||||
}
|
||||
|
||||
let displayhook = vm.get_attribute(vm.sys_module.clone(), "displayhook")?;
|
||||
vm.invoke(&displayhook, vec![expr])?;
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
bytecode::Instruction::LoadBuildClass => {
|
||||
|
||||
@@ -187,7 +187,7 @@ pub fn get_py_int(obj: &PyObjectRef) -> &PyInt {
|
||||
&obj.payload::<PyInt>().unwrap()
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq)]
|
||||
pub struct IntoPyBool {
|
||||
value: bool,
|
||||
}
|
||||
|
||||
+23
-1
@@ -1,8 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
use std::{env, mem};
|
||||
|
||||
use crate::builtins;
|
||||
use crate::frame::FrameRef;
|
||||
use crate::function::OptionalArg;
|
||||
use crate::function::{Args, OptionalArg, PyFuncArgs};
|
||||
use crate::obj::objstr::PyStringRef;
|
||||
use crate::pyhash::PyHashInfo;
|
||||
use crate::pyobject::{
|
||||
@@ -202,6 +203,24 @@ fn sys_exit(code: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
|
||||
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
|
||||
}
|
||||
|
||||
fn sys_audit(_args: PyFuncArgs) {
|
||||
// TODO: sys.audit implementation
|
||||
}
|
||||
|
||||
fn sys_displayhook(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
// Save non-None values as "_"
|
||||
if vm.is_none(&obj) {
|
||||
return Ok(());
|
||||
}
|
||||
// set to none to avoid recursion while printing
|
||||
vm.set_attr(&vm.builtins, "_", vm.get_none())?;
|
||||
// TODO: catch encoding errors
|
||||
let repr = vm.to_repr(&obj)?.into_object();
|
||||
builtins::builtin_print(Args::new(vec![repr]), Default::default(), vm)?;
|
||||
vm.set_attr(&vm.builtins, "_", obj)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
|
||||
let ctx = &vm.ctx;
|
||||
|
||||
@@ -394,6 +413,9 @@ settrace() -- set the global debug tracing function
|
||||
"base_exec_prefix" => ctx.new_str(base_exec_prefix.to_owned()),
|
||||
"exit" => ctx.new_function(sys_exit),
|
||||
"abiflags" => ctx.new_str("".to_owned()),
|
||||
"audit" => ctx.new_function(sys_audit),
|
||||
"displayhook" => ctx.new_function(sys_displayhook),
|
||||
"__displayhook__" => ctx.new_function(sys_displayhook),
|
||||
});
|
||||
|
||||
modules.set_item("sys", module.clone(), vm).unwrap();
|
||||
|
||||
Reference in new issue
Block a user