diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index a1903aea4..5637a5879 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -26,27 +26,28 @@ impl PyValue for PyEnumerate { } } -fn enumerate_new( - cls: PyClassRef, - iterable: PyObjectRef, - start: OptionalArg, - vm: &VirtualMachine, -) -> PyResult { - let counter = match start { - OptionalArg::Present(start) => start.as_bigint().clone(), - OptionalArg::Missing => BigInt::zero(), - }; - - let iterator = objiter::get_iter(vm, &iterable)?; - PyEnumerate { - counter: RefCell::new(counter.clone()), - iterator, - } - .into_ref_with_type(vm, cls) -} - #[pyimpl] impl PyEnumerate { + #[pyslot(new)] + fn tp_new( + cls: PyClassRef, + iterable: PyObjectRef, + start: OptionalArg, + vm: &VirtualMachine, + ) -> PyResult { + let counter = match start { + OptionalArg::Present(start) => start.as_bigint().clone(), + OptionalArg::Missing => BigInt::zero(), + }; + + let iterator = objiter::get_iter(vm, &iterable)?; + PyEnumerate { + counter: RefCell::new(counter.clone()), + iterator, + } + .into_ref_with_type(vm, cls) + } + #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { let iterator = &self.iterator; @@ -69,7 +70,4 @@ impl PyEnumerate { pub fn init(context: &PyContext) { PyEnumerate::extend_class(context, &context.types.enumerate_type); - extend_class!(context, &context.types.enumerate_type, { - (slot new) => enumerate_new, - }); } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index 1e2222517..6f7e2c6d4 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -24,23 +24,24 @@ impl PyValue for PyFilter { } } -fn filter_new( - cls: PyClassRef, - function: PyObjectRef, - iterable: PyObjectRef, - vm: &VirtualMachine, -) -> PyResult { - let iterator = objiter::get_iter(vm, &iterable)?; - - PyFilter { - predicate: function.clone(), - iterator, - } - .into_ref_with_type(vm, cls) -} - #[pyimpl] impl PyFilter { + #[pyslot(new)] + fn tp_new( + cls: PyClassRef, + function: PyObjectRef, + iterable: PyObjectRef, + vm: &VirtualMachine, + ) -> PyResult { + let iterator = objiter::get_iter(vm, &iterable)?; + + PyFilter { + predicate: function.clone(), + iterator, + } + .into_ref_with_type(vm, cls) + } + #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { let predicate = &self.predicate; @@ -68,7 +69,4 @@ impl PyFilter { pub fn init(context: &PyContext) { PyFilter::extend_class(context, &context.types.filter_type); - extend_class!(context, &context.types.filter_type, { - (slot new) => filter_new, - }); } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 078496eda..43506b58d 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -181,6 +181,11 @@ fn inner_rshift(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult { #[pyimpl] impl PyInt { + #[pyslot(new)] + fn tp_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResult { + PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls) + } + #[pymethod(name = "__eq__")] fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { if objtype::isinstance(&other, &vm.ctx.int_type()) { @@ -720,10 +725,6 @@ impl IntOptions { } } -fn int_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResult { - PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls) -} - #[derive(FromArgs)] struct IntFromByteArgs { #[pyarg(positional_or_keyword)] @@ -935,7 +936,4 @@ fn get_py_int(obj: &PyObjectRef) -> &PyInt { pub fn init(context: &PyContext) { PyInt::extend_class(context, &context.types.int_type); - extend_class!(context, &context.types.int_type, { - (slot new) => int_new, - }); } diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 655b561ec..45559ad00 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -23,25 +23,26 @@ impl PyValue for PyMap { } } -fn map_new( - cls: PyClassRef, - function: PyObjectRef, - iterables: Args, - vm: &VirtualMachine, -) -> PyResult { - let iterators = iterables - .into_iter() - .map(|iterable| objiter::get_iter(vm, &iterable)) - .collect::, _>>()?; - PyMap { - mapper: function.clone(), - iterators, - } - .into_ref_with_type(vm, cls.clone()) -} - #[pyimpl] impl PyMap { + #[pyslot(new)] + fn tp_new( + cls: PyClassRef, + function: PyObjectRef, + iterables: Args, + vm: &VirtualMachine, + ) -> PyResult { + let iterators = iterables + .into_iter() + .map(|iterable| objiter::get_iter(vm, &iterable)) + .collect::, _>>()?; + PyMap { + mapper: function.clone(), + iterators, + } + .into_ref_with_type(vm, cls.clone()) + } + #[pymethod(name = "__next__")] fn next(&self, vm: &VirtualMachine) -> PyResult { let next_objs = self @@ -62,7 +63,4 @@ impl PyMap { pub fn init(context: &PyContext) { PyMap::extend_class(context, &context.types.map_type); - extend_class!(context, &context.types.map_type, { - (slot new) => map_new, - }); }