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
26 changes: 11 additions & 15 deletions crates/capi/src/ceval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ pub extern "C" fn PyEval_GetBuiltins() -> *mut PyObject {
pub extern "C" fn PyEval_GetFrame() -> *mut PyFrameObject {
with_vm(|vm| -> *mut PyObject {
vm.current_frame()
.map(|frame| frame.as_object().as_raw().cast_mut())
.unwrap_or_default()
.map_or_default(|frame| frame.as_object().as_raw().cast_mut())
})
.cast()
}
Expand All @@ -91,17 +90,15 @@ pub extern "C" fn PyEval_GetFrameBuiltins() -> *mut PyObject {
#[unsafe(no_mangle)]
pub extern "C" fn PyEval_GetFrameGlobals() -> *mut PyObject {
with_vm(|vm| {
vm.current_frame()
.map(|frame| {
frame
.iframe()
.globals()
.as_object()
.to_owned()
.into_raw()
.as_ptr()
})
.unwrap_or_default()
vm.current_frame().map_or_default(|frame| {
frame
.iframe()
.globals()
.as_object()
.to_owned()
.into_raw()
.as_ptr()
})
})
}

Expand All @@ -120,8 +117,7 @@ pub extern "C" fn PyEval_GetFrameLocals() -> *mut PyObject {
pub extern "C" fn PyEval_GetGlobals() -> *mut PyObject {
with_vm(|vm| {
vm.current_frame()
.map(|frame| frame.iframe().globals().as_object().as_raw())
.unwrap_or_default()
.map_or_default(|frame| frame.iframe().globals().as_object().as_raw())
})
}

Expand Down
18 changes: 6 additions & 12 deletions crates/capi/src/methodobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,11 @@ unsafe fn call_function<A: Into<FuncArgs>>(

let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|obj| obj.as_object().as_raw().cast_mut());

let arg_ptr = arg_tuple
.as_ref()
.map(|tuple| tuple.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|tuple| tuple.as_object().as_raw().cast_mut());

unsafe { f(slf_ptr, arg_ptr).assume_owned_or_err(vm) }
}
Expand All @@ -205,8 +203,7 @@ unsafe fn call_function_with_keywords(
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|obj| obj.as_object().as_raw().cast_mut());
let (arg_tuple, kwargs) = split_args(vm, args)?;
unsafe {
f(
Expand All @@ -228,8 +225,7 @@ unsafe fn call_fast_function_with_keywords(
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|obj| obj.as_object().as_raw().cast_mut());
let nargs = args.args.len();
let mut fastcall_args = args.args;
let kwnames_tuple = if !args.kwargs.is_empty() {
Expand All @@ -244,8 +240,7 @@ unsafe fn call_fast_function_with_keywords(
};
let kwnames_ptr = kwnames_tuple
.as_ref()
.map(|tuple| tuple.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|tuple| tuple.as_object().as_raw().cast_mut());
// SAFETY: PyObjectRef is repr(transparent) over a pointer to PyObject, so a
// Vec<PyObjectRef> has a layout-compatible contiguous backing buffer. The
// vector is kept alive for the duration of the call.
Expand All @@ -264,8 +259,7 @@ unsafe fn call_fast_function(
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
.map_or_default(|obj| obj.as_object().as_raw().cast_mut());
// SAFETY: PyObjectRef is repr(transparent) over a pointer to PyObject, so a
// Vec<PyObjectRef> has a layout-compatible contiguous backing buffer. The
// vector is kept alive for the duration of the call.
Expand Down
4 changes: 2 additions & 2 deletions crates/capi/src/pycapsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub unsafe extern "C" fn PyCapsule_GetName(capsule: *mut PyObject) -> *const c_c
let capsule = unsafe { capsule.assume_borrowed() }
.downcast_ref_if_exact::<PyCapsule>(vm)
.ok_or_else(|| vm.new_value_error("Invalid capsule"))?;
Ok(capsule.name().map(CStr::as_ptr).unwrap_or_default())
Ok(capsule.name().map_or_default(CStr::as_ptr))
})
}

Expand Down Expand Up @@ -135,7 +135,7 @@ fn checked_capsule<'a>(
.downcast_ref_if_exact::<PyCapsule>(vm)
.ok_or_else(|| vm.new_value_error("Invalid capsule"))?;

if !names_match(capsule.name().map(CStr::as_ptr).unwrap_or_default(), name) {
if !names_match(capsule.name().map_or_default(CStr::as_ptr), name) {
return Err(vm.new_value_error("Capsule name does not match"));
}

Expand Down
15 changes: 5 additions & 10 deletions crates/capi/src/pyerrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,15 @@ define_py_check!(fn PyExceptionInstance_Check, exceptions.base_exception_type);
pub extern "C" fn PyErr_Occurred() -> *mut PyObject {
with_vm(|vm| {
vm.raised_exception()
.map(|exc| exc.class().as_object().as_raw())
.unwrap_or_default()
.map_or_default(|exc| exc.class().as_object().as_raw())
})
}

#[unsafe(no_mangle)]
pub extern "C" fn PyErr_GetRaisedException() -> *mut PyObject {
with_vm(|vm| {
vm.take_raised_exception()
.map(|exc| exc.into_object().into_raw().as_ptr())
.unwrap_or_default()
.map_or_default(|exc| exc.into_object().into_raw().as_ptr())
})
}

Expand Down Expand Up @@ -268,8 +266,7 @@ pub unsafe extern "C" fn PyException_GetTraceback(exc: *mut PyObject) -> *mut Py
let exc = unsafe { exc.assume_borrowed_and_cast::<PyBaseException>(vm) }?;
let tb = exc
.traceback()
.map(|tb| tb.into_object().into_raw().as_ptr())
.unwrap_or_default();
.map_or_default(|tb| tb.into_object().into_raw().as_ptr());
Ok(tb)
})
}
Expand All @@ -280,8 +277,7 @@ pub unsafe extern "C" fn PyException_GetCause(exc: *mut PyObject) -> *mut PyObje
let exc = unsafe { exc.assume_borrowed_and_cast::<PyBaseException>(vm) }?;
let cause = exc
.__cause__()
.map(|cause| cause.into_object().into_raw().as_ptr())
.unwrap_or_default();
.map_or_default(|cause| cause.into_object().into_raw().as_ptr());
Ok(cause)
})
}
Expand All @@ -292,8 +288,7 @@ pub unsafe extern "C" fn PyException_GetContext(exc: *mut PyObject) -> *mut PyOb
let exc = unsafe { exc.assume_borrowed_and_cast::<PyBaseException>(vm) }?;
let context = exc
.__context__()
.map(|context| context.into_object().into_raw().as_ptr())
.unwrap_or_default();
.map_or_default(|context| context.into_object().into_raw().as_ptr());
Ok(context)
})
}
Expand Down
12 changes: 4 additions & 8 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,18 +2107,15 @@ impl<'warnings> Compiler<'warnings> {

// Build freevars using dictbytype (FREE scope, offset by cellvars size)
let mut freevar_cache = IndexSet::default();
let annotation_free_names: IndexSet<Name> = ste
.annotation_block
.as_ref()
.map(|annotation| {
let annotation_free_names: IndexSet<Name> =
ste.annotation_block.as_ref().map_or_default(|annotation| {
annotation
.symbols
.iter()
.filter(|(_, s)| s.scope == SymbolScope::Free)
.map(|(name, _)| name.clone())
.collect()
})
.unwrap_or_default();
});
let mut free_names: Vec<_> = ste
.symbols
.iter()
Expand Down Expand Up @@ -6253,8 +6250,7 @@ impl<'warnings> Compiler<'warnings> {
.unwrap()
.static_attributes
.as_ref()
.map(|s| s.iter().cloned().collect())
.unwrap_or_default();
.map_or_default(|s| s.iter().cloned().collect());
attrs.sort();
self.emit_load_const(ConstantData::Tuple {
elements: attrs
Expand Down
2 changes: 1 addition & 1 deletion crates/host_env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ getrandom = { workspace = true }
libc = { workspace = true }
num-traits = { workspace = true }
parking_lot = { workspace = true }
paste = { workspace = true }
pkcs8 = { workspace = true, features = ["encryption", "pkcs5", "pem"], optional = true }
rustls = { workspace = true, default-features = false, features = ["std", "tls12"], optional = true }
rustls-pemfile = { workspace = true, optional = true }
Expand Down Expand Up @@ -84,6 +83,7 @@ system-configuration = { workspace = true }
object = { workspace = true }

[target.'cfg(windows)'.dependencies]
paste = { workspace = true }
memchr.workspace = true
junction = { workspace = true }
schannel = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/host_env/src/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,7 @@ pub fn read_array_element(
Some("u") => {
let value = wchar_from_bytes(rest)
.and_then(char::from_u32)
.map(|c| c.to_string())
.unwrap_or_default();
.map_or_default(|c| c.to_string());
DecodedValue::String(value)
}
Some("z") => {
Expand Down
4 changes: 1 addition & 3 deletions crates/stdlib/src/_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,9 +1131,7 @@ mod _datetime {
arg: &PyObject,
vm: &VirtualMachine,
) -> PyResult<String> {
Ok(call_utcoffset(tzinfo, arg, vm)?
.map(|offset| format_offset(&offset, sep))
.unwrap_or_default())
Ok(call_utcoffset(tzinfo, arg, vm)?.map_or_default(|offset| format_offset(&offset, sep)))
}

fn append_keyword_tzinfo(
Expand Down
6 changes: 2 additions & 4 deletions crates/stdlib/src/elementtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,8 +2217,7 @@ pub(crate) mod _elementtree {
let info: Vec<PyObjectRef> = expat.get_attr("version_info", vm)?.try_to_value(vm)?;
let part = |i: usize| -> String {
info.get(i)
.map(|o| o.str(vm).map(|s| s.to_string()).unwrap_or_default())
.unwrap_or_default()
.map_or_default(|o| o.str(vm).map_or_default(|s| s.to_string()))
};
Ok(format!("Expat {}.{}.{}", part(0), part(1), part(2)))
}
Expand Down Expand Up @@ -2310,8 +2309,7 @@ pub(crate) mod _elementtree {
let tag = zelf.make_universal(tag, vm)?;
let items = attr_list
.downcast_ref::<PyList>()
.map(|l| l.borrow_vec().to_vec())
.unwrap_or_default();
.map_or_default(|l| l.borrow_vec().to_vec());
let attrib = if items.is_empty() {
None
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ mod _errors {
for (index, &(name, message)) in ERRORS.iter().enumerate() {
let code = vm.ctx.new_int(index + 1);
module.set_attr(name, vm.ctx.new_str(message), vm)?;
codes.set_item(message, code.clone().into(), vm)?;
codes.set_item(message, code.into(), vm)?;
messages.set_item(&(index + 1), vm.ctx.new_str(message).into(), vm)?;
}
extend_module!(vm, module, {
Expand Down
10 changes: 2 additions & 8 deletions crates/unicode/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ impl Ucd {
lookup_table(BIDI_CLASS_DIFF, c)
.or_else(|| membership_3_2(c as u32).then(|| BidiClass::for_char(c)))
};
class
.as_ref()
.map(BidiClass::short_name)
.unwrap_or_default()
class.as_ref().map_or_default(BidiClass::short_name)
}

#[must_use]
Expand Down Expand Up @@ -474,10 +471,7 @@ impl Ucd {
// have to do it manually for now.
let tag = DECOMP_COMPAT[i].1.type_tag();
let end = DECOMP_COMPAT[i].2;
let start = i
.checked_sub(1)
.map(|i| DECOMP_COMPAT[i].2)
.unwrap_or_default();
let start = i.checked_sub(1).map_or_default(|i| DECOMP_COMPAT[i].2);

let decomp = &DECOMP_RANGE[start..end];
let cap = decomp.len() * 10 + decomp.len() + tag.len() + 1;
Expand Down
4 changes: 3 additions & 1 deletion crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ indexmap = { workspace = true }
itertools = { workspace = true }
itoa = { workspace = true }
is-macro = { workspace = true }
jiff = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
malachite-bigint = { workspace = true }
Expand Down Expand Up @@ -93,6 +92,9 @@ widestring = { workspace = true }
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
wasm-bindgen = { workspace = true, optional = true }

[target.'cfg(not(unix))'.dependencies]
jiff = { workspace = true }

[build-dependencies]
glob = { workspace = true }
jiff = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,11 @@ impl PySetInner {
elements
} else {
// We want to remove duplicates in iterable
iterable_set = Self::from_object(iterable.clone(), vm)?;
// Keep the source alive through toggle_entries: its finalizer may mutate the set.
#[allow(clippy::redundant_clone)]
{
iterable_set = Self::from_object(iterable.clone(), vm)?;
}
iterable_set.content.keys_with_hashes()
};
self.toggle_entries(elements, vm)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/vm/src/getpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ pub fn init_path_config(settings: &Settings) -> Paths {
let executable = get_executable_path();
let real_executable = executable
.as_ref()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default();
.map_or_default(|p| p.to_string_lossy().into_owned());

// Step 1: Check for PYTHONEXECUTABLE / __PYVENV_LAUNCHER__
// When set, these are used as sys.executable and when searching for venvs.
Expand Down Expand Up @@ -536,9 +535,7 @@ fn calculate_base_executable(executable: Option<&PathBuf>, home_dir: Option<&Pat
}

// Otherwise, base_executable == executable
executable
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default()
executable.map_or_default(|p| p.to_string_lossy().into_owned())
}

/// Calculate stdlib_dir (sys._stdlib_dir)
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,7 @@ fn calc_package(globals: Option<&PyObject>, vm: &VirtualMachine) -> PyResult<PyS
{
let parent_repr = parent
.repr_utf8(vm)
.map(|s| s.as_str().to_owned())
.unwrap_or_default();
.map_or_default(|s| s.as_str().to_owned());
let msg = format!(
"__package__ != __spec__.parent ('{}' != {})",
pkg_str.as_wtf8(),
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/stdlib/_ast/type_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ fn type_params_from_values(
let runtime_type_params = values.iter().any(Option::is_none).then(|| values.clone());
let type_params = lower_nullable_type_params(&values);
let range = Option::zip(type_params.first(), type_params.last())
.map(|(first, last)| first.range().cover(last.range()))
.unwrap_or_default();
.map_or_default(|(first, last)| first.range().cover(last.range()));
ast::TypeParams {
node_index: Default::default(),
type_params,
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/stdlib/_ctypes/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,7 @@ impl CDataValue {
.as_slice()
.first()
.and_then(|arg| arg.downcast_ref::<PyStr>())
.map(|s| s.to_string())
.unwrap_or_default();
.map_or_default(|s| s.to_string());
vm.new_runtime_error(format!(
"({}) {}: {message}",
proto.name(),
Expand Down
8 changes: 3 additions & 5 deletions crates/vm/src/stdlib/_ctypes/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,16 +975,14 @@ impl Constructor for PyCFuncPtr {
// Check if first argument is a Python callable (callback creation)
if first_arg.is_callable() {
// Get argument types and result type from the class
let (class_argtypes, class_restype, class_checker) = cls
.stg_info_opt()
.map(|info| {
let (class_argtypes, class_restype, class_checker) =
cls.stg_info_opt().map_or_default(|info| {
(
info.argtypes.clone(),
info.restype.clone(),
info.checker.clone(),
)
})
.unwrap_or_default();
});
if class_argtypes.is_none() {
return Err(
vm.new_type_error("cannot construct instance of this class: no argtypes")
Expand Down
Loading
Loading