@@ -5,12 +5,15 @@ use super::objtuple::PyTupleRef;
5
5
use super :: objtype:: PyClassRef ;
6
6
use crate :: descriptor:: PyBuiltinDescriptor ;
7
7
use crate :: function:: { OptionalArg , PyFuncArgs } ;
8
- use crate :: pyobject:: { IdProtocol , PyContext , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol } ;
8
+ use crate :: pyobject:: {
9
+ IdProtocol , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol ,
10
+ } ;
9
11
use crate :: scope:: Scope ;
10
12
use crate :: vm:: VirtualMachine ;
11
13
12
14
pub type PyFunctionRef = PyRef < PyFunction > ;
13
15
16
+ #[ pyclass]
14
17
#[ derive( Debug ) ]
15
18
pub struct PyFunction {
16
19
// TODO: these shouldn't be public
@@ -61,20 +64,25 @@ impl PyValue for PyFunction {
61
64
}
62
65
}
63
66
64
- impl PyFunctionRef {
65
- fn call ( func : PyObjectRef , args : PyFuncArgs , vm : & VirtualMachine ) -> PyResult {
66
- vm. invoke ( & func, args)
67
+ #[ pyimpl]
68
+ impl PyFunction {
69
+ #[ pymethod( name = "__call__" ) ]
70
+ fn call ( zelf : PyObjectRef , args : PyFuncArgs , vm : & VirtualMachine ) -> PyResult {
71
+ vm. invoke ( & zelf, args)
67
72
}
68
73
69
- fn code ( self , _vm : & VirtualMachine ) -> PyCodeRef {
74
+ #[ pyproperty( name = "__code__" ) ]
75
+ fn code ( & self , _vm : & VirtualMachine ) -> PyCodeRef {
70
76
self . code . clone ( )
71
77
}
72
78
73
- fn defaults ( self , _vm : & VirtualMachine ) -> Option < PyTupleRef > {
79
+ #[ pyproperty( name = "__defaults__" ) ]
80
+ fn defaults ( & self , _vm : & VirtualMachine ) -> Option < PyTupleRef > {
74
81
self . defaults . clone ( )
75
82
}
76
83
77
- fn kwdefaults ( self , _vm : & VirtualMachine ) -> Option < PyDictRef > {
84
+ #[ pyproperty( name = "__kwdefaults__" ) ]
85
+ fn kwdefaults ( & self , _vm : & VirtualMachine ) -> Option < PyDictRef > {
78
86
self . kw_only_defaults . clone ( )
79
87
}
80
88
}
@@ -104,13 +112,10 @@ impl PyValue for PyBoundMethod {
104
112
105
113
pub fn init ( context : & PyContext ) {
106
114
let function_type = & context. types . function_type ;
115
+ PyFunction :: extend_class ( context, function_type) ;
107
116
extend_class ! ( context, function_type, {
108
117
"__get__" => context. new_method( PyFunction :: get) ,
109
118
( slot descr_get) => PyFunction :: get,
110
- "__call__" => context. new_method( PyFunctionRef :: call) ,
111
- "__code__" => context. new_property( PyFunctionRef :: code) ,
112
- "__defaults__" => context. new_property( PyFunctionRef :: defaults) ,
113
- "__kwdefaults__" => context. new_property( PyFunctionRef :: kwdefaults) ,
114
119
} ) ;
115
120
116
121
let method_type = & context. types . bound_method_type ;
0 commit comments