mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Relocate vm.to_str to obj.str
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(_) => "",
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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__",
|
||||
|
||||
@@ -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()))
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ impl Constructor for PyStr {
|
||||
vm,
|
||||
)?
|
||||
} else {
|
||||
vm.to_str(&input)?
|
||||
input.str(vm)?
|
||||
}
|
||||
}
|
||||
OptionalArg::Missing => {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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", ())?,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<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 {
|
||||
|
||||
@@ -662,7 +662,7 @@ mod builtins {
|
||||
write(sep.clone())?;
|
||||
}
|
||||
|
||||
write(vm.to_str(&object)?)?;
|
||||
write(object.str(vm)?)?;
|
||||
}
|
||||
|
||||
let end = options
|
||||
|
||||
10
vm/src/vm.rs
10
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<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__", ())?;
|
||||
|
||||
@@ -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))?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user