Skip to content

Commit cdfc676

Browse files
committed
Constructor/Initializer for BaseException
1 parent 4fba939 commit cdfc676

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

derive-impl/src/pyclass.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
471471
// spell-checker:ignore initproc
472472
let init_method = match init {
473473
Some(init_def) => quote! { #init_def(zelf, args, vm) },
474-
None => quote! { #base_class::init(zelf, args, vm) },
474+
None => quote! { #base_class::slot_init(zelf, args, vm) },
475475
};
476476

477477
let ret = quote! {
@@ -499,8 +499,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
499499
}
500500

501501
#[pyslot]
502-
#[pymethod(magic)]
503-
pub(crate) fn init(
502+
#[pymethod(name="__init__")]
503+
pub(crate) fn slot_init(
504504
zelf: PyObjectRef,
505505
args: ::rustpython_vm::function::FuncArgs,
506506
vm: &::rustpython_vm::VirtualMachine,

vm/src/exceptions.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
py_io::{self, Write},
1212
stdlib::sys,
1313
suggestion::offer_suggestions,
14-
types::Callable,
14+
types::{Callable, Constructor, Initializer},
1515
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
1616
};
1717
use crossbeam_utils::atomic::AtomicCell;
@@ -414,7 +414,7 @@ macro_rules! extend_exception {
414414
};
415415
}
416416

417-
#[pyclass(flags(BASETYPE, HAS_DICT))]
417+
#[pyclass(with(Constructor, Initializer), flags(BASETYPE, HAS_DICT))]
418418
impl PyBaseException {
419419
pub(crate) fn new(args: Vec<PyObjectRef>, vm: &VirtualMachine) -> PyBaseException {
420420
PyBaseException {
@@ -426,21 +426,6 @@ impl PyBaseException {
426426
}
427427
}
428428

429-
#[pyslot]
430-
pub(crate) fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
431-
PyBaseException::new(args.args, vm)
432-
.into_ref_with_type(vm, cls)
433-
.map(Into::into)
434-
}
435-
436-
#[pyslot]
437-
#[pymethod(magic)]
438-
pub(crate) fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
439-
let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
440-
*zelf.args.write() = PyTuple::new_ref(args.args, &vm.ctx);
441-
Ok(())
442-
}
443-
444429
pub fn get_arg(&self, idx: usize) -> Option<PyObjectRef> {
445430
self.args.read().get(idx).cloned()
446431
}
@@ -532,6 +517,25 @@ impl PyBaseException {
532517
}
533518
}
534519

520+
impl Constructor for PyBaseException {
521+
type Args = FuncArgs;
522+
523+
fn py_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
524+
PyBaseException::new(args.args, vm)
525+
.into_ref_with_type(vm, cls)
526+
.map(Into::into)
527+
}
528+
}
529+
530+
impl Initializer for PyBaseException {
531+
type Args = FuncArgs;
532+
533+
fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
534+
*zelf.args.write() = PyTuple::new_ref(args.args, &vm.ctx);
535+
Ok(())
536+
}
537+
}
538+
535539
impl ExceptionZoo {
536540
pub(crate) fn init() -> Self {
537541
use self::types::*;
@@ -1119,6 +1123,7 @@ pub(super) mod types {
11191123
builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef},
11201124
convert::ToPyResult,
11211125
function::FuncArgs,
1126+
types::{Constructor, Initializer},
11221127
PyObjectRef, PyRef, PyResult, VirtualMachine,
11231128
};
11241129
use crossbeam_utils::atomic::AtomicCell;
@@ -1371,7 +1376,7 @@ pub(super) mod types {
13711376

13721377
new_args.args.truncate(2);
13731378
}
1374-
PyBaseException::init(zelf, new_args, vm)
1379+
PyBaseException::slot_init(zelf, new_args, vm)
13751380
}
13761381

13771382
define_exception! {

0 commit comments

Comments
 (0)