Skip to content

Commit 3eb687d

Browse files
authored
Merge pull request RustPython#1846 from RustPython/coolreader18/print-sys-stdout
Direct print() to sys.stdout
2 parents 408032a + 2cd42c5 commit 3eb687d

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

vm/src/builtins.rs

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
use std::cell::Cell;
66
use std::char;
7-
use std::io::{self, Write};
87
use std::str;
98

109
use num_bigint::Sign;
@@ -618,6 +617,11 @@ fn builtin_pow(
618617
}
619618
}
620619

620+
pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
621+
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
622+
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
623+
}
624+
621625
#[derive(Debug, FromArgs)]
622626
pub struct PrintOptions {
623627
#[pyarg(keyword_only, default = "None")]
@@ -630,48 +634,12 @@ pub struct PrintOptions {
630634
file: Option<PyObjectRef>,
631635
}
632636

633-
trait Printer {
634-
fn write(&mut self, vm: &VirtualMachine, obj: PyStringRef) -> PyResult<()>;
635-
fn flush(&mut self, vm: &VirtualMachine) -> PyResult<()>;
636-
}
637-
638-
impl Printer for &'_ PyObjectRef {
639-
fn write(&mut self, vm: &VirtualMachine, obj: PyStringRef) -> PyResult<()> {
640-
vm.call_method(self, "write", vec![obj.into_object()])?;
641-
Ok(())
642-
}
643-
644-
fn flush(&mut self, vm: &VirtualMachine) -> PyResult<()> {
645-
vm.call_method(self, "flush", vec![])?;
646-
Ok(())
647-
}
648-
}
649-
650-
impl Printer for std::io::StdoutLock<'_> {
651-
fn write(&mut self, _vm: &VirtualMachine, s: PyStringRef) -> PyResult<()> {
652-
write!(self, "{}", s).unwrap();
653-
Ok(())
654-
}
655-
656-
fn flush(&mut self, _vm: &VirtualMachine) -> PyResult<()> {
657-
<Self as io::Write>::flush(self).unwrap();
658-
Ok(())
659-
}
660-
}
661-
662-
pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
663-
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
664-
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
665-
}
666-
667637
pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
668-
let stdout = io::stdout();
669-
670-
let mut printer: Box<dyn Printer> = if let Some(file) = &options.file {
671-
Box::new(file)
672-
} else {
673-
Box::new(stdout.lock())
638+
let file = match options.file {
639+
Some(f) => f,
640+
None => vm.get_attribute(vm.sys_module.clone(), "stdout")?,
674641
};
642+
let write = |obj: PyStringRef| vm.call_method(&file, "write", vec![obj.into_object()]);
675643

676644
let sep = options
677645
.sep
@@ -682,19 +650,19 @@ pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine)
682650
if first {
683651
first = false;
684652
} else {
685-
printer.write(vm, sep.clone())?;
653+
write(sep.clone())?;
686654
}
687655

688-
printer.write(vm, vm.to_str(&object)?)?;
656+
write(vm.to_str(&object)?)?;
689657
}
690658

691659
let end = options
692660
.end
693661
.unwrap_or_else(|| PyString::from("\n").into_ref(vm));
694-
printer.write(vm, end)?;
662+
write(end)?;
695663

696664
if options.flush.to_bool() {
697-
printer.flush(vm)?;
665+
vm.call_method(&file, "flush", vec![])?;
698666
}
699667

700668
Ok(())

0 commit comments

Comments
 (0)