1
1
/*! Python `super` class.
2
2
3
+ See also:
4
+
5
+ https://github.com/python/cpython/blob/50b48572d9a90c5bb36e2bef6179548ea927a35a/Objects/typeobject.c#L7663
6
+
3
7
*/
4
8
5
9
use super :: super :: pyobject:: {
@@ -15,9 +19,47 @@ pub fn init(context: &PyContext) {
15
19
16
20
fn super_init ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
17
21
trace ! ( "super.__init__ {:?}" , args. args) ;
18
- arg_check ! ( vm, args, required = [ ( _inst, None ) ] ) ;
22
+ arg_check ! (
23
+ vm,
24
+ args,
25
+ required = [ ( inst, None ) ] ,
26
+ optional = [ ( py_type, None ) , ( py_obj, None ) ]
27
+ ) ;
28
+
29
+ // Get the type:
30
+ let py_type = if let Some ( ty) = py_type {
31
+ ty. clone ( )
32
+ } else {
33
+ // TODO: implement complex logic here....
34
+ vm. get_none ( )
35
+ } ;
36
+
37
+ // Check type argument:
38
+ if !objtype:: isinstance ( & py_type, & vm. get_type ( ) ) {
39
+ let type_name = objtype:: get_type_name ( & py_type. typ ( ) ) ;
40
+ return Err ( vm. new_type_error ( format ! (
41
+ "super() argument 1 must be type, not {}" ,
42
+ type_name
43
+ ) ) ) ;
44
+ }
45
+
46
+ // Get the bound object:
47
+ let py_obj = if let Some ( obj) = py_obj {
48
+ obj. clone ( )
49
+ } else {
50
+ vm. get_none ( )
51
+ } ;
52
+
53
+ // Check obj type:
54
+ if !( objtype:: isinstance ( & py_obj, & py_type) || objtype:: issubclass ( & py_obj, & py_type) ) {
55
+ return Err ( vm. new_type_error ( format ! (
56
+ "super(type, obj): obj must be an instance or subtype of type"
57
+ ) ) ) ;
58
+ }
19
59
20
- // TODO: implement complex logic here....
60
+ // TODO: how to store those types?
61
+ inst. set_attr ( "type" , py_type) ;
62
+ inst. set_attr ( "obj" , py_obj) ;
21
63
22
64
Ok ( vm. get_none ( ) )
23
65
}
0 commit comments