Skip to content

Commit 972b77a

Browse files
Merge pull request RustPython#122 from RustPython/int_new
Int new
2 parents 514fea8 + ec0eb47 commit 972b77a

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

tests/snippets/numbers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
x = 5
2+
x.__init__(6)
3+
assert x == 5
4+
5+
class A(int):
6+
pass
7+
8+
x = A(7)
9+
assert x == 7
10+
assert type(x) is A

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));

vm/src/obj/objobject.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ fn object_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5959
pub fn init(context: &PyContext) {
6060
let ref object = context.object;
6161
object.set_attr("__new__", context.new_rustfunc(new_instance));
62+
object.set_attr("__init__", context.new_rustfunc(object_init));
6263
object.set_attr("__eq__", context.new_rustfunc(object_eq));
6364
object.set_attr("__ne__", context.new_rustfunc(object_ne));
6465
object.set_attr("__dict__", context.new_member_descriptor(object_dict));
6566
object.set_attr("__str__", context.new_rustfunc(object_str));
6667
}
6768

69+
fn object_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
70+
Ok(vm.ctx.none())
71+
}
72+
6873
fn object_dict(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6974
match args.args[0].borrow().kind {
7075
PyObjectKind::Class { ref dict, .. } => Ok(dict.clone()),

vm/src/pyobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl AttributeProtocol for PyObjectRef {
417417
None
418418
}
419419
PyObjectKind::Instance { ref dict } => dict.get_item(attr_name),
420-
ref kind => unimplemented!("load_attr unimplemented for: {:?}", kind),
420+
_ => None,
421421
}
422422
}
423423

0 commit comments

Comments
 (0)