MCPcopy Index your code
hub / github.com/RustPython/RustPython / sort

Method sort

crates/vm/src/builtins/list.rs:395–418  ·  view source on GitHub ↗
(&self, options: SortOptions, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

393
394 #[pymethod]
395 pub(crate) fn sort(&self, options: SortOptions, vm: &VirtualMachine) -> PyResult<()> {
396 // replace list contents with [] for duration of sort.
397 // this prevents keyfunc from messing with the list and makes it easy to
398 // check if it tries to append elements to it.
399 let (mut elements, version_before) = {
400 let mut guard = self.elements.write();
401 let version_before = self.mutation_counter.load(Ordering::Relaxed);
402 (core::mem::take(guard.deref_mut()), version_before)
403 };
404 let res = do_sort(vm, &mut elements, options.key, options.reverse);
405 let mutated = {
406 let mut guard = self.elements.write();
407 let mutated = self.mutation_counter.load(Ordering::Relaxed) != version_before;
408 core::mem::swap(guard.deref_mut(), &mut elements);
409 mutated
410 };
411 res?;
412
413 if mutated {
414 return Err(vm.new_value_error("list modified during sort"));
415 }
416
417 Ok(())
418 }
419
420 #[pyclassmethod]
421 fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {

Callers 6

builtin_module_namesFunction · 0.45
sortedFunction · 0.45
ast_replaceFunction · 0.45
dirMethod · 0.45
enter_scopeMethod · 0.45
compile_class_bodyMethod · 0.45

Calls 6

takeFunction · 0.85
do_sortFunction · 0.85
ErrClass · 0.50
writeMethod · 0.45
loadMethod · 0.45
deref_mutMethod · 0.45

Tested by

no test coverage detected