1
1
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 ,
4
4
} ;
5
5
use super :: super :: vm:: VirtualMachine ;
6
6
use super :: objfloat;
@@ -12,22 +12,16 @@ fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
12
12
Ok ( vm. new_str ( v. to_string ( ) ) )
13
13
}
14
14
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
+ ) )
31
25
}
32
26
33
27
// Casting function:
@@ -51,10 +45,6 @@ pub fn get_value(obj: &PyObjectRef) -> i32 {
51
45
}
52
46
}
53
47
54
- fn set_value ( obj : & PyObjectRef , value : i32 ) {
55
- obj. borrow_mut ( ) . kind = PyObjectKind :: Integer { value } ;
56
- }
57
-
58
48
impl FromPyObjectRef for i32 {
59
49
fn from_pyobj ( obj : & PyObjectRef ) -> i32 {
60
50
get_value ( obj)
@@ -224,7 +214,7 @@ pub fn init(context: &PyContext) {
224
214
int_type. set_attr ( "__eq__" , context. new_rustfunc ( int_eq) ) ;
225
215
int_type. set_attr ( "__add__" , context. new_rustfunc ( int_add) ) ;
226
216
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 ) ) ;
228
218
int_type. set_attr ( "__mod__" , context. new_rustfunc ( int_mod) ) ;
229
219
int_type. set_attr ( "__mul__" , context. new_rustfunc ( int_mul) ) ;
230
220
int_type. set_attr ( "__or__" , context. new_rustfunc ( int_or) ) ;
0 commit comments