Skip to content

Commit 5f285a5

Browse files
Merge pull request RustPython#1252 from mpajkowski/invoke_ref
Make func_ref an &PyObjectRef
2 parents 87b9e6e + e65e9ff commit 5f285a5

26 files changed

+82
-80
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
359359
debug!("Running module {}", module);
360360
let runpy = vm.import("runpy", &vm.ctx.new_tuple(vec![]), 0)?;
361361
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
362-
vm.invoke(run_module_as_main, vec![vm.new_str(module.to_owned())])?;
362+
vm.invoke(&run_module_as_main, vec![vm.new_str(module.to_owned())])?;
363363
Ok(())
364364
}
365365

vm/src/builtins.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn builtin_abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
3737
let method = vm.get_method_or_type_error(x.clone(), "__abs__", || {
3838
format!("bad operand type for abs(): '{}'", x.class().name)
3939
})?;
40-
vm.invoke(method, PyFuncArgs::new(vec![], vec![]))
40+
vm.invoke(&method, PyFuncArgs::new(vec![], vec![]))
4141
}
4242

4343
fn builtin_all(iterable: PyIterable<bool>, vm: &VirtualMachine) -> PyResult<bool> {
@@ -395,7 +395,7 @@ fn builtin_len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
395395
let method = vm.get_method_or_type_error(obj.clone(), "__len__", || {
396396
format!("object of type '{}' has no len()", obj.class().name)
397397
})?;
398-
vm.invoke(method, PyFuncArgs::default())
398+
vm.invoke(&method, PyFuncArgs::default())
399399
}
400400

401401
fn builtin_locals(vm: &VirtualMachine) -> PyDictRef {
@@ -428,15 +428,15 @@ fn builtin_max(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
428428
let mut x = candidates_iter.next().unwrap();
429429
// TODO: this key function looks pretty duplicate. Maybe we can create
430430
// a local function?
431-
let mut x_key = if let Some(f) = &key_func {
432-
vm.invoke(f.clone(), vec![x.clone()])?
431+
let mut x_key = if let Some(ref f) = &key_func {
432+
vm.invoke(f, vec![x.clone()])?
433433
} else {
434434
x.clone()
435435
};
436436

437437
for y in candidates_iter {
438-
let y_key = if let Some(f) = &key_func {
439-
vm.invoke(f.clone(), vec![y.clone()])?
438+
let y_key = if let Some(ref f) = &key_func {
439+
vm.invoke(f, vec![y.clone()])?
440440
} else {
441441
y.clone()
442442
};
@@ -476,15 +476,15 @@ fn builtin_min(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
476476
let mut x = candidates_iter.next().unwrap();
477477
// TODO: this key function looks pretty duplicate. Maybe we can create
478478
// a local function?
479-
let mut x_key = if let Some(f) = &key_func {
480-
vm.invoke(f.clone(), vec![x.clone()])?
479+
let mut x_key = if let Some(ref f) = &key_func {
480+
vm.invoke(f, vec![x.clone()])?
481481
} else {
482482
x.clone()
483483
};
484484

485485
for y in candidates_iter {
486-
let y_key = if let Some(f) = &key_func {
487-
vm.invoke(f.clone(), vec![y.clone()])?
486+
let y_key = if let Some(ref f) = &key_func {
487+
vm.invoke(f, vec![y.clone()])?
488488
} else {
489489
y.clone()
490490
};
@@ -711,7 +711,7 @@ fn builtin_reversed(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
711711
arg_check!(vm, args, required = [(obj, None)]);
712712

713713
if let Some(reversed_method) = vm.get_method(obj.clone(), "__reversed__") {
714-
vm.invoke(reversed_method?, PyFuncArgs::default())
714+
vm.invoke(&reversed_method?, PyFuncArgs::default())
715715
} else {
716716
vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
717717
"argument to reversed() must be a sequence".to_string()
@@ -777,7 +777,7 @@ fn builtin_sum(iterable: PyIterable, start: OptionalArg, vm: &VirtualMachine) ->
777777

778778
// Should be renamed to builtin___import__?
779779
fn builtin_import(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
780-
vm.invoke(vm.import_func.borrow().clone(), args)
780+
vm.invoke(&vm.import_func.borrow(), args)
781781
}
782782

783783
fn builtin_vars(obj: OptionalArg, vm: &VirtualMachine) -> PyResult {
@@ -964,13 +964,13 @@ pub fn builtin_build_class_(
964964

965965
// Prepare uses full __getattribute__ resolution chain.
966966
let prepare = vm.get_attribute(metaclass.clone().into_object(), "__prepare__")?;
967-
let namespace = vm.invoke(prepare, vec![name_obj.clone(), bases.clone()])?;
967+
let namespace = vm.invoke(&prepare, vec![name_obj.clone(), bases.clone()])?;
968968

969969
let namespace: PyDictRef = TryFromObject::try_from_object(vm, namespace)?;
970970

971971
let cells = vm.ctx.new_dict();
972972

973-
vm.invoke_with_locals(function, cells.clone(), namespace.clone())?;
973+
vm.invoke_with_locals(&function, cells.clone(), namespace.clone())?;
974974

975975
namespace.set_item("__name__", name_obj.clone(), vm)?;
976976
namespace.set_item("__qualname__", qualified_name.into_object(), vm)?;

vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl Frame {
488488

489489
// Call function:
490490
let func_ref = self.pop_value();
491-
let value = vm.invoke(func_ref, args)?;
491+
let value = vm.invoke(&func_ref, args)?;
492492
self.push_value(value);
493493
Ok(None)
494494
}
@@ -599,7 +599,7 @@ impl Frame {
599599
if !expr.is(&vm.get_none()) {
600600
let repr = vm.to_repr(&expr)?;
601601
// TODO: implement sys.displayhook
602-
if let Ok(print) = vm.get_attribute(vm.builtins.clone(), "print") {
602+
if let Ok(ref print) = vm.get_attribute(vm.builtins.clone(), "print") {
603603
vm.invoke(print, vec![repr.into_object()])?;
604604
}
605605
}

vm/src/import.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
1717
let importlib = import_frozen(vm, "_frozen_importlib")?;
1818
let impmod = import_builtin(vm, "_imp")?;
1919
let install = vm.get_attribute(importlib.clone(), "_install")?;
20-
vm.invoke(install, vec![vm.sys_module.clone(), impmod])?;
20+
vm.invoke(&install, vec![vm.sys_module.clone(), impmod])?;
2121
vm.import_func
2222
.replace(vm.get_attribute(importlib.clone(), "__import__")?);
2323
if external && cfg!(feature = "rustpython-compiler") {
2424
flame_guard!("install_external");
2525
let install_external =
2626
vm.get_attribute(importlib.clone(), "_install_external_importers")?;
27-
vm.invoke(install_external, vec![])?;
27+
vm.invoke(&install_external, vec![])?;
2828
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
2929
let importlib_external =
3030
vm.import("_frozen_importlib_external", &vm.ctx.new_tuple(vec![]), 0)?;

vm/src/obj/objbool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
2828
Some(method_or_err) => {
2929
// If descriptor returns Error, propagate it further
3030
let method = method_or_err?;
31-
let bool_obj = vm.invoke(method, PyFuncArgs::default())?;
31+
let bool_obj = vm.invoke(&method, PyFuncArgs::default())?;
3232
match bool_obj.payload::<PyInt>() {
3333
Some(int_obj) => !int_obj.as_bigint().is_zero(),
3434
None => {
@@ -42,7 +42,7 @@ pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
4242
None => match vm.get_method(obj.clone(), "__len__") {
4343
Some(method_or_err) => {
4444
let method = method_or_err?;
45-
let bool_obj = vm.invoke(method, PyFuncArgs::default())?;
45+
let bool_obj = vm.invoke(&method, PyFuncArgs::default())?;
4646
match bool_obj.payload::<PyInt>() {
4747
Some(int_obj) => !int_obj.as_bigint().is_zero(),
4848
None => {

vm/src/obj/objdict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl PyDictRef {
210210
}
211211
if let Some(method_or_err) = vm.get_method(self.clone().into_object(), "__missing__") {
212212
let method = method_or_err?;
213-
return vm.invoke(method, vec![key]);
213+
return vm.invoke(&method, vec![key]);
214214
}
215215
Err(vm.new_key_error(key.clone()))
216216
}

vm/src/obj/objfilter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl PyFilter {
5252
} else {
5353
// the predicate itself can raise StopIteration which does stop the filter
5454
// iteration
55-
vm.invoke(predicate.clone(), vec![next_obj.clone()])?
55+
vm.invoke(&predicate, vec![next_obj.clone()])?
5656
};
5757
if objbool::boolval(vm, predicate_value)? {
5858
return Ok(next_obj);

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ pub fn make_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
612612
obj.class().name
613613
)
614614
})?;
615-
let result = vm.invoke(method, vec![])?;
615+
let result = vm.invoke(&method, vec![])?;
616616
Ok(get_value(&result))
617617
}
618618
}

vm/src/obj/objfunction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl PyValue for PyFunction {
4242

4343
impl PyFunctionRef {
4444
fn call(self, args: Args, kwargs: KwArgs, vm: &VirtualMachine) -> PyResult {
45-
vm.invoke(self.into_object(), (&args, &kwargs))
45+
vm.invoke(&self.into_object(), (&args, &kwargs))
4646
}
4747

4848
fn code(self, _vm: &VirtualMachine) -> PyCodeRef {

vm/src/obj/objint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, mut base: u32) -> PyResult
724724
let method = vm.get_method_or_type_error(obj.clone(), "__int__", || {
725725
format!("int() argument must be a string or a number, not '{}'", obj.class().name)
726726
})?;
727-
let result = vm.invoke(method, PyFuncArgs::default())?;
727+
let result = vm.invoke(&method, PyFuncArgs::default())?;
728728
match result.payload::<PyInt>() {
729729
Some(int_obj) => Ok(int_obj.as_bigint().clone()),
730730
None => Err(vm.new_type_error(format!(

vm/src/obj/objiter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::objtype::PyClassRef;
2020
pub fn get_iter(vm: &VirtualMachine, iter_target: &PyObjectRef) -> PyResult {
2121
if let Some(method_or_err) = vm.get_method(iter_target.clone(), "__iter__") {
2222
let method = method_or_err?;
23-
vm.invoke(method, vec![])
23+
vm.invoke(&method, vec![])
2424
} else {
2525
vm.get_method_or_type_error(iter_target.clone(), "__getitem__", || {
2626
format!("Cannot iterate over {}", iter_target.class().name)

vm/src/obj/objlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ fn do_sort(
755755
for x in values.iter() {
756756
keys.push(match &key_func {
757757
None => x.clone(),
758-
Some(ref func) => vm.invoke((*func).clone(), vec![x.clone()])?,
758+
Some(ref func) => vm.invoke(func, vec![x.clone()])?,
759759
});
760760
}
761761

vm/src/obj/objmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl PyMap {
5151
.collect::<Result<Vec<_>, _>>()?;
5252

5353
// the mapper itself can raise StopIteration which does stop the map iteration
54-
vm.invoke(self.mapper.clone(), next_objs)
54+
vm.invoke(&self.mapper, next_objs)
5555
}
5656

5757
#[pymethod(name = "__iter__")]

vm/src/obj/objnone.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl PyNoneRef {
6464
if let Ok(property) = PyPropertyRef::try_from_object(vm, descriptor.clone()) {
6565
property.instance_binding_get(obj, vm)
6666
} else {
67-
vm.invoke(get_func, vec![descriptor, obj, cls])
67+
vm.invoke(&get_func, vec![descriptor, obj, cls])
6868
}
6969
}
7070

@@ -95,7 +95,7 @@ impl PyNoneRef {
9595
Ok(attr)
9696
}
9797
} else if let Some(getter) = class_get_attr(&cls, "__getattr__") {
98-
vm.invoke(getter, vec![self.into_object(), name.into_object()])
98+
vm.invoke(&getter, vec![self.into_object(), name.into_object()])
9999
} else {
100100
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", self.as_object(), name)))
101101
}

vm/src/obj/objobject.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn object_setattr(
7272
if let Some(attr) = objtype::class_get_attr(&cls, &attr_name.value) {
7373
if let Some(descriptor) = objtype::class_get_attr(&attr.class(), "__set__") {
7474
return vm
75-
.invoke(descriptor, vec![attr, obj.clone(), value])
75+
.invoke(&descriptor, vec![attr, obj.clone(), value])
7676
.map(|_| ());
7777
}
7878
}
@@ -94,7 +94,7 @@ fn object_delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine)
9494

9595
if let Some(attr) = objtype::class_get_attr(&cls, &attr_name.value) {
9696
if let Some(descriptor) = objtype::class_get_attr(&attr.class(), "__delete__") {
97-
return vm.invoke(descriptor, vec![attr, obj.clone()]).map(|_| ());
97+
return vm.invoke(&descriptor, vec![attr, obj.clone()]).map(|_| ());
9898
}
9999
}
100100

@@ -126,7 +126,7 @@ pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> {
126126
// Get instance attributes:
127127
if let Some(object_dict) = &obj.dict {
128128
vm.invoke(
129-
vm.get_attribute(dict.clone().into_object(), "update")?,
129+
&vm.get_attribute(dict.clone().into_object(), "update")?,
130130
object_dict.clone().into_object(),
131131
)?;
132132
}
@@ -227,7 +227,7 @@ fn object_getattribute(obj: PyObjectRef, name_str: PyStringRef, vm: &VirtualMach
227227
let attr_class = attr.class();
228228
if objtype::class_has_attr(&attr_class, "__set__") {
229229
if let Some(descriptor) = objtype::class_get_attr(&attr_class, "__get__") {
230-
return vm.invoke(descriptor, vec![attr, obj, cls.into_object()]);
230+
return vm.invoke(&descriptor, vec![attr, obj, cls.into_object()]);
231231
}
232232
}
233233
}
@@ -236,7 +236,7 @@ fn object_getattribute(obj: PyObjectRef, name_str: PyStringRef, vm: &VirtualMach
236236
Ok(obj_attr)
237237
} else if let Some(attr) = objtype::class_get_attr(&cls, &name) {
238238
vm.call_get_descriptor(attr, obj)
239-
} else if let Some(getter) = objtype::class_get_attr(&cls, "__getattr__") {
239+
} else if let Some(ref getter) = objtype::class_get_attr(&cls, "__getattr__") {
240240
vm.invoke(getter, vec![obj, name_str.into_object()])
241241
} else {
242242
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))

vm/src/obj/objproperty.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl PyReadOnlyProperty {
3737
if obj.is(vm.ctx.none.as_object()) {
3838
Ok(zelf.into_object())
3939
} else {
40-
vm.invoke(zelf.getter.clone(), obj)
40+
vm.invoke(&zelf.getter, obj)
4141
}
4242
}
4343
}
@@ -123,8 +123,8 @@ impl PyProperty {
123123

124124
// specialised version that doesn't check for None
125125
pub(crate) fn instance_binding_get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
126-
if let Some(getter) = self.getter.as_ref() {
127-
vm.invoke(getter.clone(), obj)
126+
if let Some(ref getter) = self.getter.as_ref() {
127+
vm.invoke(getter, obj)
128128
} else {
129129
Err(vm.new_attribute_error("unreadable attribute".to_string()))
130130
}
@@ -141,7 +141,7 @@ impl PyProperty {
141141
if obj.is(vm.ctx.none.as_object()) {
142142
Ok(zelf.into_object())
143143
} else {
144-
vm.invoke(getter.clone(), obj)
144+
vm.invoke(&getter, obj)
145145
}
146146
} else {
147147
Err(vm.new_attribute_error("unreadable attribute".to_string()))
@@ -150,17 +150,17 @@ impl PyProperty {
150150

151151
#[pymethod(name = "__set__")]
152152
fn set(&self, obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
153-
if let Some(setter) = self.setter.as_ref() {
154-
vm.invoke(setter.clone(), vec![obj, value])
153+
if let Some(ref setter) = self.setter.as_ref() {
154+
vm.invoke(setter, vec![obj, value])
155155
} else {
156156
Err(vm.new_attribute_error("can't set attribute".to_string()))
157157
}
158158
}
159159

160160
#[pymethod(name = "__delete__")]
161161
fn delete(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
162-
if let Some(deleter) = self.deleter.as_ref() {
163-
vm.invoke(deleter.clone(), obj)
162+
if let Some(ref deleter) = self.deleter.as_ref() {
163+
vm.invoke(deleter, obj)
164164
} else {
165165
Err(vm.new_attribute_error("can't delete attribute".to_string()))
166166
}

vm/src/obj/objtype.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl PyClassRef {
137137
if let Some(attr) = class_get_attr(&mcl, &name) {
138138
let attr_class = attr.class();
139139
if class_has_attr(&attr_class, "__set__") {
140-
if let Some(descriptor) = class_get_attr(&attr_class, "__get__") {
140+
if let Some(ref descriptor) = class_get_attr(&attr_class, "__get__") {
141141
return vm.invoke(
142142
descriptor,
143143
vec![attr, self.into_object(), mcl.into_object()],
@@ -148,7 +148,7 @@ impl PyClassRef {
148148

149149
if let Some(attr) = class_get_attr(&self, &name) {
150150
let attr_class = attr.class();
151-
if let Some(descriptor) = class_get_attr(&attr_class, "__get__") {
151+
if let Some(ref descriptor) = class_get_attr(&attr_class, "__get__") {
152152
let none = vm.get_none();
153153
return vm.invoke(descriptor, vec![attr, none, self.into_object()]);
154154
}
@@ -158,7 +158,7 @@ impl PyClassRef {
158158
Ok(cls_attr)
159159
} else if let Some(attr) = class_get_attr(&mcl, &name) {
160160
vm.call_get_descriptor(attr, self.into_object())
161-
} else if let Some(getter) = class_get_attr(&self, "__getattr__") {
161+
} else if let Some(ref getter) = class_get_attr(&self, "__getattr__") {
162162
vm.invoke(getter, vec![mcl.into_object(), name_ref.into_object()])
163163
} else {
164164
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", self, name)))
@@ -172,7 +172,7 @@ impl PyClassRef {
172172
vm: &VirtualMachine,
173173
) -> PyResult<()> {
174174
if let Some(attr) = class_get_attr(&self.class(), &attr_name.value) {
175-
if let Some(descriptor) = class_get_attr(&attr.class(), "__set__") {
175+
if let Some(ref descriptor) = class_get_attr(&attr.class(), "__set__") {
176176
vm.invoke(descriptor, vec![attr, self.into_object(), value])?;
177177
return Ok(());
178178
}
@@ -288,11 +288,11 @@ pub fn type_call(class: PyClassRef, args: Args, kwargs: KwArgs, vm: &VirtualMach
288288
vm_trace!("type_call: {:?}", class);
289289
let new = class_get_attr(&class, "__new__").expect("All types should have a __new__.");
290290
let new_wrapped = vm.call_get_descriptor(new, class.into_object())?;
291-
let obj = vm.invoke(new_wrapped, (&args, &kwargs))?;
291+
let obj = vm.invoke(&new_wrapped, (&args, &kwargs))?;
292292

293293
if let Some(init_method_or_err) = vm.get_method(obj.clone(), "__init__") {
294294
let init_method = init_method_or_err?;
295-
let res = vm.invoke(init_method, (&args, &kwargs))?;
295+
let res = vm.invoke(&init_method, (&args, &kwargs))?;
296296
if !res.is(&vm.get_none()) {
297297
return Err(vm.new_type_error("__init__ must return None".to_string()));
298298
}

0 commit comments

Comments
 (0)