Skip to content
Merged
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
6 changes: 3 additions & 3 deletions derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
// spell-checker:ignore initproc
let init_method = match init {
Some(init_def) => 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! {
Expand Down Expand Up @@ -499,8 +499,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
}

#[pyslot]
#[pymethod(magic)]
pub(crate) fn init(
#[pymethod(name="__init__")]
pub(crate) fn slot_init(
zelf: PyObjectRef,
args: ::rustpython_vm::function::FuncArgs,
vm: &::rustpython_vm::VirtualMachine,
Expand Down
41 changes: 23 additions & 18 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
py_io::{self, Write},
stdlib::sys,
suggestion::offer_suggestions,
types::Callable,
types::{Callable, Constructor, Initializer},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
Expand Down Expand Up @@ -414,7 +414,7 @@ macro_rules! extend_exception {
};
}

#[pyclass(flags(BASETYPE, HAS_DICT))]
#[pyclass(with(Constructor, Initializer), flags(BASETYPE, HAS_DICT))]
impl PyBaseException {
pub(crate) fn new(args: Vec<PyObjectRef>, vm: &VirtualMachine) -> PyBaseException {
PyBaseException {
Expand All @@ -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<Self> = zelf.try_into_value(vm)?;
*zelf.args.write() = PyTuple::new_ref(args.args, &vm.ctx);
Ok(())
}

pub fn get_arg(&self, idx: usize) -> Option<PyObjectRef> {
self.args.read().get(idx).cloned()
}
Expand Down Expand Up @@ -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<Self>, 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::*;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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! {
Expand Down