Implement no-args int() construction

This commit is contained in:
Daniel Watkins
2018-09-02 17:00:35 -04:00
parent 82226bfea3
commit 10f0ef33d6
2 changed files with 11 additions and 2 deletions

View File

@@ -33,3 +33,4 @@ try:
except TypeError:
pass
assert int() == 0

View File

@@ -13,11 +13,19 @@ fn int_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let ref cls = args.args[0];
arg_check!(
vm,
args,
required = [(cls, None)],
optional = [(val_option, None)]
);
if !objtype::issubclass(cls, vm.ctx.int_type()) {
return Err(vm.new_type_error(format!("{:?} is not a subtype of int", cls)));
}
let val = to_int(vm, &args.args[1].clone())?;
let val = match val_option {
Some(val) => to_int(vm, val)?,
None => 0,
};
Ok(PyObject::new(
PyObjectKind::Integer { value: val },
cls.clone(),