mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Ints aren't mutable, change int.__init__ to int.__new__
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use super::super::pyobject::{
|
||||
AttributeProtocol, FromPyObjectRef, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
|
||||
TypeProtocol,
|
||||
AttributeProtocol, FromPyObjectRef, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
|
||||
PyResult, TypeProtocol,
|
||||
};
|
||||
use super::super::vm::VirtualMachine;
|
||||
use super::objfloat;
|
||||
@@ -12,22 +12,16 @@ fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
Ok(vm.new_str(v.to_string()))
|
||||
}
|
||||
|
||||
// __init__ (store value into objectkind)
|
||||
fn int_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(
|
||||
vm,
|
||||
args,
|
||||
required = [(zelf, Some(vm.ctx.int_type())), (arg, None)]
|
||||
);
|
||||
|
||||
// Try to cast to int:
|
||||
let val = match to_int(vm, arg) {
|
||||
Ok(val) => val,
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
|
||||
set_value(zelf, val);
|
||||
Ok(vm.get_none())
|
||||
fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
let ref cls = args.args[0];
|
||||
if !objtype::issubclass(cls.clone(), 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())?;
|
||||
Ok(PyObject::new(
|
||||
PyObjectKind::Integer { value: val },
|
||||
cls.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
// Casting function:
|
||||
@@ -51,10 +45,6 @@ pub fn get_value(obj: &PyObjectRef) -> i32 {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_value(obj: &PyObjectRef, value: i32) {
|
||||
obj.borrow_mut().kind = PyObjectKind::Integer { value };
|
||||
}
|
||||
|
||||
impl FromPyObjectRef for i32 {
|
||||
fn from_pyobj(obj: &PyObjectRef) -> i32 {
|
||||
get_value(obj)
|
||||
@@ -224,7 +214,7 @@ pub fn init(context: &PyContext) {
|
||||
int_type.set_attr("__eq__", context.new_rustfunc(int_eq));
|
||||
int_type.set_attr("__add__", context.new_rustfunc(int_add));
|
||||
int_type.set_attr("__and__", context.new_rustfunc(int_and));
|
||||
int_type.set_attr("__init__", context.new_rustfunc(int_init));
|
||||
int_type.set_attr("__new__", context.new_rustfunc(int_new));
|
||||
int_type.set_attr("__mod__", context.new_rustfunc(int_mod));
|
||||
int_type.set_attr("__mul__", context.new_rustfunc(int_mul));
|
||||
int_type.set_attr("__or__", context.new_rustfunc(int_or));
|
||||
|
||||
Reference in New Issue
Block a user