Merge pull request #604 from RustPython/unit-payload

Unit payload
This commit is contained in:
Adam
2019-03-07 08:17:43 +00:00
committed by GitHub
7 changed files with 50 additions and 32 deletions

View File

@@ -14,7 +14,7 @@ impl IntoPyObject for bool {
}
}
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObjectRef> {
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
if let Some(s) = obj.payload::<PyString>() {
return Ok(!s.value.is_empty());
}
@@ -27,7 +27,6 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
let result = match obj.payload {
PyObjectPayload::Integer { ref value } => !value.is_zero(),
PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(),
PyObjectPayload::None { .. } => false,
_ => {
if let Ok(f) = vm.get_method(obj.clone(), "__bool__") {
let bool_res = vm.invoke(f, PyFuncArgs::default())?;

View File

@@ -5,6 +5,7 @@ pub fn init(context: &PyContext) {
let none_type = &context.none.typ();
context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new));
context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr));
context.set_attr(&none_type, "__bool__", context.new_rustfunc(none_bool));
}
fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -20,3 +21,8 @@ fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
Ok(vm.ctx.new_str("None".to_string()))
}
fn none_bool(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
Ok(vm.ctx.new_bool(false))
}