diff --git a/derive-impl/src/pyclass.rs b/derive-impl/src/pyclass.rs index cd5bbc9a4..182286e5b 100644 --- a/derive-impl/src/pyclass.rs +++ b/derive-impl/src/pyclass.rs @@ -471,7 +471,7 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result quote! { #init_def(zelf, args, vm) }, - None => quote! { #base_class::init(zelf, args, vm) }, + None => quote! { #base_class::slot_init(zelf, args, vm) }, }; let ret = quote! { @@ -499,8 +499,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result, vm: &VirtualMachine) -> PyBaseException { PyBaseException { @@ -426,21 +426,6 @@ impl PyBaseException { } } - #[pyslot] - pub(crate) fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { - PyBaseException::new(args.args, vm) - .into_ref_with_type(vm, cls) - .map(Into::into) - } - - #[pyslot] - #[pymethod(magic)] - pub(crate) fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> { - let zelf: PyRef = zelf.try_into_value(vm)?; - *zelf.args.write() = PyTuple::new_ref(args.args, &vm.ctx); - Ok(()) - } - pub fn get_arg(&self, idx: usize) -> Option { self.args.read().get(idx).cloned() } @@ -532,6 +517,25 @@ impl PyBaseException { } } +impl Constructor for PyBaseException { + type Args = FuncArgs; + + fn py_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { + PyBaseException::new(args.args, vm) + .into_ref_with_type(vm, cls) + .map(Into::into) + } +} + +impl Initializer for PyBaseException { + type Args = FuncArgs; + + fn init(zelf: PyRef, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> { + *zelf.args.write() = PyTuple::new_ref(args.args, &vm.ctx); + Ok(()) + } +} + impl ExceptionZoo { pub(crate) fn init() -> Self { use self::types::*; @@ -1119,6 +1123,7 @@ pub(super) mod types { builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef}, convert::ToPyResult, function::FuncArgs, + types::{Constructor, Initializer}, PyObjectRef, PyRef, PyResult, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; @@ -1371,7 +1376,7 @@ pub(super) mod types { new_args.args.truncate(2); } - PyBaseException::init(zelf, new_args, vm) + PyBaseException::slot_init(zelf, new_args, vm) } define_exception! {