Skip to content
Next Next commit
Make new_X_error recv impl Into<String>
  • Loading branch information
ShaharNaveh committed Jun 23, 2025
commit e7923f6c75a57d5d6172b7efc10fe1172785478d
27 changes: 13 additions & 14 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ mod array {
.chars()
.exactly_one()
.map(|ch| Self(ch as _))
.map_err(|_| vm.new_type_error("array item must be unicode character".into()))
.map_err(|_| vm.new_type_error("array item must be unicode character"))
}
fn byteswap(self) -> Self {
Self(self.0.swap_bytes())
Expand Down Expand Up @@ -832,9 +832,9 @@ mod array {
))
})?;
if zelf.read().typecode() != 'u' {
return Err(vm.new_value_error(
"fromunicode() may only be called on unicode type arrays".into(),
));
return Err(
vm.new_value_error("fromunicode() may only be called on unicode type arrays")
);
}
let mut w = zelf.try_resizable(vm)?;
let bytes = Self::_unicode_to_wchar_bytes(wtf8, w.itemsize());
Expand All @@ -846,9 +846,9 @@ mod array {
fn tounicode(&self, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
let array = self.array.read();
if array.typecode() != 'u' {
return Err(vm.new_value_error(
"tounicode() may only be called on unicode type arrays".into(),
));
return Err(
vm.new_value_error("tounicode() may only be called on unicode type arrays")
);
}
let bytes = array.get_bytes();
Self::_wchar_bytes_to_string(bytes, self.itemsize(), vm)
Expand Down Expand Up @@ -1500,7 +1500,7 @@ mod array {
.unwrap_or(u8::MAX)
.try_into()
.map_err(|_| {
vm.new_value_error("third argument must be a valid machine format code.".into())
vm.new_value_error("third argument must be a valid machine format code.")
})
}
}
Expand Down Expand Up @@ -1572,11 +1572,11 @@ mod array {
fn check_type_code(spec: PyStrRef, vm: &VirtualMachine) -> PyResult<ArrayContentType> {
let spec = spec.as_str().chars().exactly_one().map_err(|_| {
vm.new_type_error(
"_array_reconstructor() argument 2 must be a unicode character, not str".into(),
"_array_reconstructor() argument 2 must be a unicode character, not str",
)
})?;
ArrayContentType::from_char(spec)
.map_err(|_| vm.new_value_error("second argument must be a valid type code".into()))
.map_err(|_| vm.new_value_error("second argument must be a valid type code"))
}

macro_rules! chunk_to_obj {
Expand Down Expand Up @@ -1609,7 +1609,7 @@ mod array {
let format = args.mformat_code;
let bytes = args.items.as_bytes();
if bytes.len() % format.item_size() != 0 {
return Err(vm.new_value_error("bytes length not a multiple of item size".into()));
return Err(vm.new_value_error("bytes length not a multiple of item size"));
}
if MachineFormatCode::from_typecode(array.typecode()) == Some(format) {
array.frombytes(bytes);
Expand Down Expand Up @@ -1642,9 +1642,8 @@ mod array {
})?,
MachineFormatCode::Utf16 { big_endian } => {
let utf16: Vec<_> = chunks.map(|b| chunk_to_obj!(b, u16, big_endian)).collect();
let s = String::from_utf16(&utf16).map_err(|_| {
vm.new_unicode_encode_error("items cannot decode as utf16".into())
})?;
let s = String::from_utf16(&utf16)
.map_err(|_| vm.new_unicode_encode_error("items cannot decode as utf16"))?;
let bytes = PyArray::_unicode_to_wchar_bytes((*s).as_ref(), array.itemsize());
array.frombytes_move(bytes);
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub mod _hashlib {

#[pyslot]
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("cannot create '_hashlib.HASH' instances".into()))
Err(vm.new_type_error("cannot create '_hashlib.HASH' instances"))
}

#[pygetset]
Expand Down Expand Up @@ -171,7 +171,7 @@ pub mod _hashlib {

#[pyslot]
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("cannot create '_hashlib.HASHXOF' instances".into()))
Err(vm.new_type_error("cannot create '_hashlib.HASHXOF' instances"))
}

#[pygetset]
Expand Down Expand Up @@ -337,7 +337,7 @@ pub mod _hashlib {

#[pyfunction]
fn hmac_new(_args: NewHMACHashArgs, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
Err(vm.new_type_error("cannot create 'hmac' instances".into())) // TODO: RUSTPYTHON support hmac
Err(vm.new_type_error("cannot create 'hmac' instances")) // TODO: RUSTPYTHON support hmac
}

pub trait ThreadSafeDynDigest: DynClone + DynDigest + Sync + Send {}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Representable for PyClassMethod {
.map(|n| n.as_str()),
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
) {
(None, _) => return Err(vm.new_type_error("Unknown qualified name".into())),
(None, _) => return Err(vm.new_type_error("Unknown qualified name")),
(Some(qualname), Some(module)) if module != "builtins" => {
format!("<{module}.{qualname}({callable})>")
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Representable for PyModule {
let module_repr = importlib.get_attr("_module_repr", vm)?;
let repr = module_repr.call((zelf.to_owned(),), vm)?;
repr.downcast()
.map_err(|_| vm.new_type_error("_module_repr did not return a string".into()))
.map_err(|_| vm.new_type_error("_module_repr did not return a string"))
}

#[cold]
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 @@ -350,7 +350,7 @@ impl PyBaseObject {
.map(|n| n.as_str()),
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
) {
(None, _) => Err(vm.new_type_error("Unknown qualified name".into())),
(None, _) => Err(vm.new_type_error("Unknown qualified name")),
(Some(qualname), Some(module)) if module != "builtins" => Ok(PyStr::from(format!(
"<{}.{} object at {:#x}>",
module,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/staticmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Representable for PyStaticMethod {
.map(|n| n.as_str()),
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
) {
(None, _) => Err(vm.new_type_error("Unknown qualified name".into())),
(None, _) => Err(vm.new_type_error("Unknown qualified name")),
(Some(qualname), Some(module)) if module != "builtins" => {
Ok(format!("<{module}.{qualname}({callable})>"))
}
Expand Down
5 changes: 3 additions & 2 deletions vm/src/vm/vm_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ macro_rules! define_exception_fn {
stringify!($python_repr),
" object.\nUseful for raising errors from python functions implemented in rust."
)]
pub fn $fn_name(&self, msg: String) -> PyBaseExceptionRef {
pub fn $fn_name(&self, msg: impl Into<String>) -> PyBaseExceptionRef
{
let err = self.ctx.exceptions.$attr.to_owned();
self.new_exception_msg(err, msg)
self.new_exception_msg(err, msg.into())
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion wasm/lib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &PyBaseExceptionRef) -> JsV

pub fn js_py_typeerror(vm: &VirtualMachine, js_err: JsValue) -> PyBaseExceptionRef {
let msg = js_err.unchecked_into::<js_sys::Error>().to_string();
vm.new_type_error(msg.into())
vm.new_type_error(msg)
}

pub fn js_err_to_py_err(vm: &VirtualMachine, js_err: &JsValue) -> PyBaseExceptionRef {
Expand Down
20 changes: 6 additions & 14 deletions wasm/lib/src/js_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ mod _js {
} else if proto.value.is_null() {
Object::create(proto.value.unchecked_ref())
} else {
return Err(
vm.new_value_error("prototype must be an Object or null".to_owned())
);
return Err(vm.new_value_error("prototype must be an Object or null"));
}
} else {
Object::new()
Expand Down Expand Up @@ -184,7 +182,7 @@ mod _js {
let func = self
.value
.dyn_ref::<js_sys::Function>()
.ok_or_else(|| vm.new_type_error("JS value is not callable".to_owned()))?;
.ok_or_else(|| vm.new_type_error("JS value is not callable"))?;
let js_args = args.iter().map(|x| -> &PyJsValue { x }).collect::<Array>();
let res = match opts.this {
Some(this) => Reflect::apply(func, &this.value, &js_args),
Expand Down Expand Up @@ -216,7 +214,7 @@ mod _js {
let ctor = self
.value
.dyn_ref::<js_sys::Function>()
.ok_or_else(|| vm.new_type_error("JS value is not callable".to_owned()))?;
.ok_or_else(|| vm.new_type_error("JS value is not callable"))?;
let proto = opts
.prototype
.as_ref()
Expand Down Expand Up @@ -361,9 +359,7 @@ mod _js {
#[pymethod]
fn destroy(&self, vm: &VirtualMachine) -> PyResult<()> {
let (closure, _) = self.closure.replace(None).ok_or_else(|| {
vm.new_value_error(
"can't destroy closure has already been destroyed or detached".to_owned(),
)
vm.new_value_error("can't destroy closure has already been destroyed or detached")
})?;
drop(closure);
self.destroyed.set(true);
Expand All @@ -372,9 +368,7 @@ mod _js {
#[pymethod]
fn detach(&self, vm: &VirtualMachine) -> PyResult<PyJsValueRef> {
let (closure, js_val) = self.closure.replace(None).ok_or_else(|| {
vm.new_value_error(
"can't detach closure has already been detached or destroyed".to_owned(),
)
vm.new_value_error("can't detach closure has already been detached or destroyed")
})?;
closure.forget();
self.detached.set(true);
Expand Down Expand Up @@ -574,9 +568,7 @@ mod _js {
match self.obj.take() {
Some(prom) => {
if val.is_some() {
Err(vm.new_type_error(
"can't send non-None value to an AwaitPromise".to_owned(),
))
Err(vm.new_type_error("can't send non-None value to an AwaitPromise"))
} else {
Ok(PyIterReturn::Return(prom))
}
Expand Down