extend_class for PyNone (#1436)

* extend_class for PyNone

* Apply clippy
This commit is contained in:
Johan
2019-09-30 02:07:42 +09:00
committed by Windel Bouwman
parent e95c23f351
commit dd6ca78c19

View File

@@ -2,10 +2,12 @@ use crate::obj::objproperty::PyPropertyRef;
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::{class_get_attr, class_has_attr, PyClassRef};
use crate::pyobject::{
IntoPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
};
use crate::vm::VirtualMachine;
#[pyclass(name = "none")]
#[derive(Debug)]
pub struct PyNone;
pub type PyNoneRef = PyRef<PyNone>;
@@ -33,15 +35,24 @@ impl<T: IntoPyObject> IntoPyObject for Option<T> {
}
}
#[pyimpl]
impl PyNoneRef {
#[pymethod(name = "__new__")]
fn none_new(_: PyClassRef, vm: &VirtualMachine) -> PyNoneRef {
vm.ctx.none.clone()
}
#[pymethod(name = "__repr__")]
fn repr(self, _vm: &VirtualMachine) -> PyResult<String> {
Ok("None".to_string())
}
#[pymethod(name = "__bool__")]
fn bool(self, _vm: &VirtualMachine) -> PyResult<bool> {
Ok(false)
}
#[pymethod(name = "__getattribute__")]
fn get_attribute(self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
vm_trace!("None.__getattribute__({:?}, {:?})", self, name);
let cls = self.class();
@@ -102,15 +113,6 @@ impl PyNoneRef {
}
}
fn none_new(_: PyClassRef, vm: &VirtualMachine) -> PyNoneRef {
vm.ctx.none.clone()
}
pub fn init(context: &PyContext) {
extend_class!(context, &context.none.class(), {
"__new__" => context.new_rustfunc(none_new),
"__repr__" => context.new_rustfunc(PyNoneRef::repr),
"__bool__" => context.new_rustfunc(PyNoneRef::bool),
"__getattribute__" => context.new_rustfunc(PyNoneRef::get_attribute)
});
PyNoneRef::extend_class(context, &context.none.class());
}