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
3 changes: 1 addition & 2 deletions crates/common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,7 @@ impl FieldName {
FieldType::Index(index)
} else if first
.as_str()
.ok()
.is_some_and(|s| s.bytes().all(|b| b.is_ascii_digit()))
.is_ok_and(|s| s.bytes().all(|b| b.is_ascii_digit()))
{
// All-digit segment whose value overflows usize itself.
return Err(FormatParseError::TooManyDecimalDigits);
Expand Down
17 changes: 13 additions & 4 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@ impl TryFrom<&[u8]> for CodeUnit {
}
}

impl TryFrom<[u8; 2]> for CodeUnit {
type Error = MarshalError;

fn try_from(value: [u8; 2]) -> Result<Self, Self::Error> {
Ok(Self::new(value[0].try_into()?, value[1].into()))
}
}

pub struct CodeUnits {
units: UnsafeCell<Box<[CodeUnit]>>,
adaptive_counters: Box<[AtomicU16]>,
Expand Down Expand Up @@ -610,12 +618,13 @@ impl TryFrom<&[u8]> for CodeUnits {
type Error = MarshalError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if !value.len().is_multiple_of(2) {
let (chunks, []) = value.as_chunks::<2>() else {
return Err(Self::Error::InvalidBytecode);
}
};

let units = value
.chunks_exact(2)
let units = chunks
.iter()
.copied()
.map(CodeUnit::try_from)
.collect::<Result<Vec<_>, _>>()?;
Ok(units.into())
Expand Down
4 changes: 3 additions & 1 deletion crates/host_env/src/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,9 @@ pub fn readlink(path: &Path) -> Result<OsString, ReadlinkError> {

let path_slice = &buffer[path_start..path_end];
let mut wide_chars: Vec<u16> = path_slice
.chunks_exact(2)
.as_chunks::<2>()
.0
.iter()
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();

Expand Down
8 changes: 4 additions & 4 deletions crates/stdlib/src/_testconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ mod _testconsole {
let data = &*data;

// Interpret as UTF-16-LE pairs
if !data.len().is_multiple_of(2) {
let (chunks, []) = data.as_chunks::<2>() else {
return Err(vm.new_value_error("buffer must contain UTF-16-LE data (even length)"));
}
let wchars: Vec<u16> = data
.chunks_exact(2)
};
let wchars: Vec<u16> = chunks
.iter()
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
host_testconsole::write_console_input(fd, &wchars).map_err(|e| e.into_pyexception(vm))
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/builtins/interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ impl Constructor for PyInterpolation {
.as_bytes()
.iter()
.exactly_one()
.ok()
.is_some_and(|s| matches!(*s, b's' | b'r' | b'a'));
.is_ok_and(|s| matches!(*s, b's' | b'r' | b'a'));
if !has_flag {
return Err(vm.new_value_error(
"Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'",
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/dict_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static KEYS_VERSION: AtomicU32 = AtomicU32::new(0);
/// unrealistic in practice.
fn next_keys_version() -> u32 {
KEYS_VERSION
.fetch_update(Relaxed, Relaxed, |v| v.checked_add(1))
.try_update(Relaxed, Relaxed, |v| v.checked_add(1))
.map_or(0, |v| v + 1)
}

Expand Down
6 changes: 2 additions & 4 deletions crates/vm/src/stdlib/_sre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ mod _sre {
let mut items = Vec::with_capacity(1);
let v = template.borrow_vec();
let literal = v.first().ok_or_else(err)?.clone();
let trunks = v[1..].chunks_exact(2);

if !trunks.remainder().is_empty() {
let (trunks, []) = v[1..].as_chunks::<2>() else {
return Err(err());
}
};

for trunk in trunks {
let index: usize = trunk[0]
Expand Down
10 changes: 5 additions & 5 deletions crates/vm/src/stdlib/sys/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) {
continue;
}
// Excluded: RESUME, END_FOR, CACHE (and their instrumented variants)
let base = op.to_base().map_or(op, |b| b);
let base = op.to_base().unwrap_or(op);
if matches!(
base,
Instruction::Resume { .. } | Instruction::EndFor | Instruction::Cache
Expand Down Expand Up @@ -387,7 +387,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) {
.skip(first_traceable)
{
let op = unit.op;
let base = op.to_base().map_or(op, |b| b);
let base = op.to_base().unwrap_or(op);
if matches!(base, Instruction::ExtendedArg) {
continue;
}
Expand Down Expand Up @@ -425,7 +425,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) {
let mut instr_idx = first_traceable;
for unit in code.code.instructions[first_traceable..len].iter().copied() {
let (op, arg) = arg_state.get(unit);
let base = op.to_base().map_or(op, |b| b);
let base = op.to_base().unwrap_or(op);

if matches!(base, Instruction::ExtendedArg) || matches!(base, Instruction::Cache) {
instr_idx += 1;
Expand Down Expand Up @@ -460,7 +460,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) {
&& !no_loc_mask.get(target_idx).copied().unwrap_or(false)
{
let target_op = code.code.instructions[target_idx].op;
let target_base = target_op.to_base().map_or(target_op, |b| b);
let target_base = target_op.to_base().unwrap_or(target_op);
// Skip synthetic cleanup targets.
if matches!(target_base, Instruction::PopIter) {
instr_idx += 1;
Expand All @@ -483,7 +483,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) {
&& !no_loc_mask.get(target_idx).copied().unwrap_or(false)
{
let target_op = code.code.instructions[target_idx].op;
let target_base = target_op.to_base().map_or(target_op, |b| b);
let target_base = target_op.to_base().unwrap_or(target_op);
if !matches!(target_base, Instruction::PopIter)
&& let Some((loc, _)) = line_locations.get(target_idx)
&& loc.line.get() > 0
Expand Down
5 changes: 1 addition & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2092,10 +2092,7 @@ impl VirtualMachine {
if exc.class().is(self.ctx.exceptions.attribute_error) {
let exc = exc.as_object();
// Check if this exception was already augmented
let already_set = exc
.get_attr("name", self)
.ok()
.is_some_and(|v| !self.is_none(&v));
let already_set = exc.get_attr("name", self).is_ok_and(|v| !self.is_none(&v));
if already_set {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/vm/vm_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ impl VirtualMachine {
/// Returns true if the file object's `closed` attribute is truthy.
fn file_is_closed(&self, file: &PyObject) -> bool {
file.get_attr("closed", self)
.ok()
.is_some_and(|v| v.try_to_bool(self).unwrap_or(false))
.is_ok_and(|v| v.try_to_bool(self).unwrap_or_default())
}

pub(crate) fn flush_std(&self) -> i32 {
Expand Down
Loading