Skip to content

Commit f4b9bdf

Browse files
authored
Implement property name (RustPython#4944)
1 parent 429d1d1 commit f4b9bdf

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

vm/src/builtins/property.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*! Python `property` descriptor class.
22
33
*/
4-
use super::{PyType, PyTypeRef};
4+
use super::{PyStrRef, PyType, PyTypeRef};
55
use crate::common::lock::PyRwLock;
6-
use crate::function::PosArgs;
6+
use crate::function::{IntoFuncArgs, PosArgs};
77
use crate::{
88
class::PyClassImpl,
99
function::{FuncArgs, PySetterValue},
@@ -18,6 +18,7 @@ pub struct PyProperty {
1818
setter: PyRwLock<Option<PyObjectRef>>,
1919
deleter: PyRwLock<Option<PyObjectRef>>,
2020
doc: PyRwLock<Option<PyObjectRef>>,
21+
name: PyRwLock<Option<PyObjectRef>>,
2122
}
2223

2324
impl PyPayload for PyProperty {
@@ -36,6 +37,8 @@ pub struct PropertyArgs {
3637
fdel: Option<PyObjectRef>,
3738
#[pyarg(any, default)]
3839
doc: Option<PyObjectRef>,
40+
#[pyarg(any, default)]
41+
name: Option<PyStrRef>,
3942
}
4043

4144
impl GetDescriptor for PyProperty {
@@ -125,19 +128,18 @@ impl PyProperty {
125128

126129
#[pymethod(magic)]
127130
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
137137
))
138-
} else {
139-
Ok(())
140-
}
138+
})?;
139+
140+
*self.name.write() = Some(name);
141+
142+
Ok(())
141143
}
142144

143145
// Python builder functions
@@ -153,6 +155,7 @@ impl PyProperty {
153155
setter: PyRwLock::new(zelf.fset()),
154156
deleter: PyRwLock::new(zelf.fdel()),
155157
doc: PyRwLock::new(None),
158+
name: PyRwLock::new(None),
156159
}
157160
.into_ref_with_type(vm, zelf.class().to_owned())
158161
}
@@ -168,6 +171,7 @@ impl PyProperty {
168171
setter: PyRwLock::new(setter.or_else(|| zelf.fset())),
169172
deleter: PyRwLock::new(zelf.fdel()),
170173
doc: PyRwLock::new(None),
174+
name: PyRwLock::new(None),
171175
}
172176
.into_ref_with_type(vm, zelf.class().to_owned())
173177
}
@@ -183,6 +187,7 @@ impl PyProperty {
183187
setter: PyRwLock::new(zelf.fset()),
184188
deleter: PyRwLock::new(deleter.or_else(|| zelf.fdel())),
185189
doc: PyRwLock::new(None),
190+
name: PyRwLock::new(None),
186191
}
187192
.into_ref_with_type(vm, zelf.class().to_owned())
188193
}
@@ -223,6 +228,7 @@ impl Constructor for PyProperty {
223228
setter: PyRwLock::new(None),
224229
deleter: PyRwLock::new(None),
225230
doc: PyRwLock::new(None),
231+
name: PyRwLock::new(None),
226232
}
227233
.into_ref_with_type(vm, cls)
228234
.map(Into::into)
@@ -237,6 +243,8 @@ impl Initializer for PyProperty {
237243
*zelf.setter.write() = args.fset;
238244
*zelf.deleter.write() = args.fdel;
239245
*zelf.doc.write() = args.doc;
246+
*zelf.name.write() = args.name.map(|a| a.as_object().to_owned());
247+
240248
Ok(())
241249
}
242250
}

0 commit comments

Comments
 (0)