Initializer for PyProperty

This commit is contained in:
Jeong Yunwon
2022-04-27 01:47:07 +09:00
parent 0ee3e7ee2b
commit d4fd303584

View File

@@ -4,8 +4,10 @@
use super::PyTypeRef;
use crate::common::lock::PyRwLock;
use crate::{
class::PyClassImpl, function::FuncArgs, types::GetDescriptor, AsObject, Context, PyObjectRef,
PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
class::PyClassImpl,
function::FuncArgs,
types::{Constructor, GetDescriptor, Initializer},
AsObject, Context, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
/// Property attribute.
@@ -56,7 +58,7 @@ impl PyPayload for PyProperty {
}
#[derive(FromArgs)]
struct PropertyArgs {
pub struct PropertyArgs {
#[pyarg(any, default)]
fget: Option<PyObjectRef>,
#[pyarg(any, default)]
@@ -85,34 +87,8 @@ impl GetDescriptor for PyProperty {
}
}
#[pyimpl(with(GetDescriptor), flags(BASETYPE))]
#[pyimpl(with(Constructor, Initializer, GetDescriptor), flags(BASETYPE))]
impl PyProperty {
#[pyslot]
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
PyProperty {
getter: PyRwLock::new(None),
setter: PyRwLock::new(None),
deleter: PyRwLock::new(None),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
}
#[pyslot]
#[pymethod(magic)]
fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
let args: PropertyArgs = args.bind(vm)?;
*zelf.getter.write() = args.fget;
*zelf.setter.write() = args.fset;
*zelf.deleter.write() = args.fdel;
*zelf.doc.write() = args.doc;
Ok(())
}
// Descriptor methods
#[pyslot]
@@ -252,6 +228,33 @@ impl PyProperty {
}
}
impl Constructor for PyProperty {
type Args = FuncArgs;
fn py_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
PyProperty {
getter: PyRwLock::new(None),
setter: PyRwLock::new(None),
deleter: PyRwLock::new(None),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
}
}
impl Initializer for PyProperty {
type Args = PropertyArgs;
fn init(zelf: PyRef<Self>, args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
*zelf.getter.write() = args.fget;
*zelf.setter.write() = args.fset;
*zelf.deleter.write() = args.fdel;
*zelf.doc.write() = args.doc;
Ok(())
}
}
pub(crate) fn init(context: &Context) {
PyProperty::extend_class(context, &context.types.property_type);