mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #4716 from youknowone/exception
Constructor/Initializer for BaseException
This commit is contained in:
2 files changed
+26
-21
No files matched your search
@@ -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! {
|
||||
@@ -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,
|
||||
|
||||
+23
-18
@@ -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;
|
||||
@@ -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 {
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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::*;
|
||||
@@ -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! {
|
||||
|
||||
Reference in new issue
Block a user