From fdd1baec65aeed372b3530628adfcac64fb46378 Mon Sep 17 00:00:00 2001 From: Aratrik Date: Wed, 20 Oct 2021 10:28:24 +0530 Subject: [PATCH] Relocate vm.to_str to obj.str --- src/lib.rs | 2 +- src/shell.rs | 2 +- stdlib/src/csv.rs | 2 +- vm/src/builtins/list.rs | 4 ++-- vm/src/builtins/object.rs | 2 +- vm/src/builtins/pybool.rs | 2 +- vm/src/builtins/pystr.rs | 2 +- vm/src/cformat.rs | 2 +- vm/src/exceptions.rs | 3 ++- vm/src/format.rs | 2 +- vm/src/frame.rs | 2 +- vm/src/protocol/object.rs | 9 ++++++++- vm/src/stdlib/builtins.rs | 2 +- vm/src/vm.rs | 10 ---------- wasm/lib/src/browser_module.rs | 4 ++-- 15 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 087eaa8c1..fb0199ccd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,7 +121,7 @@ where if vm.is_none(arg) { 0 } else { - if let Ok(s) = vm.to_str(arg) { + if let Ok(s) = arg.str(vm) { eprintln!("{}", s); } 1 diff --git a/src/shell.rs b/src/shell.rs index e605c2115..26c00b8b8 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -59,7 +59,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> { .sys_module .clone() .get_attr(prompt_name, vm) - .and_then(|prompt| vm.to_str(&prompt)); + .and_then(|prompt| prompt.str(vm)); let prompt = match prompt { Ok(ref s) => s.as_str(), Err(_) => "", diff --git a/stdlib/src/csv.rs b/stdlib/src/csv.rs index ac4ada2cf..4b4ce2db1 100644 --- a/stdlib/src/csv.rs +++ b/stdlib/src/csv.rs @@ -286,7 +286,7 @@ mod _csv { ref s @ PyStr => s.as_str().as_bytes(), crate::builtins::PyNone => b"", ref obj => { - stringified = vm.to_str(obj)?; + stringified = obj.str(vm)?; stringified.as_str().as_bytes() } }); diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index 027bca90b..25f1bf156 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -441,7 +441,7 @@ impl PyList { .unwrap_or(sys::MAXSIZE as usize); let index = self.find_equal(&needle, start..stop, vm)?; if index == usize::MAX { - Err(vm.new_value_error(format!("'{}' is not in list", vm.to_str(&needle)?))) + Err(vm.new_value_error(format!("'{}' is not in list", needle.str(vm)?))) } else { Ok(index) } @@ -471,7 +471,7 @@ impl PyList { // defer delete out of borrow Ok(self.borrow_vec_mut().remove(index)) } else { - Err(vm.new_value_error(format!("'{}' is not in list", vm.to_str(&needle)?))) + Err(vm.new_value_error(format!("'{}' is not in list", needle.str(vm)?))) } .map(drop) } diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 0b0f034e8..0089bd8fd 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -227,7 +227,7 @@ impl PyBaseObject { #[pymethod(magic)] fn format(obj: PyObjectRef, format_spec: PyStrRef, vm: &VirtualMachine) -> PyResult { if format_spec.as_str().is_empty() { - vm.to_str(&obj) + obj.str(vm) } else { Err(vm.new_type_error(format!( "unsupported format string passed to {}.__format__", diff --git a/vm/src/builtins/pybool.rs b/vm/src/builtins/pybool.rs index 6a18c5b47..3e20ade25 100644 --- a/vm/src/builtins/pybool.rs +++ b/vm/src/builtins/pybool.rs @@ -119,7 +119,7 @@ impl PyBool { #[pymethod(magic)] fn format(obj: PyObjectRef, format_spec: PyStrRef, vm: &VirtualMachine) -> PyResult { if format_spec.as_str().is_empty() { - vm.to_str(&obj) + obj.str(vm) } else { Err(vm.new_type_error("unsupported format string passed to bool.__format__".to_owned())) } diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 6e7c5bcdc..f589ec4e9 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -296,7 +296,7 @@ impl Constructor for PyStr { vm, )? } else { - vm.to_str(&input)? + input.str(vm)? } } OptionalArg::Missing => { diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 4f2c87f41..50528aede 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -460,7 +460,7 @@ impl CFormatSpec { match &self.format_type { CFormatType::String(preconversor) => { let result = match preconversor { - CFormatPreconversor::Str => vm.to_str(&obj)?, + CFormatPreconversor::Str => obj.str(vm)?, CFormatPreconversor::Repr | CFormatPreconversor::Ascii => vm.to_repr(&obj)?, CFormatPreconversor::Bytes => { return Err(vm.new_value_error( diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 53e733748..9668ba0d7 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -150,7 +150,8 @@ impl VirtualMachine { 0 => vec![], 1 => { let args0_repr = if str_single { - vm.to_str(&varargs[0]) + varargs[0] + .str(vm) .unwrap_or_else(|_| PyStr::from("").into_ref(vm)) } else { vm.to_repr(&varargs[0]) diff --git a/vm/src/format.rs b/vm/src/format.rs index 8815a75cb..43b2d8aa9 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -892,7 +892,7 @@ fn call_object_format( format_spec: &str, ) -> PyResult { let argument = match preconversion_spec.and_then(FormatPreconversor::from_char) { - Some(FormatPreconversor::Str) => vm.to_str(&argument)?.into(), + Some(FormatPreconversor::Str) => argument.str(vm)?.into(), Some(FormatPreconversor::Repr) => vm.to_repr(&argument)?.into(), Some(FormatPreconversor::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(), Some(FormatPreconversor::Bytes) => vm.call_method(&argument, "decode", ())?, diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 5084c2a0b..a9c888436 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -1092,7 +1092,7 @@ impl ExecutingFrame<'_> { use bytecode::ConversionFlag; let value = self.pop_value(); let value = match conversion { - ConversionFlag::Str => vm.to_str(&value)?.into(), + ConversionFlag::Str => value.str(vm)?.into(), ConversionFlag::Repr => vm.to_repr(&value)?.into(), ConversionFlag::Ascii => vm.ctx.new_str(builtins::ascii(value, vm)?).into(), ConversionFlag::None => value, diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 7bcd081ca..965d93797 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -7,6 +7,7 @@ use crate::{ common::{hash::PyHash, str::to_ascii}, function::OptionalArg, protocol::PyIter, + pyobject::IdProtocol, pyref_type_error, types::{Constructor, PyComparisonOp}, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine, @@ -104,8 +105,14 @@ impl PyObjectRef { Ok(ascii) } + // Container of the virtual machine state: pub fn str(&self, vm: &VirtualMachine) -> PyResult { - vm.to_str(self) + if self.class().is(&vm.ctx.types.str_type) { + Ok(self.clone().downcast().unwrap()) + } else { + let s = vm.call_special_method(self.clone(), "__str__", ())?; + s.try_into_value(vm) + } } pub fn bytes(self, vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 83020cb3a..18a8e3830 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -662,7 +662,7 @@ mod builtins { write(sep.clone())?; } - write(vm.to_str(&object)?)?; + write(object.str(vm)?)?; } let end = options diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 6f11bea8f..e303957d0 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -893,16 +893,6 @@ impl VirtualMachine { obj.unwrap_or_else(|| self.ctx.none()) } - // Container of the virtual machine state: - pub fn to_str(&self, obj: &PyObjectRef) -> PyResult { - if obj.class().is(&self.ctx.types.str_type) { - Ok(obj.clone().downcast().unwrap()) - } else { - let s = self.call_special_method(obj.clone(), "__str__", ())?; - s.try_into_value(self) - } - } - pub fn to_repr(&self, obj: &PyObjectRef) -> PyResult { self.with_recursion("while getting the repr of an object", || { let repr = self.call_special_method(obj.clone(), "__repr__", ())?; diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index 5ba9c73ba..9f6b651aa 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -78,8 +78,8 @@ fn browser_fetch(url: PyStrRef, args: FetchArgs, vm: &VirtualMachine) -> PyResul if let Some(headers) = headers { let h = request.headers(); for (key, value) in headers { - let key = vm.to_str(&key)?; - let value = vm.to_str(&value)?; + let key = key.str(vm)?; + let value = value.str(vm)?; h.set(key.as_str(), value.as_str()) .map_err(|err| convert::js_py_typeerror(vm, err))?; }