Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => "",
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
});
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl PyBaseObject {
#[pymethod(magic)]
fn format(obj: PyObjectRef, format_spec: PyStrRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
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__",
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/pybool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl PyBool {
#[pymethod(magic)]
fn format(obj: PyObjectRef, format_spec: PyStrRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
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()))
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/pystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl Constructor for PyStr {
vm,
)?
} else {
vm.to_str(&input)?
input.str(vm)?
}
}
OptionalArg::Missing => {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<element str() failed>").into_ref(vm))
} else {
vm.to_repr(&varargs[0])
Expand Down
2 changes: 1 addition & 1 deletion vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ fn call_object_format(
format_spec: &str,
) -> PyResult<PyStrRef> {
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", ())?,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -104,8 +105,14 @@ impl PyObjectRef {
Ok(ascii)
}

// Container of the virtual machine state:
pub fn str(&self, vm: &VirtualMachine) -> PyResult<PyStrRef> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ mod builtins {
write(sep.clone())?;
}

write(vm.to_str(&object)?)?;
write(object.str(vm)?)?;
}

let end = options
Expand Down
10 changes: 0 additions & 10 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PyStrRef> {
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<PyStrRef> {
self.with_recursion("while getting the repr of an object", || {
let repr = self.call_special_method(obj.clone(), "__repr__", ())?;
Expand Down
4 changes: 2 additions & 2 deletions wasm/lib/src/browser_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
}
Expand Down