@@ -9,7 +9,7 @@ use crate::vm::VirtualMachine;
9
9
10
10
use super :: objdict;
11
11
use super :: objlist:: PyList ;
12
- use super :: objstr;
12
+ use super :: objstr:: { self , PyStringRef } ;
13
13
use super :: objtuple:: PyTuple ;
14
14
15
15
#[ derive( Clone , Debug ) ]
@@ -59,6 +59,35 @@ impl PyClassRef {
59
59
offset : None ,
60
60
}
61
61
}
62
+
63
+ fn mro ( self , _vm : & mut VirtualMachine ) -> PyTuple {
64
+ PyTuple :: from ( _mro ( & self ) )
65
+ }
66
+
67
+ fn dir ( self , vm : & mut VirtualMachine ) -> PyList {
68
+ let attributes = get_attributes ( self ) ;
69
+ let attributes: Vec < PyObjectRef > = attributes
70
+ . keys ( )
71
+ . map ( |k| vm. ctx . new_str ( k. to_string ( ) ) )
72
+ . collect ( ) ;
73
+ PyList :: from ( attributes)
74
+ }
75
+
76
+ fn instance_check ( self , obj : PyObjectRef , _vm : & mut VirtualMachine ) -> bool {
77
+ isinstance ( & obj, self . as_object ( ) )
78
+ }
79
+
80
+ fn subclass_check ( self , subclass : PyObjectRef , _vm : & mut VirtualMachine ) -> bool {
81
+ issubclass ( & subclass, self . as_object ( ) )
82
+ }
83
+
84
+ fn repr ( self , _vm : & mut VirtualMachine ) -> String {
85
+ format ! ( "<class '{}'>" , self . name)
86
+ }
87
+
88
+ fn prepare ( _name : PyStringRef , _bases : PyObjectRef , vm : & mut VirtualMachine ) -> PyObjectRef {
89
+ vm. new_dict ( )
90
+ }
62
91
}
63
92
64
93
/*
@@ -86,21 +115,17 @@ pub fn init(ctx: &PyContext) {
86
115
extend_class ! ( & ctx, & ctx. type_type, {
87
116
"__call__" => ctx. new_rustfunc( type_call) ,
88
117
"__new__" => ctx. new_rustfunc( type_new) ,
89
- "__mro__" => ctx. new_property( type_mro ) ,
90
- "__repr__" => ctx. new_rustfunc( type_repr ) ,
91
- "__prepare__" => ctx. new_rustfunc( type_prepare ) ,
118
+ "__mro__" => ctx. new_property( PyClassRef :: mro ) ,
119
+ "__repr__" => ctx. new_rustfunc( PyClassRef :: repr ) ,
120
+ "__prepare__" => ctx. new_rustfunc( PyClassRef :: prepare ) ,
92
121
"__getattribute__" => ctx. new_rustfunc( type_getattribute) ,
93
- "__instancecheck__" => ctx. new_rustfunc( type_instance_check ) ,
94
- "__subclasscheck__" => ctx. new_rustfunc( type_subclass_check ) ,
122
+ "__instancecheck__" => ctx. new_rustfunc( PyClassRef :: instance_check ) ,
123
+ "__subclasscheck__" => ctx. new_rustfunc( PyClassRef :: subclass_check ) ,
95
124
"__doc__" => ctx. new_str( type_doc. to_string( ) ) ,
96
- "__dir__" => ctx. new_rustfunc( type_dir ) ,
125
+ "__dir__" => ctx. new_rustfunc( PyClassRef :: dir ) ,
97
126
} ) ;
98
127
}
99
128
100
- fn type_mro ( cls : PyClassRef , _vm : & mut VirtualMachine ) -> PyResult < PyTuple > {
101
- Ok ( PyTuple :: from ( _mro ( & cls) ) )
102
- }
103
-
104
129
fn _mro ( cls : & PyClassRef ) -> Vec < PyObjectRef > {
105
130
cls. iter_mro ( ) . cloned ( ) . collect ( )
106
131
}
@@ -111,15 +136,6 @@ pub fn isinstance(obj: &PyObjectRef, cls: &PyObjectRef) -> bool {
111
136
issubclass ( obj. type_ref ( ) , & cls)
112
137
}
113
138
114
- fn type_instance_check ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
115
- arg_check ! (
116
- vm,
117
- args,
118
- required = [ ( typ, Some ( vm. ctx. type_type( ) ) ) , ( obj, None ) ]
119
- ) ;
120
- Ok ( vm. new_bool ( isinstance ( obj, typ) ) )
121
- }
122
-
123
139
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
124
140
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
125
141
/// method.
@@ -128,18 +144,6 @@ pub fn issubclass(subclass: &PyObjectRef, cls: &PyObjectRef) -> bool {
128
144
subclass. is ( & cls) || mro. iter ( ) . any ( |c| c. is ( & cls) )
129
145
}
130
146
131
- fn type_subclass_check ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
132
- arg_check ! (
133
- vm,
134
- args,
135
- required = [
136
- ( cls, Some ( vm. ctx. type_type( ) ) ) ,
137
- ( subclass, Some ( vm. ctx. type_type( ) ) )
138
- ]
139
- ) ;
140
- Ok ( vm. new_bool ( issubclass ( subclass, cls) ) )
141
- }
142
-
143
147
pub fn get_type_name ( typ : & PyObjectRef ) -> String {
144
148
if let Some ( PyClass { name, .. } ) = & typ. payload :: < PyClass > ( ) {
145
149
name. clone ( )
@@ -239,15 +243,6 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
239
243
}
240
244
}
241
245
242
- pub fn type_dir ( obj : PyClassRef , vm : & mut VirtualMachine ) -> PyList {
243
- let attributes = get_attributes ( obj) ;
244
- let attributes: Vec < PyObjectRef > = attributes
245
- . keys ( )
246
- . map ( |k| vm. ctx . new_str ( k. to_string ( ) ) )
247
- . collect ( ) ;
248
- PyList :: from ( attributes)
249
- }
250
-
251
246
pub fn get_attributes ( cls : PyClassRef ) -> PyAttributes {
252
247
// Gather all members here:
253
248
let mut attributes = PyAttributes :: new ( ) ;
@@ -335,16 +330,6 @@ pub fn new(
335
330
. into_ref ( ) )
336
331
}
337
332
338
- fn type_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
339
- arg_check ! ( vm, args, required = [ ( obj, Some ( vm. ctx. type_type( ) ) ) ] ) ;
340
- let type_name = get_type_name ( & obj) ;
341
- Ok ( vm. new_str ( format ! ( "<class '{}'>" , type_name) ) )
342
- }
343
-
344
- fn type_prepare ( vm : & mut VirtualMachine , _args : PyFuncArgs ) -> PyResult {
345
- Ok ( vm. new_dict ( ) )
346
- }
347
-
348
333
#[ cfg( test) ]
349
334
mod tests {
350
335
use super :: { linearise_mro, new} ;
0 commit comments