Skip to content
Draft
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
11 changes: 7 additions & 4 deletions crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ impl PyList {
pub(crate) struct SortOptions {
#[pyarg(named, optional)]
key: Option<PyObjectRef>,
#[pytraverse(skip)]
#[pyarg(named, default)]
reverse: bool,
#[pyarg(named, default, py_default = "False")]
reverse: Option<PyObjectRef>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how about ArgIntoBool? if it works, I prefer to use it.

@jseop-lim jseop-lim Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It works, with one difference: ArgIntoBool converts at bind time, so sorted(None, reverse=X()) runs X.__bool__ before the iterable is read, where CPython raise the TypeError for None first. That ordering is the only reason I took an object and converted inside sort. If you're fine with it I'll switch, it's a three-line change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the order could be kept as well by having sorted() bind its keyword arguments after building the list, but I haven't tried that.

}

pub type PyListRef = PyRef<PyList>;
Expand All @@ -203,6 +202,10 @@ impl PyList {
}

pub(crate) fn sort(&self, options: SortOptions, vm: &VirtualMachine) -> PyResult<()> {
let reverse = match &options.reverse {
Some(reverse) => reverse.try_to_bool(vm)?,
None => false,
};
// replace list contents with [] for duration of sort.
// this prevents keyfunc from messing with the list and makes it easy to
// check if it tries to append elements to it.
Expand All @@ -211,7 +214,7 @@ impl PyList {
let version_before = self.mutation_counter.load(Ordering::Relaxed);
(core::mem::take(&mut *guard), version_before)
};
let res = do_sort(vm, &mut elements, options.key.as_deref(), options.reverse);
let res = do_sort(vm, &mut elements, options.key.as_deref(), reverse);
let mutated = {
let mut guard = self.elements.write();
let mutated = self.mutation_counter.load(Ordering::Relaxed) != version_before;
Expand Down
21 changes: 21 additions & 0 deletions extra_tests/snippets/builtin_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ def __eq__(self, x):
assert sorted(lst, key=lambda x: -x) == lst
assert sorted(lst, key=lambda x: -x, reverse=True) == orig


class Truthy:
def __bool__(self):
return True


assert sorted([1, 2, 3], reverse=Truthy()) == [3, 2, 1]
assert sorted([1, 2, 3], reverse=[]) == [1, 2, 3]
lst = [1, 2, 3]
lst.sort(reverse=Truthy())
assert lst == [3, 2, 1]


class RaisingBool:
def __bool__(self):
raise ZeroDivisionError


assert_raises(ZeroDivisionError, sorted, [1, 2, 3], reverse=RaisingBool())
assert_raises(TypeError, sorted, None, reverse=RaisingBool())

assert sorted([(1, 2, 3), (0, 3, 6)]) == [(0, 3, 6), (1, 2, 3)]
assert sorted([(1, 2, 3), (0, 3, 6)], key=lambda x: x[0]) == [(0, 3, 6), (1, 2, 3)]
assert sorted([(1, 2, 3), (0, 3, 6)], key=lambda x: x[1]) == [(1, 2, 3), (0, 3, 6)]
Expand Down
Loading