Skip to content

Commit ab95b57

Browse files
authored
Merge pull request RustPython#3627 from youknowone/getattro
GetAttro take reference
2 parents 05bf9b5 + f8234ae commit ab95b57

File tree

26 files changed

+96
-97
lines changed

26 files changed

+96
-97
lines changed

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,13 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
646646
}
647647

648648
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
649-
let path_importer_cache = vm.sys_module.clone().get_attr("path_importer_cache", vm)?;
649+
let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?;
650650
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
651651
if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
652652
return Ok(Some(importer));
653653
}
654654
let path = vm.ctx.new_str(path);
655-
let path_hooks = vm.sys_module.clone().get_attr("path_hooks", vm)?;
655+
let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
656656
let mut importer = None;
657657
let path_hooks: Vec<PyObjectRef> = path_hooks.try_into_value(vm)?;
658658
for path_hook in path_hooks {
@@ -674,7 +674,7 @@ fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>
674674
}
675675

676676
fn insert_sys_path(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<()> {
677-
let sys_path = vm.sys_module.clone().get_attr("path", vm).unwrap();
677+
let sys_path = vm.sys_module.get_attr("path", vm).unwrap();
678678
vm.call_method(&sys_path, "insert", (0, obj))?;
679679
Ok(())
680680
}

src/shell/helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'vm> ShellHelper<'vm> {
7878
let mut current = self.globals.get_item_opt(first.as_str(), self.vm).ok()??;
7979

8080
for attr in parents {
81-
current = current.clone().get_attr(attr.as_str(), self.vm).ok()?;
81+
current = current.get_attr(attr.as_str(), self.vm).ok()?;
8282
}
8383

8484
let current_iter = str_iter_method(current, "__dir__").ok()?;

stdlib/src/dis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod decl {
1010

1111
#[pyfunction]
1212
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
13-
let co = if let Ok(co) = obj.clone().get_attr("__code__", vm) {
13+
let co = if let Ok(co) = obj.get_attr("__code__", vm) {
1414
// Method or function:
1515
PyRef::try_from_object(vm, co)?
1616
} else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) {

stdlib/src/json.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,23 @@ mod _json {
3232
type Args = PyObjectRef;
3333

3434
fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
35-
let strict = ctx.clone().get_attr("strict", vm)?.try_to_bool(vm)?;
36-
let object_hook = vm.option_if_none(ctx.clone().get_attr("object_hook", vm)?);
37-
let object_pairs_hook =
38-
vm.option_if_none(ctx.clone().get_attr("object_pairs_hook", vm)?);
39-
let parse_float = ctx.clone().get_attr("parse_float", vm)?;
35+
let strict = ctx.get_attr("strict", vm)?.try_to_bool(vm)?;
36+
let object_hook = vm.option_if_none(ctx.get_attr("object_hook", vm)?);
37+
let object_pairs_hook = vm.option_if_none(ctx.get_attr("object_pairs_hook", vm)?);
38+
let parse_float = ctx.get_attr("parse_float", vm)?;
4039
let parse_float =
4140
if vm.is_none(&parse_float) || parse_float.is(&vm.ctx.types.float_type) {
4241
None
4342
} else {
4443
Some(parse_float)
4544
};
46-
let parse_int = ctx.clone().get_attr("parse_int", vm)?;
45+
let parse_int = ctx.get_attr("parse_int", vm)?;
4746
let parse_int = if vm.is_none(&parse_int) || parse_int.is(&vm.ctx.types.int_type) {
4847
None
4948
} else {
5049
Some(parse_int)
5150
};
52-
let parse_constant = ctx.clone().get_attr("parse_constant", vm)?;
51+
let parse_constant = ctx.get_attr("parse_constant", vm)?;
5352

5453
Self {
5554
strict,
@@ -91,7 +90,7 @@ mod _json {
9190
}
9291
'{' => {
9392
// TODO: parse the object in rust
94-
let parse_obj = self.ctx.clone().get_attr("parse_object", vm)?;
93+
let parse_obj = self.ctx.get_attr("parse_object", vm)?;
9594
return PyIterReturn::from_pyresult(
9695
vm.invoke(
9796
&parse_obj,
@@ -108,7 +107,7 @@ mod _json {
108107
}
109108
'[' => {
110109
// TODO: parse the array in rust
111-
let parse_array = self.ctx.clone().get_attr("parse_array", vm)?;
110+
let parse_array = self.ctx.get_attr("parse_array", vm)?;
112111
return PyIterReturn::from_pyresult(
113112
vm.invoke(&parse_array, ((pystr, next_idx), scan_once)),
114113
vm,

stdlib/src/unicodedata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1515
.copied()
1616
{
1717
crate::vm::extend_module!(vm, &module, {
18-
attr => ucd.clone().get_attr(attr, vm).unwrap(),
18+
attr => ucd.get_attr(attr, vm).unwrap(),
1919
});
2020
}
2121

vm/src/builtins/function.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
function::{FuncArgs, OptionalArg, PyComparisonValue},
1515
scope::Scope,
1616
types::{Callable, Comparable, Constructor, GetAttr, GetDescriptor, PyComparisonOp},
17-
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
17+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1818
};
1919
#[cfg(feature = "jit")]
2020
use crate::{common::lock::OnceCell, convert::ToPyObject};
@@ -460,12 +460,12 @@ impl Comparable for PyBoundMethod {
460460
}
461461

462462
impl GetAttr for PyBoundMethod {
463-
fn getattro(zelf: PyRef<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
463+
fn getattro(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
464464
let class_attr = zelf.get_class_attr(name.as_str());
465465
if let Some(obj) = class_attr {
466-
return vm.call_if_get_descriptor(obj, zelf.into());
466+
return vm.call_if_get_descriptor(obj, zelf.to_owned().into());
467467
}
468-
zelf.function.clone().get_attr(name, vm)
468+
zelf.function.get_attr(name, vm)
469469
}
470470
}
471471

@@ -526,7 +526,7 @@ impl PyBoundMethod {
526526

527527
#[pyproperty(magic)]
528528
fn doc(&self, vm: &VirtualMachine) -> PyResult {
529-
self.function.clone().get_attr("__doc__", vm)
529+
self.function.get_attr("__doc__", vm)
530530
}
531531

532532
#[pyproperty(magic)]
@@ -541,7 +541,7 @@ impl PyBoundMethod {
541541

542542
#[pyproperty(magic)]
543543
fn module(&self, vm: &VirtualMachine) -> Option<PyObjectRef> {
544-
self.function.clone().get_attr("__module__", vm).ok()
544+
self.function.get_attr("__module__", vm).ok()
545545
}
546546

547547
#[pyproperty(magic)]
@@ -563,7 +563,7 @@ impl PyBoundMethod {
563563
))
564564
.into());
565565
}
566-
self.function.clone().get_attr("__qualname__", vm)
566+
self.function.get_attr("__qualname__", vm)
567567
}
568568
}
569569

vm/src/builtins/genericalias.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ impl Hashable for PyGenericAlias {
366366
}
367367

368368
impl GetAttr for PyGenericAlias {
369-
fn getattro(zelf: PyRef<Self>, attr: PyStrRef, vm: &VirtualMachine) -> PyResult {
370-
for exc in &ATTR_EXCEPTIONS {
369+
fn getattro(zelf: &Py<Self>, attr: PyStrRef, vm: &VirtualMachine) -> PyResult {
370+
for exc in ATTR_EXCEPTIONS.iter() {
371371
if *(*exc) == attr.to_string() {
372372
return zelf.as_object().generic_getattr(attr, vm);
373373
}

vm/src/builtins/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ pub fn builtins_iter(vm: &VirtualMachine) -> &PyObject {
150150
static_cell! {
151151
static INSTANCE: PyObjectRef;
152152
}
153-
INSTANCE.get_or_init(|| vm.builtins.clone().get_attr("iter", vm).unwrap())
153+
INSTANCE.get_or_init(|| vm.builtins.get_attr("iter", vm).unwrap())
154154
}
155155

156156
pub fn builtins_reversed(vm: &VirtualMachine) -> &PyObject {
157157
static_cell! {
158158
static INSTANCE: PyObjectRef;
159159
}
160-
INSTANCE.get_or_init(|| vm.builtins.clone().get_attr("reversed", vm).unwrap())
160+
INSTANCE.get_or_init(|| vm.builtins.get_attr("reversed", vm).unwrap())
161161
}
162162

163163
#[pyclass(module = false, name = "iterator")]

vm/src/builtins/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl Initializer for PyModule {
137137
}
138138

139139
impl GetAttr for PyModule {
140-
fn getattro(zelf: PyRef<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
141-
Self::getattr_inner(&zelf, name, vm)
140+
fn getattro(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
141+
Self::getattr_inner(zelf, name, vm)
142142
}
143143
}
144144

vm/src/builtins/object.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,17 @@ impl PyBaseObject {
287287
}
288288

289289
/// Return getattr(self, name).
290-
#[pymethod(name = "__getattribute__")]
291290
#[pyslot]
292-
pub(crate) fn getattro(obj: PyObjectRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
291+
pub(crate) fn getattro(obj: &PyObject, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
293292
vm_trace!("object.__getattribute__({:?}, {:?})", obj, name);
294293
obj.as_object().generic_getattr(name, vm)
295294
}
296295

296+
#[pymethod(magic)]
297+
fn getattribute(obj: PyObjectRef, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
298+
Self::getattro(&obj, name, vm)
299+
}
300+
297301
#[pymethod(magic)]
298302
fn reduce(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
299303
common_reduce(obj, 0, vm)

0 commit comments

Comments
 (0)