Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
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
30 changes: 26 additions & 4 deletions vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ impl Representable for PyList {
}
}

use crate::exceptions::types::PyBaseExceptionRef;
use std::cmp::Ordering;
fn do_sort(
vm: &VirtualMachine,
values: &mut Vec<PyObjectRef>,
Expand All @@ -515,20 +517,40 @@ fn do_sort(
} else {
PyComparisonOp::Gt
};
let cmp = |a: &PyObjectRef, b: &PyObjectRef| a.rich_compare_bool(b, op, vm);

// If a PyException is encountered stop swapping elements and store the error.
let mut error_slot: Option<PyBaseExceptionRef> = None;
let mut cmp = |a: &PyObjectRef, b: &PyObjectRef| match a.rich_compare_bool(b, op, vm) {
Ok(res) => {
if res {
Ordering::Greater
} else {
Ordering::Less
}
Comment on lines +525 to +529
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meaning of true/false will be different by op

}
Err(e) => {
error_slot = Some(e);
Ordering::Equal
}
};

if let Some(ref key_func) = key_func {
let mut items = values
.iter()
.map(|x| Ok((x.clone(), key_func.call((x.clone(),), vm)?)))
.collect::<Result<Vec<_>, _>>()?;
timsort::try_sort_by_gt(&mut items, |a, b| cmp(&a.1, &b.1))?;
// timsort::try_sort_by_gt(&mut items, |a, b| cmp(&a.1, &b.1))?;
items.sort_unstable_by(|a, b| cmp(&a.1, &b.1));
*values = items.into_iter().map(|(val, _)| val).collect();
} else {
timsort::try_sort_by_gt(values, cmp)?;
values.sort_unstable_by(cmp);
}

Ok(())
// After the sort is done, check if an error was captured.
match error_slot {
Some(e) => Err(e),
None => Ok(()),
}
}

#[pyclass(module = false, name = "list_iterator", traverse)]
Expand Down
Loading