1
1
/*! Python `property` descriptor class.
2
2
3
3
*/
4
- use super :: { PyType , PyTypeRef } ;
4
+ use super :: { PyStrRef , PyType , PyTypeRef } ;
5
5
use crate :: common:: lock:: PyRwLock ;
6
- use crate :: function:: PosArgs ;
6
+ use crate :: function:: { IntoFuncArgs , PosArgs } ;
7
7
use crate :: {
8
8
class:: PyClassImpl ,
9
9
function:: { FuncArgs , PySetterValue } ,
@@ -18,6 +18,7 @@ pub struct PyProperty {
18
18
setter : PyRwLock < Option < PyObjectRef > > ,
19
19
deleter : PyRwLock < Option < PyObjectRef > > ,
20
20
doc : PyRwLock < Option < PyObjectRef > > ,
21
+ name : PyRwLock < Option < PyObjectRef > > ,
21
22
}
22
23
23
24
impl PyPayload for PyProperty {
@@ -36,6 +37,8 @@ pub struct PropertyArgs {
36
37
fdel : Option < PyObjectRef > ,
37
38
#[ pyarg( any, default ) ]
38
39
doc : Option < PyObjectRef > ,
40
+ #[ pyarg( any, default ) ]
41
+ name : Option < PyStrRef > ,
39
42
}
40
43
41
44
impl GetDescriptor for PyProperty {
@@ -125,19 +128,18 @@ impl PyProperty {
125
128
126
129
#[ pymethod( magic) ]
127
130
fn set_name ( & self , args : PosArgs , vm : & VirtualMachine ) -> PyResult < ( ) > {
128
- let arg_len = args. into_vec ( ) . len ( ) ;
129
-
130
- if arg_len != 2 {
131
- Err ( vm. new_exception_msg (
132
- vm. ctx . exceptions . type_error . to_owned ( ) ,
133
- format ! (
134
- "__set_name__() takes 2 positional arguments but {} were given" ,
135
- arg_len
136
- ) ,
131
+ let func_args = args. into_args ( vm) ;
132
+ let func_args_len = func_args. args . len ( ) ;
133
+ let ( _owner, name) : ( PyObjectRef , PyObjectRef ) = func_args. bind ( vm) . map_err ( |_e| {
134
+ vm. new_type_error ( format ! (
135
+ "__set_name__() takes 2 positional arguments but {} were given" ,
136
+ func_args_len
137
137
) )
138
- } else {
139
- Ok ( ( ) )
140
- }
138
+ } ) ?;
139
+
140
+ * self . name . write ( ) = Some ( name) ;
141
+
142
+ Ok ( ( ) )
141
143
}
142
144
143
145
// Python builder functions
@@ -153,6 +155,7 @@ impl PyProperty {
153
155
setter : PyRwLock :: new ( zelf. fset ( ) ) ,
154
156
deleter : PyRwLock :: new ( zelf. fdel ( ) ) ,
155
157
doc : PyRwLock :: new ( None ) ,
158
+ name : PyRwLock :: new ( None ) ,
156
159
}
157
160
. into_ref_with_type ( vm, zelf. class ( ) . to_owned ( ) )
158
161
}
@@ -168,6 +171,7 @@ impl PyProperty {
168
171
setter : PyRwLock :: new ( setter. or_else ( || zelf. fset ( ) ) ) ,
169
172
deleter : PyRwLock :: new ( zelf. fdel ( ) ) ,
170
173
doc : PyRwLock :: new ( None ) ,
174
+ name : PyRwLock :: new ( None ) ,
171
175
}
172
176
. into_ref_with_type ( vm, zelf. class ( ) . to_owned ( ) )
173
177
}
@@ -183,6 +187,7 @@ impl PyProperty {
183
187
setter : PyRwLock :: new ( zelf. fset ( ) ) ,
184
188
deleter : PyRwLock :: new ( deleter. or_else ( || zelf. fdel ( ) ) ) ,
185
189
doc : PyRwLock :: new ( None ) ,
190
+ name : PyRwLock :: new ( None ) ,
186
191
}
187
192
. into_ref_with_type ( vm, zelf. class ( ) . to_owned ( ) )
188
193
}
@@ -223,6 +228,7 @@ impl Constructor for PyProperty {
223
228
setter : PyRwLock :: new ( None ) ,
224
229
deleter : PyRwLock :: new ( None ) ,
225
230
doc : PyRwLock :: new ( None ) ,
231
+ name : PyRwLock :: new ( None ) ,
226
232
}
227
233
. into_ref_with_type ( vm, cls)
228
234
. map ( Into :: into)
@@ -237,6 +243,8 @@ impl Initializer for PyProperty {
237
243
* zelf. setter . write ( ) = args. fset ;
238
244
* zelf. deleter . write ( ) = args. fdel ;
239
245
* zelf. doc . write ( ) = args. doc ;
246
+ * zelf. name . write ( ) = args. name . map ( |a| a. as_object ( ) . to_owned ( ) ) ;
247
+
240
248
Ok ( ( ) )
241
249
}
242
250
}
0 commit comments