From afec714a440d00a1d9a70d33ed8c18fcb32af51f Mon Sep 17 00:00:00 2001 From: silmeth Date: Tue, 9 Jul 2019 10:53:40 +0200 Subject: [PATCH] use vm.call_method() instead of get_method&invoke, remove String alloc --- vm/src/stdlib/io.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index a6a6f2749..88a8fae55 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -467,6 +467,8 @@ fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { } fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { + use std::str::from_utf8; + arg_check!( vm, args, @@ -481,24 +483,19 @@ fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { return Err(vm.new_value_error("not writable".to_string())); } - let write = vm - .get_method(raw.clone(), "write") - .ok_or_else(|| vm.new_attribute_error("BufferedWriter has no write method".to_owned())) - .and_then(|it| it)?; let bytes = objstr::get_value(obj).into_bytes(); - let len = vm.invoke( - write, - PyFuncArgs::new(vec![vm.ctx.new_bytes(bytes.clone())], vec![]), - )?; + let len = vm.call_method(&raw, "write", vec![vm.ctx.new_bytes(bytes.clone())])?; let len = objint::get_value(&len).to_usize().ok_or_else(|| { vm.new_overflow_error("int to large to convert to Rust usize".to_string()) })?; // returns the count of unicode code points written - Ok(vm - .ctx - .new_int(String::from_utf8_lossy(&bytes[0..len]).chars().count())) + let len = from_utf8(&bytes[..len]) + .unwrap_or_else(|e| from_utf8(&bytes[..e.valid_up_to()]).unwrap()) + .chars() + .count(); + Ok(vm.ctx.new_int(len)) } fn split_mode_string(mode_string: String) -> Result<(String, String), String> {