Skip to content

Commit de41d4c

Browse files
authored
Merge pull request RustPython#3338 from AP2008/relocate-set_attrs
Relocate `vm.set_attr` to `obj.set_attr`
2 parents 7bcf2c3 + d6fc20f commit de41d4c

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

vm/src/stdlib/os.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,13 @@ impl IntoPyException for IOErrorBuilder {
296296
let excp = self.error.into_pyexception(vm);
297297

298298
if let Some(filename) = self.filename {
299-
vm.set_attr(excp.as_object(), "filename", filename.filename(vm))
299+
excp.as_object()
300+
.set_attr("filename", filename.filename(vm), vm)
300301
.unwrap();
301302
}
302303
if let Some(filename2) = self.filename2 {
303-
vm.set_attr(excp.as_object(), "filename2", filename2.filename(vm))
304+
excp.as_object()
305+
.set_attr("filename2", filename2.filename(vm), vm)
304306
.unwrap();
305307
}
306308
excp

vm/src/stdlib/sys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ mod sys {
289289
return Ok(());
290290
}
291291
// set to none to avoid recursion while printing
292-
vm.set_attr(&vm.builtins, "_", vm.ctx.none())?;
292+
vm.builtins.set_attr("_", vm.ctx.none(), vm)?;
293293
// TODO: catch encoding errors
294294
let repr = vm.to_repr(&obj)?.into();
295295
builtins::print(PosArgs::new(vec![repr]), Default::default(), vm)?;
296-
vm.set_attr(&vm.builtins, "_", obj)?;
296+
vm.builtins.set_attr("_", obj, vm)?;
297297
Ok(())
298298
}
299299

vm/src/vm.rs

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,20 @@ impl VirtualMachine {
360360
Default::default(),
361361
self,
362362
)?;
363-
self.set_attr(
364-
&self.sys_module,
363+
self.sys_module.set_attr(
365364
format!("__{}__", name), // e.g. __stdin__
366365
stdio.clone(),
366+
self,
367367
)?;
368-
self.set_attr(&self.sys_module, name, stdio)?;
368+
self.sys_module.set_attr(name, stdio, self)?;
369369
Ok(())
370370
};
371371
set_stdio("stdin", 0, "r")?;
372372
set_stdio("stdout", 1, "w")?;
373373
set_stdio("stderr", 2, "w")?;
374374

375375
let io_open = io.get_attr("open", self)?;
376-
self.set_attr(&self.builtins, "open", io_open)?;
376+
self.builtins.set_attr("open", io_open, self)?;
377377
}
378378

379379
Ok(())
@@ -778,29 +778,34 @@ impl VirtualMachine {
778778
let syntax_error = self.new_exception_msg(syntax_error_type, error.to_string());
779779
let lineno = self.ctx.new_int(error.location.row());
780780
let offset = self.ctx.new_int(error.location.column());
781-
self.set_attr(syntax_error.as_object(), "lineno", lineno)
781+
syntax_error
782+
.as_object()
783+
.set_attr("lineno", lineno, self)
782784
.unwrap();
783-
self.set_attr(syntax_error.as_object(), "offset", offset)
785+
syntax_error
786+
.as_object()
787+
.set_attr("offset", offset, self)
788+
.unwrap();
789+
syntax_error
790+
.as_object()
791+
.set_attr("text", error.statement.clone().into_pyobject(self), self)
792+
.unwrap();
793+
syntax_error
794+
.as_object()
795+
.set_attr(
796+
"filename",
797+
self.ctx.new_str(error.source_path.clone()),
798+
self,
799+
)
784800
.unwrap();
785-
self.set_attr(
786-
syntax_error.as_object(),
787-
"text",
788-
error.statement.clone().into_pyobject(self),
789-
)
790-
.unwrap();
791-
self.set_attr(
792-
syntax_error.as_object(),
793-
"filename",
794-
self.ctx.new_str(error.source_path.clone()),
795-
)
796-
.unwrap();
797801
syntax_error
798802
}
799803

800804
pub fn new_import_error(&self, msg: String, name: impl IntoPyStrRef) -> PyBaseExceptionRef {
801805
let import_error = self.ctx.exceptions.import_error.clone();
802806
let exc = self.new_exception_msg(import_error, msg);
803-
self.set_attr(exc.as_object(), "name", name.into_pystr_ref(self))
807+
exc.as_object()
808+
.set_attr("name", name.into_pystr_ref(self), self)
804809
.unwrap();
805810
exc
806811
}
@@ -1408,42 +1413,9 @@ impl VirtualMachine {
14081413
}
14091414
}
14101415

1411-
pub fn call_set_attr(
1412-
&self,
1413-
obj: &PyObjectRef,
1414-
attr_name: PyStrRef,
1415-
attr_value: Option<PyObjectRef>,
1416-
) -> PyResult<()> {
1417-
let setattro = {
1418-
let cls = obj.class();
1419-
cls.mro_find_map(|cls| cls.slots.setattro.load())
1420-
.ok_or_else(|| {
1421-
let assign = attr_value.is_some();
1422-
let has_getattr = cls.mro_find_map(|cls| cls.slots.getattro.load()).is_some();
1423-
self.new_type_error(format!(
1424-
"'{}' object has {} attributes ({} {})",
1425-
cls.name(),
1426-
if has_getattr { "only read-only" } else { "no" },
1427-
if assign { "assign to" } else { "del" },
1428-
attr_name
1429-
))
1430-
})?
1431-
};
1432-
setattro(obj, attr_name, attr_value, self)
1433-
}
1434-
1435-
pub fn set_attr<K, V>(&self, obj: &PyObjectRef, attr_name: K, attr_value: V) -> PyResult<()>
1436-
where
1437-
K: IntoPyStrRef,
1438-
V: Into<PyObjectRef>,
1439-
{
1440-
let attr_name = attr_name.into_pystr_ref(self);
1441-
self.call_set_attr(obj, attr_name, Some(attr_value.into()))
1442-
}
1443-
14441416
pub fn del_attr(&self, obj: &PyObjectRef, attr_name: impl IntoPyStrRef) -> PyResult<()> {
14451417
let attr_name = attr_name.into_pystr_ref(self);
1446-
self.call_set_attr(obj, attr_name, None)
1418+
obj.call_set_attr(self, attr_name, None)
14471419
}
14481420

14491421
// get_method should be used for internal access to magic methods (by-passing

wasm/lib/src/vm_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl WASMVirtualMachine {
246246
} else {
247247
return Err(error());
248248
};
249-
vm.set_attr(&vm.sys_module, "stdout", stdout).unwrap();
249+
vm.sys_module.set_attr("stdout", stdout, vm).unwrap();
250250
Ok(())
251251
})?
252252
}

0 commit comments

Comments
 (0)