Skip to content

Commit d6fc20f

Browse files
committed
Relocate vm.set_attr to obj.set_attr
1 parent 7bcf2c3 commit d6fc20f

File tree

14 files changed

+79
-80
lines changed

14 files changed

+79
-80
lines changed

vm/src/builtins/namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Constructor for PyNamespace {
2424
fn py_new(cls: PyTypeRef, kwargs: Self::Args, vm: &VirtualMachine) -> PyResult {
2525
let zelf = PyNamespace.into_ref_with_type(vm, cls)?;
2626
for (name, value) in kwargs.into_iter() {
27-
vm.set_attr(zelf.as_object(), name, value)?;
27+
zelf.as_object().set_attr(name, value, vm)?;
2828
}
2929
Ok(zelf.into_pyobject(vm))
3030
}

vm/src/builtins/weakproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl SetAttr for PyWeakProxy {
6666
vm: &VirtualMachine,
6767
) -> PyResult<()> {
6868
match zelf.weak.upgrade() {
69-
Some(obj) => vm.call_set_attr(&obj, attr_name, value),
69+
Some(obj) => obj.call_set_attr(vm, attr_name, value),
7070
None => Err(vm.new_exception_msg(
7171
vm.ctx.exceptions.reference_error.clone(),
7272
"weakly-referenced object no longer exists".to_owned(),

vm/src/exceptions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,16 +1079,16 @@ pub(super) mod types {
10791079
args: FuncArgs,
10801080
vm: &VirtualMachine,
10811081
) -> PyResult<()> {
1082-
let exc_self = zelf.into();
1083-
vm.set_attr(
1084-
&exc_self,
1082+
let zelf: PyObjectRef = zelf.into();
1083+
zelf.set_attr(
10851084
"name",
10861085
vm.unwrap_or_none(args.kwargs.get("name").cloned()),
1086+
vm,
10871087
)?;
1088-
vm.set_attr(
1089-
&exc_self,
1088+
zelf.set_attr(
10901089
"path",
10911090
vm.unwrap_or_none(args.kwargs.get("path").cloned()),
1091+
vm,
10921092
)?;
10931093
Ok(())
10941094
}

vm/src/frame.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,14 +1651,14 @@ impl ExecutingFrame<'_> {
16511651
)
16521652
.into_object(vm);
16531653

1654-
vm.set_attr(&func_obj, "__doc__", vm.ctx.none())?;
1654+
func_obj.set_attr("__doc__", vm.ctx.none(), vm)?;
16551655

16561656
let name = qualified_name.as_str().split('.').next_back().unwrap();
1657-
vm.set_attr(&func_obj, "__name__", vm.new_pyobj(name))?;
1658-
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
1657+
func_obj.set_attr("__name__", vm.new_pyobj(name), vm)?;
1658+
func_obj.set_attr("__qualname__", qualified_name, vm)?;
16591659
let module = vm.unwrap_or_none(self.globals.get_item_option("__name__", vm)?);
1660-
vm.set_attr(&func_obj, "__module__", module)?;
1661-
vm.set_attr(&func_obj, "__annotations__", annotations)?;
1660+
func_obj.set_attr("__module__", module, vm)?;
1661+
func_obj.set_attr("__annotations__", annotations, vm)?;
16621662

16631663
self.push_value(func_obj);
16641664
Ok(None)
@@ -1803,7 +1803,7 @@ impl ExecutingFrame<'_> {
18031803
let attr_name = self.code.names[attr as usize].clone();
18041804
let parent = self.pop_value();
18051805
let value = self.pop_value();
1806-
vm.set_attr(&parent, attr_name, value)?;
1806+
parent.set_attr(attr_name, value, vm)?;
18071807
Ok(None)
18081808
}
18091809

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn init_importlib(
5757
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
5858
}
5959
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
60-
vm.set_attr(&importlib_external, "MAGIC_NUMBER", magic)?;
60+
importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?;
6161
let zipimport_res = (|| -> PyResult<()> {
6262
let zipimport = vm.import("zipimport", None, 0)?;
6363
let zipimporter = zipimport.get_attr("zipimporter", vm)?;

vm/src/protocol/object.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ impl PyObjectRef {
3535
getattro(self, attr_name, vm)
3636
}
3737

38+
pub fn call_set_attr(
39+
&self,
40+
vm: &VirtualMachine,
41+
attr_name: PyStrRef,
42+
attr_value: Option<PyObjectRef>,
43+
) -> PyResult<()> {
44+
let setattro = {
45+
let cls = self.class();
46+
cls.mro_find_map(|cls| cls.slots.setattro.load())
47+
.ok_or_else(|| {
48+
let assign = attr_value.is_some();
49+
let has_getattr = cls.mro_find_map(|cls| cls.slots.getattro.load()).is_some();
50+
vm.new_type_error(format!(
51+
"'{}' object has {} attributes ({} {})",
52+
cls.name(),
53+
if has_getattr { "only read-only" } else { "no" },
54+
if assign { "assign to" } else { "del" },
55+
attr_name
56+
))
57+
})?
58+
};
59+
setattro(self, attr_name, attr_value, vm)
60+
}
61+
3862
// PyObject *PyObject_GenericGetAttr(PyObject *o, PyObject *name)
3963

4064
pub fn set_attr(
@@ -43,7 +67,8 @@ impl PyObjectRef {
4367
attr_value: impl Into<PyObjectRef>,
4468
vm: &VirtualMachine,
4569
) -> PyResult<()> {
46-
vm.set_attr(self, attr_name, attr_value)
70+
let attr_name = attr_name.into_pystr_ref(vm);
71+
self.call_set_attr(vm, attr_name, Some(attr_value.into()))
4772
}
4873

4974
// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)

vm/src/stdlib/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl AstNode {
5959
)));
6060
}
6161
for (name, arg) in fields.iter().zip(args.args) {
62-
vm.set_attr(&zelf, name.clone(), arg)?;
62+
zelf.set_attr(name.clone(), arg, vm)?;
6363
}
6464
for (key, value) in args.kwargs {
6565
if let Some(pos) = fields.iter().position(|f| f.as_str() == key) {
@@ -71,7 +71,7 @@ impl AstNode {
7171
)));
7272
}
7373
}
74-
vm.set_attr(&zelf, key, value)?;
74+
zelf.set_attr(key, value, vm)?;
7575
}
7676
Ok(())
7777
}

vm/src/stdlib/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ mod builtins {
732732
value: PyObjectRef,
733733
vm: &VirtualMachine,
734734
) -> PyResult<()> {
735-
vm.set_attr(&obj, attr, value)?;
735+
obj.set_attr(attr, value, vm)?;
736736
Ok(())
737737
}
738738

vm/src/stdlib/errno.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1616
errorcode
1717
.set_item(code.clone(), name.clone().into(), vm)
1818
.unwrap();
19-
vm.set_attr(&module, name, code).unwrap();
19+
module.set_attr(name, code, vm).unwrap();
2020
}
2121
module
2222
}

vm/src/stdlib/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ mod _io {
542542
pub(super) fn iobase_close(file: &PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
543543
if !file_closed(file, vm)? {
544544
let res = vm.call_method(file, "flush", ());
545-
vm.set_attr(file, "__closed", vm.new_pyobj(true))?;
545+
file.set_attr("__closed", vm.new_pyobj(true), vm)?;
546546
res?;
547547
}
548548
Ok(())
@@ -3622,7 +3622,7 @@ mod _io {
36223622
line_buffering,
36233623
),
36243624
)?;
3625-
vm.set_attr(&wrapper, "mode", vm.new_pyobj(mode_string))?;
3625+
wrapper.set_attr("mode", vm.new_pyobj(mode_string), vm)?;
36263626
Ok(wrapper)
36273627
}
36283628
EncodeMode::Bytes => Ok(buffered),
@@ -3876,7 +3876,7 @@ mod fileio {
38763876
zelf.closefd.store(args.closefd);
38773877
#[cfg(windows)]
38783878
crate::stdlib::msvcrt::setmode_binary(fd);
3879-
vm.set_attr(zelf.as_object(), "name", name)?;
3879+
zelf.as_object().set_attr("name", name, vm)?;
38803880
Ok(())
38813881
}
38823882

0 commit comments

Comments
 (0)