X Tutup
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
60 changes: 22 additions & 38 deletions crates/stdlib/src/_asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub(crate) mod _asyncio {
fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
// Future does not accept positional arguments
if !args.args.is_empty() {
return Err(vm.new_type_error("Future() takes no positional arguments".to_string()));
return Err(vm.new_type_error("Future() takes no positional arguments"));
}
// Extract only 'loop' keyword argument
let loop_ = args.kwargs.get("loop").cloned();
Expand Down Expand Up @@ -265,7 +265,7 @@ pub(crate) mod _asyncio {
#[pymethod]
fn set_result(zelf: PyRef<Self>, result: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
if zelf.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
if zelf.fut_state.load() != FutureState::Pending {
return Err(new_invalid_state_error(vm, "invalid state"));
Expand All @@ -283,7 +283,7 @@ pub(crate) mod _asyncio {
vm: &VirtualMachine,
) -> PyResult<()> {
if zelf.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
if zelf.fut_state.load() != FutureState::Pending {
return Err(new_invalid_state_error(vm, "invalid state"));
Expand Down Expand Up @@ -336,7 +336,7 @@ pub(crate) mod _asyncio {
vm: &VirtualMachine,
) -> PyResult<()> {
if zelf.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
let ctx = match args.context.flatten() {
Some(c) => c,
Expand Down Expand Up @@ -364,7 +364,7 @@ pub(crate) mod _asyncio {
#[pymethod]
fn remove_done_callback(&self, func: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
if self.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
let mut cleared_callback0 = 0usize;

Expand Down Expand Up @@ -461,7 +461,7 @@ pub(crate) mod _asyncio {
#[pymethod]
fn cancel(zelf: PyRef<Self>, args: CancelArgs, vm: &VirtualMachine) -> PyResult<bool> {
if zelf.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
if zelf.fut_state.load() != FutureState::Pending {
// Clear log_tb even when cancel fails
Expand Down Expand Up @@ -598,9 +598,7 @@ pub(crate) mod _asyncio {
self.fut_blocking.store(v, Ordering::Relaxed);
Ok(())
}
PySetterValue::Delete => {
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
}
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
}
}

Expand Down Expand Up @@ -670,16 +668,12 @@ pub(crate) mod _asyncio {
match value {
PySetterValue::Assign(v) => {
if v {
return Err(vm.new_value_error(
"_log_traceback can only be set to False".to_string(),
));
return Err(vm.new_value_error("_log_traceback can only be set to False"));
}
self.fut_log_tb.store(false, Ordering::Relaxed);
Ok(())
}
PySetterValue::Delete => {
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
}
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
}
}

Expand Down Expand Up @@ -1055,7 +1049,7 @@ pub(crate) mod _asyncio {
// Must be a subclass of BaseException
if !exc_class.fast_issubclass(vm.ctx.exceptions.base_exception_type) {
return Err(vm.new_type_error(
"exceptions must be classes or instances deriving from BaseException, not type".to_string()
"exceptions must be classes or instances deriving from BaseException, not type"
));
}

Expand All @@ -1072,9 +1066,9 @@ pub(crate) mod _asyncio {
if let OptionalArg::Present(ref val) = exc_val
&& !vm.is_none(val)
{
return Err(vm.new_type_error(
"instance exception may not have a separate value".to_string(),
));
return Err(
vm.new_type_error("instance exception may not have a separate value")
);
}
exc_type
} else {
Expand Down Expand Up @@ -1339,7 +1333,7 @@ pub(crate) mod _asyncio {
vm: &VirtualMachine,
) -> PyResult<()> {
if zelf.base.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
let ctx = match args.context.flatten() {
Some(c) => c,
Expand Down Expand Up @@ -1367,7 +1361,7 @@ pub(crate) mod _asyncio {
#[pymethod]
fn remove_done_callback(&self, func: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
if self.base.fut_loop.read().is_none() {
return Err(vm.new_runtime_error("Future object is not initialized.".to_string()));
return Err(vm.new_runtime_error("Future object is not initialized."));
}
let mut cleared_callback0 = 0usize;

Expand Down Expand Up @@ -1686,9 +1680,7 @@ pub(crate) mod _asyncio {
self.base.fut_blocking.store(v, Ordering::Relaxed);
Ok(())
}
PySetterValue::Delete => {
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
}
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
}
}

Expand Down Expand Up @@ -1718,7 +1710,7 @@ pub(crate) mod _asyncio {
Ok(())
}
PySetterValue::Delete => {
Err(vm.new_attribute_error("can't delete _log_destroy_pending".to_owned()))
Err(vm.new_attribute_error("can't delete _log_destroy_pending"))
}
}
}
Expand All @@ -1737,16 +1729,12 @@ pub(crate) mod _asyncio {
match value {
PySetterValue::Assign(v) => {
if v {
return Err(vm.new_value_error(
"_log_traceback can only be set to False".to_string(),
));
return Err(vm.new_value_error("_log_traceback can only be set to False"));
}
self.base.fut_log_tb.store(false, Ordering::Relaxed);
Ok(())
}
PySetterValue::Delete => {
Err(vm.new_attribute_error("cannot delete attribute".to_string()))
}
PySetterValue::Delete => Err(vm.new_attribute_error("cannot delete attribute")),
}
}

Expand Down Expand Up @@ -2532,14 +2520,10 @@ pub(crate) mod _asyncio {
let running_task = vm.asyncio_running_task.borrow();
match running_task.as_ref() {
None => {
return Err(vm.new_runtime_error(
"_leave_task: task is not the current task".to_owned(),
));
return Err(vm.new_runtime_error("_leave_task: task is not the current task"));
}
Some(current) if !current.is(&task) => {
return Err(vm.new_runtime_error(
"_leave_task: task is not the current task".to_owned(),
));
return Err(vm.new_runtime_error("_leave_task: task is not the current task"));
}
_ => {}
}
Expand Down Expand Up @@ -2777,7 +2761,7 @@ pub(crate) mod _asyncio {
.ok_or_else(|| vm.new_attribute_error("CancelledError not found"))?;
exc_type
.downcast()
.map_err(|_| vm.new_type_error("CancelledError is not a type".to_string()))
.map_err(|_| vm.new_type_error("CancelledError is not a type"))
}

fn is_cancelled_error(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/_remote_debugging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod _remote_debugging {
type Args = FuncArgs;

fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
Err(vm.new_not_implemented_error("_remote_debugging is not available".to_owned()))
Err(vm.new_not_implemented_error("_remote_debugging is not available"))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/stdlib/src/_sqlite3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,9 +1512,9 @@ mod _sqlite3 {
let _ = unsafe { self.isolation_level.swap(value) };
Ok(())
}
PySetterValue::Delete => Err(vm.new_attribute_error(
"'isolation_level' attribute cannot be deleted".to_owned(),
)),
PySetterValue::Delete => {
Err(vm.new_attribute_error("'isolation_level' attribute cannot be deleted"))
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions crates/stdlib/src/faulthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ mod decl {

// Install signal handlers
if !faulthandler_enable_internal() {
return Err(vm.new_runtime_error("Failed to enable faulthandler".to_owned()));
return Err(vm.new_runtime_error("Failed to enable faulthandler"));
}

Ok(())
Expand Down Expand Up @@ -802,19 +802,15 @@ mod decl {
// Check if it's an integer (file descriptor)
if let Ok(fd) = f.try_to_value::<i32>(vm) {
if fd < 0 {
return Err(
vm.new_value_error("file is not a valid file descriptor".to_owned())
);
return Err(vm.new_value_error("file is not a valid file descriptor"));
}
return Ok(fd);
}
// Try to get fileno() from file object
let fileno = vm.call_method(&f, "fileno", ())?;
let fd: i32 = fileno.try_to_value(vm)?;
if fd < 0 {
return Err(
vm.new_value_error("file is not a valid file descriptor".to_owned())
);
return Err(vm.new_value_error("file is not a valid file descriptor"));
}
// Try to flush the file
let _ = vm.call_method(&f, "flush", ());
Expand All @@ -824,7 +820,7 @@ mod decl {
// file=None or file not passed: fall back to sys.stderr
let stderr = vm.sys_module.get_attr("stderr", vm)?;
if vm.is_none(&stderr) {
return Err(vm.new_runtime_error("sys.stderr is None".to_owned()));
return Err(vm.new_runtime_error("sys.stderr is None"));
}
let fileno = vm.call_method(&stderr, "fileno", ())?;
let fd: i32 = fileno.try_to_value(vm)?;
Expand Down Expand Up @@ -912,15 +908,15 @@ mod decl {
let timeout: f64 = args.timeout.into_float();

if timeout <= 0.0 {
return Err(vm.new_value_error("timeout must be greater than 0".to_owned()));
return Err(vm.new_value_error("timeout must be greater than 0"));
}

let fd = get_fd_from_file_opt(args.file, vm)?;

// Convert timeout to microseconds
let timeout_us = (timeout * 1_000_000.0) as u64;
if timeout_us == 0 {
return Err(vm.new_value_error("timeout must be greater than 0".to_owned()));
return Err(vm.new_value_error("timeout must be greater than 0"));
}

let header = format_timeout(timeout_us);
Expand Down Expand Up @@ -1098,7 +1094,7 @@ mod decl {

// Check if signal is in valid range
if !(1..64).contains(&signum) {
return Err(vm.new_value_error("signal number out of range".to_owned()));
return Err(vm.new_value_error("signal number out of range"));
}

Ok(())
Expand Down
20 changes: 10 additions & 10 deletions crates/stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ pub mod _hashlib {
(Some(_), Some(_)) => Err(vm.new_type_error(
"'data' and 'string' are mutually exclusive \
and support for 'string' keyword parameter \
is slated for removal in a future version."
.to_owned(),
is slated for removal in a future version.",
)),
}
}
Expand Down Expand Up @@ -306,7 +305,7 @@ pub mod _hashlib {
impl PyHmac {
#[pyslot]
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("cannot create '_hashlib.HMAC' instances".to_owned()))
Err(vm.new_type_error("cannot create '_hashlib.HMAC' instances"))
}

#[pygetset]
Expand Down Expand Up @@ -758,9 +757,10 @@ pub mod _hashlib {

#[pyfunction]
fn hmac_new(args: NewHMACHashArgs, vm: &VirtualMachine) -> PyResult<PyHmac> {
let digestmod = args.digestmod.into_option().ok_or_else(|| {
vm.new_type_error("Missing required parameter 'digestmod'.".to_owned())
})?;
let digestmod = args
.digestmod
.into_option()
.ok_or_else(|| vm.new_type_error("Missing required parameter 'digestmod'."))?;
let name = resolve_digestmod(&digestmod, vm)?;

let key_buf = args.key.borrow_buf();
Expand Down Expand Up @@ -833,10 +833,10 @@ pub mod _hashlib {
let name = args.hash_name.as_str().to_lowercase();

if args.iterations < 1 {
return Err(vm.new_value_error("iteration value must be greater than 0.".to_owned()));
return Err(vm.new_value_error("iteration value must be greater than 0."));
}
let rounds = u32::try_from(args.iterations)
.map_err(|_| vm.new_overflow_error("iteration value is too great.".to_owned()))?;
.map_err(|_| vm.new_overflow_error("iteration value is too great."))?;

let dklen: usize = match args.dklen.into_option() {
Some(obj) if vm.is_none(&obj) => {
Expand All @@ -845,10 +845,10 @@ pub mod _hashlib {
Some(obj) => {
let len: i64 = obj.try_into_value(vm)?;
if len < 1 {
return Err(vm.new_value_error("key length must be greater than 0.".to_owned()));
return Err(vm.new_value_error("key length must be greater than 0."));
}
usize::try_from(len)
.map_err(|_| vm.new_overflow_error("key length is too great.".to_owned()))?
.map_err(|_| vm.new_overflow_error("key length is too great."))?
}
None => hash_digest_size(&name).ok_or_else(|| unsupported_hash(&name, vm))?,
};
Expand Down
8 changes: 4 additions & 4 deletions crates/stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ mod math {
)));
}
if b == 1.0 {
return Err(vm.new_value_error("math domain error".to_owned()));
return Err(vm.new_value_error("math domain error"));
}
}
// Handle BigInt specially for large values (only for actual int type, not float)
if let Some(i) = x.downcast_ref::<PyInt>() {
return pymath::math::log_bigint(i.as_bigint(), base).map_err(|err| match err {
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
_ => pymath_exception(err, vm),
});
}
Expand All @@ -132,7 +132,7 @@ mod math {
// Handle BigInt specially for large values (only for actual int type, not float)
if let Some(i) = x.downcast_ref::<PyInt>() {
return pymath::math::log2_bigint(i.as_bigint()).map_err(|err| match err {
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
_ => pymath_exception(err, vm),
});
}
Expand All @@ -151,7 +151,7 @@ mod math {
// Handle BigInt specially for large values (only for actual int type, not float)
if let Some(i) = x.downcast_ref::<PyInt>() {
return pymath::math::log10_bigint(i.as_bigint()).map_err(|err| match err {
pymath::Error::EDOM => vm.new_value_error("expected a positive input".to_owned()),
pymath::Error::EDOM => vm.new_value_error("expected a positive input"),
_ => pymath_exception(err, vm),
});
}
Expand Down
10 changes: 4 additions & 6 deletions crates/stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,11 @@ mod mmap {
// Parse tagname: None or a string
let tag_str: Option<String> = match tagname {
Some(ref obj) if !vm.is_none(obj) => {
let s = obj.try_to_value::<String>(vm).map_err(|_| {
vm.new_type_error("tagname must be a string or None".to_owned())
})?;
let s = obj
.try_to_value::<String>(vm)
.map_err(|_| vm.new_type_error("tagname must be a string or None"))?;
if s.contains('\0') {
return Err(vm.new_value_error(
"tagname must not contain null characters".to_owned(),
));
return Err(vm.new_value_error("tagname must not contain null characters"));
}
Some(s)
}
Expand Down
Loading
Loading
X Tutup