Skip to content

Implement property attributes and functions #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
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
15 changes: 15 additions & 0 deletions tests/snippets/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ def foo(self):

with assertRaises(TypeError):
property.__new__(object)


p1 = property("a", "b", "c")

assert p1.fget == "a"
assert p1.fset == "b"
assert p1.fdel == "c"

assert p1.getter(1).fget == 1
assert p1.setter(2).fset == 2
assert p1.deleter(3).fdel == 3

assert p1.getter(None).fget == "a"
assert p1.setter(None).fset == "b"
assert p1.deleter(None).fdel == "c"
9 changes: 9 additions & 0 deletions vm/src/obj/objnone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ impl IntoPyObject for () {
}
}

impl<T: IntoPyObject> IntoPyObject for Option<T> {
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
match self {
Some(x) => x.into_pyobject(ctx),
None => Ok(ctx.none()),
}
}
}

impl PyNoneRef {
fn repr(self, _vm: &mut VirtualMachine) -> PyResult<String> {
Ok("None".to_string())
Expand Down
62 changes: 62 additions & 0 deletions vm/src/obj/objproperty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ impl PyPropertyRef {
)
}

// Descriptor methods

fn get(self, obj: PyObjectRef, _owner: PyClassRef, vm: &mut VirtualMachine) -> PyResult {
if let Some(getter) = self.getter.as_ref() {
vm.invoke(getter.clone(), obj)
Expand All @@ -90,6 +92,58 @@ impl PyPropertyRef {
Err(vm.new_attribute_error("can't delete attribute".to_string()))
}
}

// Access functions

fn fget(self, _vm: &mut VirtualMachine) -> Option<PyObjectRef> {
self.getter.clone()
}

fn fset(self, _vm: &mut VirtualMachine) -> Option<PyObjectRef> {
self.setter.clone()
}

fn fdel(self, _vm: &mut VirtualMachine) -> Option<PyObjectRef> {
self.deleter.clone()
}

// Python builder functions

fn getter(self, getter: Option<PyObjectRef>, vm: &mut VirtualMachine) -> PyResult<Self> {
Self::new_with_type(
vm,
PyProperty {
getter: getter.or_else(|| self.getter.clone()),
setter: self.setter.clone(),
deleter: self.deleter.clone(),
},
self.typ(),
)
}

fn setter(self, setter: Option<PyObjectRef>, vm: &mut VirtualMachine) -> PyResult<Self> {
Self::new_with_type(
vm,
PyProperty {
getter: self.getter.clone(),
setter: setter.or_else(|| self.setter.clone()),
deleter: self.deleter.clone(),
},
self.typ(),
)
}

fn deleter(self, deleter: Option<PyObjectRef>, vm: &mut VirtualMachine) -> PyResult<Self> {
Self::new_with_type(
vm,
PyProperty {
getter: self.getter.clone(),
setter: self.setter.clone(),
deleter: deleter.or_else(|| self.deleter.clone()),
},
self.typ(),
)
}
}

pub struct PropertyBuilder<'a, T> {
Expand Down Expand Up @@ -190,5 +244,13 @@ pub fn init(context: &PyContext) {
"__get__" => context.new_rustfunc(PyPropertyRef::get),
"__set__" => context.new_rustfunc(PyPropertyRef::set),
"__delete__" => context.new_rustfunc(PyPropertyRef::delete),

"fget" => context.new_property(PyPropertyRef::fget),
"fset" => context.new_property(PyPropertyRef::fset),
"fdel" => context.new_property(PyPropertyRef::fdel),

"getter" => context.new_rustfunc(PyPropertyRef::getter),
"setter" => context.new_rustfunc(PyPropertyRef::setter),
"deleter" => context.new_rustfunc(PyPropertyRef::deleter),
});
}
23 changes: 17 additions & 6 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ impl<T: PyValue> PyRef<T> {
pub fn into_object(self) -> PyObjectRef {
self.obj
}

pub fn typ(&self) -> PyClassRef {
PyRef {
obj: self.obj.typ(),
_payload: PhantomData,
}
}
}

impl<T> Deref for PyRef<T>
Expand Down Expand Up @@ -1211,6 +1218,16 @@ impl TryFromObject for PyObjectRef {
}
}

impl<T: TryFromObject> TryFromObject for Option<T> {
fn try_from_object(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
if vm.get_none().is(&obj) {
Ok(None)
} else {
T::try_from_object(vm, obj).map(|x| Some(x))
}
}
}

/// A map of keyword arguments to their values.
///
/// A built-in function with a `KwArgs` parameter is analagous to a Python
Expand Down Expand Up @@ -1389,12 +1406,6 @@ where
}
}

// TODO: Allow a built-in function to return an `Option`, i.e.:
//
// impl<T: IntoPyObject> IntoPyObject for Option<T>
//
// Option::None should map to a Python `None`.

// Allows a built-in function to return any built-in object payload without
// explicitly implementing `IntoPyObject`.
impl<T> IntoPyObject for T
Expand Down