Skip to content

Commit 3b7bb5c

Browse files
committed
Relocate generic_setattr into object protocol
As generic_setattr is included in object protocol, it seems more appropriate to relocate. Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent 94d3777 commit 3b7bb5c

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

vm/src/builtins/object.rs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ impl PyBaseObject {
162162
value: PyObjectRef,
163163
vm: &VirtualMachine,
164164
) -> PyResult<()> {
165-
generic_setattr(&obj, name, Some(value), vm)
165+
obj.generic_setattr(name, Some(value), vm)
166166
}
167167

168168
/// Implement delattr(self, name).
169169
#[pymethod]
170170
fn __delattr__(obj: PyObjectRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
171-
generic_setattr(&obj, name, None, vm)
171+
obj.generic_setattr(name, None, vm)
172172
}
173173

174174
#[pyslot]
@@ -178,7 +178,7 @@ impl PyBaseObject {
178178
value: Option<PyObjectRef>,
179179
vm: &VirtualMachine,
180180
) -> PyResult<()> {
181-
generic_setattr(&*obj, attr_name, value, vm)
181+
obj.generic_setattr(attr_name, value, vm)
182182
}
183183

184184
/// Return str(self).
@@ -334,48 +334,6 @@ pub fn generic_getattr(obj: PyObjectRef, attr_name: PyStrRef, vm: &VirtualMachin
334334
vm.generic_getattribute(obj, attr_name)
335335
}
336336

337-
#[cfg_attr(feature = "flame-it", flame)]
338-
pub fn generic_setattr(
339-
obj: &PyObject,
340-
attr_name: PyStrRef,
341-
value: Option<PyObjectRef>,
342-
vm: &VirtualMachine,
343-
) -> PyResult<()> {
344-
vm_trace!("object.__setattr__({:?}, {}, {:?})", obj, attr_name, value);
345-
346-
if let Some(attr) = obj.get_class_attr(attr_name.as_str()) {
347-
let descr_set = attr.class().mro_find_map(|cls| cls.slots.descr_set.load());
348-
if let Some(descriptor) = descr_set {
349-
return descriptor(attr, obj.to_owned(), value, vm);
350-
}
351-
}
352-
353-
if let Some(dict) = obj.dict() {
354-
if let Some(value) = value {
355-
dict.set_item(attr_name, value, vm)?;
356-
} else {
357-
dict.del_item(attr_name.clone(), vm).map_err(|e| {
358-
if e.fast_isinstance(&vm.ctx.exceptions.key_error) {
359-
vm.new_attribute_error(format!(
360-
"'{}' object has no attribute '{}'",
361-
obj.class().name(),
362-
attr_name,
363-
))
364-
} else {
365-
e
366-
}
367-
})?;
368-
}
369-
Ok(())
370-
} else {
371-
Err(vm.new_attribute_error(format!(
372-
"'{}' object has no attribute '{}'",
373-
obj.class().name(),
374-
attr_name,
375-
)))
376-
}
377-
}
378-
379337
pub fn init(ctx: &Context) {
380338
PyBaseObject::extend_class(ctx, &ctx.types.object_type);
381339
}

vm/src/protocol/object.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,47 @@ impl PyObject {
131131
}
132132

133133
// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
134+
#[cfg_attr(feature = "flame-it", flame)]
135+
pub fn generic_setattr(
136+
&self,
137+
attr_name: PyStrRef,
138+
value: Option<PyObjectRef>,
139+
vm: &VirtualMachine,
140+
) -> PyResult<()> {
141+
vm_trace!("object.__setattr__({:?}, {}, {:?})", obj, attr_name, value);
142+
143+
if let Some(attr) = self.get_class_attr(attr_name.as_str()) {
144+
let descr_set = attr.class().mro_find_map(|cls| cls.slots.descr_set.load());
145+
if let Some(descriptor) = descr_set {
146+
return descriptor(attr, self.to_owned(), value, vm);
147+
}
148+
}
149+
150+
if let Some(dict) = self.dict() {
151+
if let Some(value) = value {
152+
dict.set_item(attr_name, value, vm)?;
153+
} else {
154+
dict.del_item(attr_name.clone(), vm).map_err(|e| {
155+
if e.fast_isinstance(&vm.ctx.exceptions.key_error) {
156+
vm.new_attribute_error(format!(
157+
"'{}' object has no attribute '{}'",
158+
self.class().name(),
159+
attr_name,
160+
))
161+
} else {
162+
e
163+
}
164+
})?;
165+
}
166+
Ok(())
167+
} else {
168+
Err(vm.new_attribute_error(format!(
169+
"'{}' object has no attribute '{}'",
170+
self.class().name(),
171+
attr_name,
172+
)))
173+
}
174+
}
134175

135176
pub fn del_attr(&self, attr_name: impl IntoPyStrRef, vm: &VirtualMachine) -> PyResult<()> {
136177
let attr_name = attr_name.into_pystr_ref(vm);

vm/src/types/slot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use crate::builtins::object::{generic_getattr, generic_setattr};
1+
pub use crate::builtins::object::generic_getattr;
22
use crate::common::{hash::PyHash, lock::PyRwLock};
33
use crate::{
44
builtins::{PyInt, PyStrRef, PyType, PyTypeRef},

vm/src/vm/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod vm_ops;
1717
use crate::{
1818
builtins::{
1919
code::{self, PyCode},
20-
object,
2120
pystr::IntoPyStrRef,
2221
tuple::{PyTuple, PyTupleTyped},
2322
PyBaseExceptionRef, PyDictRef, PyList, PyModule, PyStrRef, PyTypeRef,
@@ -741,6 +740,6 @@ impl VirtualMachine {
741740
attr_value: impl Into<PyObjectRef>,
742741
) -> PyResult<()> {
743742
let val = attr_value.into();
744-
object::generic_setattr(module, attr_name.into_pystr_ref(self), Some(val), self)
743+
module.generic_setattr(attr_name.into_pystr_ref(self), Some(val), self)
745744
}
746745
}

0 commit comments

Comments
 (0)