Skip to content

Commit e68dbf6

Browse files
committed
Ints aren't mutable, change int.__init__ to int.__new__
1 parent dd24727 commit e68dbf6

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

vm/src/obj/objint.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, FromPyObjectRef, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
3-
TypeProtocol,
2+
AttributeProtocol, FromPyObjectRef, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
3+
PyResult, TypeProtocol,
44
};
55
use super::super::vm::VirtualMachine;
66
use super::objfloat;
@@ -12,22 +12,16 @@ fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1212
Ok(vm.new_str(v.to_string()))
1313
}
1414

15-
// __init__ (store value into objectkind)
16-
fn int_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
17-
arg_check!(
18-
vm,
19-
args,
20-
required = [(zelf, Some(vm.ctx.int_type())), (arg, None)]
21-
);
22-
23-
// Try to cast to int:
24-
let val = match to_int(vm, arg) {
25-
Ok(val) => val,
26-
Err(err) => return Err(err),
27-
};
28-
29-
set_value(zelf, val);
30-
Ok(vm.get_none())
15+
fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
16+
let ref cls = args.args[0];
17+
if !objtype::issubclass(cls.clone(), vm.ctx.int_type()) {
18+
return Err(vm.new_type_error(format!("{:?} is not a subtype of int", cls)));
19+
}
20+
let val = to_int(vm, &args.args[1].clone())?;
21+
Ok(PyObject::new(
22+
PyObjectKind::Integer { value: val },
23+
cls.clone(),
24+
))
3125
}
3226

3327
// Casting function:
@@ -51,10 +45,6 @@ pub fn get_value(obj: &PyObjectRef) -> i32 {
5145
}
5246
}
5347

54-
fn set_value(obj: &PyObjectRef, value: i32) {
55-
obj.borrow_mut().kind = PyObjectKind::Integer { value };
56-
}
57-
5848
impl FromPyObjectRef for i32 {
5949
fn from_pyobj(obj: &PyObjectRef) -> i32 {
6050
get_value(obj)
@@ -224,7 +214,7 @@ pub fn init(context: &PyContext) {
224214
int_type.set_attr("__eq__", context.new_rustfunc(int_eq));
225215
int_type.set_attr("__add__", context.new_rustfunc(int_add));
226216
int_type.set_attr("__and__", context.new_rustfunc(int_and));
227-
int_type.set_attr("__init__", context.new_rustfunc(int_init));
217+
int_type.set_attr("__new__", context.new_rustfunc(int_new));
228218
int_type.set_attr("__mod__", context.new_rustfunc(int_mod));
229219
int_type.set_attr("__mul__", context.new_rustfunc(int_mul));
230220
int_type.set_attr("__or__", context.new_rustfunc(int_or));

0 commit comments

Comments
 (0)