Skip to content

Commit 82b8183

Browse files
Merge pull request RustPython#1337 from palaviv/remove-arg-check-2
Convert more modules to new arg style
2 parents 814260e + 64479a0 commit 82b8183

File tree

7 files changed

+190
-219
lines changed

7 files changed

+190
-219
lines changed

vm/src/obj/objfilter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
2-
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
2+
use crate::vm::VirtualMachine;
33

44
use super::objbool;
55
use super::objiter;

vm/src/obj/objlist.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::Range;
66
use num_bigint::{BigInt, ToBigInt};
77
use num_traits::{One, Signed, ToPrimitive, Zero};
88

9-
use crate::function::{OptionalArg, PyFuncArgs};
9+
use crate::function::OptionalArg;
1010
use crate::pyobject::{
1111
IdProtocol, PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
1212
TryFromObject,
@@ -17,8 +17,7 @@ use super::objbool;
1717
//use super::objint;
1818
use super::objiter;
1919
use super::objsequence::{
20-
get_elements_cell, get_elements_list, get_item, seq_equal, seq_ge, seq_gt, seq_le, seq_lt,
21-
seq_mul, SequenceIndex,
20+
get_elements_list, get_item, seq_equal, seq_ge, seq_gt, seq_le, seq_lt, seq_mul, SequenceIndex,
2221
};
2322
use super::objslice::PySliceRef;
2423
use super::objtype;
@@ -99,6 +98,14 @@ impl PyList {
9998
}
10099
}
101100

101+
#[derive(FromArgs)]
102+
struct SortOptions {
103+
#[pyarg(keyword_only, default = "None")]
104+
key: Option<PyObjectRef>,
105+
#[pyarg(keyword_only, default = "false")]
106+
reverse: bool,
107+
}
108+
102109
pub type PyListRef = PyRef<PyList>;
103110

104111
impl PyListRef {
@@ -687,6 +694,21 @@ impl PyListRef {
687694
// then drain (the values to delete should now be contiguous at teh start of the range)
688695
elements.drain(range.start..(range.start + deleted));
689696
}
697+
698+
fn sort(self, options: SortOptions, vm: &VirtualMachine) -> PyResult<()> {
699+
// replace list contents with [] for duration of sort.
700+
// this prevents keyfunc from messing with the list and makes it easy to
701+
// check if it tries to append elements to it.
702+
let mut elements = self.elements.replace(vec![]);
703+
do_sort(vm, &mut elements, options.key, options.reverse)?;
704+
let temp_elements = self.elements.replace(elements);
705+
706+
if !temp_elements.is_empty() {
707+
return Err(vm.new_value_error("list modified during sort".to_string()));
708+
}
709+
710+
Ok(())
711+
}
690712
}
691713

692714
fn list_new(
@@ -768,30 +790,6 @@ fn do_sort(
768790
Ok(())
769791
}
770792

771-
fn list_sort(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
772-
arg_check!(vm, args, required = [(list, Some(vm.ctx.list_type()))]);
773-
let key_func = args.get_optional_kwarg("key");
774-
let reverse = args.get_optional_kwarg("reverse");
775-
let reverse = match reverse {
776-
None => false,
777-
Some(val) => objbool::boolval(vm, val)?,
778-
};
779-
780-
let elements_cell = get_elements_cell(list);
781-
// replace list contents with [] for duration of sort.
782-
// this prevents keyfunc from messing with the list and makes it easy to
783-
// check if it tries to append elements to it.
784-
let mut elements = elements_cell.replace(vec![]);
785-
do_sort(vm, &mut elements, key_func, reverse)?;
786-
let temp_elements = elements_cell.replace(elements);
787-
788-
if !temp_elements.is_empty() {
789-
return Err(vm.new_value_error("list modified during sort".to_string()));
790-
}
791-
792-
Ok(vm.get_none())
793-
}
794-
795793
#[pyclass]
796794
#[derive(Debug)]
797795
pub struct PyListIterator {
@@ -896,7 +894,7 @@ pub fn init(context: &PyContext) {
896894
"index" => context.new_rustfunc(PyListRef::index),
897895
"insert" => context.new_rustfunc(PyListRef::insert),
898896
"reverse" => context.new_rustfunc(PyListRef::reverse),
899-
"sort" => context.new_rustfunc(list_sort),
897+
"sort" => context.new_rustfunc(PyListRef::sort),
900898
"pop" => context.new_rustfunc(PyListRef::pop),
901899
"remove" => context.new_rustfunc(PyListRef::remove)
902900
});

vm/src/obj/objproperty.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! Python `property` descriptor class.
22
33
*/
4-
use crate::function::{IntoPyNativeFunc, OptionalArg, PyFuncArgs};
4+
use crate::function::{IntoPyNativeFunc, OptionalArg};
55
use crate::obj::objtype::PyClassRef;
66
use crate::pyobject::{
77
IdProtocol, PyClassImpl, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
@@ -91,30 +91,31 @@ impl PyValue for PyProperty {
9191

9292
pub type PyPropertyRef = PyRef<PyProperty>;
9393

94+
#[derive(FromArgs)]
95+
struct PropertyArgs {
96+
#[pyarg(positional_or_keyword, default = "None")]
97+
fget: Option<PyObjectRef>,
98+
#[pyarg(positional_or_keyword, default = "None")]
99+
fset: Option<PyObjectRef>,
100+
#[pyarg(positional_or_keyword, default = "None")]
101+
fdel: Option<PyObjectRef>,
102+
#[pyarg(positional_or_keyword, default = "None")]
103+
doc: Option<PyObjectRef>,
104+
}
105+
94106
#[pyimpl]
95107
impl PyProperty {
96108
#[pymethod(name = "__new__")]
97109
fn new_property(
98110
cls: PyClassRef,
99-
args: PyFuncArgs,
111+
args: PropertyArgs,
100112
vm: &VirtualMachine,
101113
) -> PyResult<PyPropertyRef> {
102-
arg_check!(
103-
vm,
104-
args,
105-
required = [],
106-
optional = [(fget, None), (fset, None), (fdel, None), (doc, None)]
107-
);
108-
109-
fn into_option(vm: &VirtualMachine, arg: Option<&PyObjectRef>) -> Option<PyObjectRef> {
110-
arg.and_then(|arg| py_none_to_option(vm, arg))
111-
}
112-
113114
PyProperty {
114-
getter: into_option(vm, fget),
115-
setter: into_option(vm, fset),
116-
deleter: into_option(vm, fdel),
117-
doc: RefCell::new(into_option(vm, doc)),
115+
getter: args.fget,
116+
setter: args.fset,
117+
deleter: args.fdel,
118+
doc: RefCell::new(args.doc),
118119
}
119120
.into_ref_with_type(vm, cls)
120121
}

0 commit comments

Comments
 (0)