Skip to content

Commit e048037

Browse files
Merge pull request RustPython#756 from adrian17/objobject
Convert objobject.rs to new args style
2 parents c2eb664 + 89f63e4 commit e048037

File tree

2 files changed

+31
-82
lines changed

2 files changed

+31
-82
lines changed

vm/src/obj/objfunction.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::frame::Scope;
2-
use crate::function::PyFuncArgs;
32
use crate::obj::objcode::PyCodeRef;
43
use crate::obj::objtype::PyClassRef;
54
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
@@ -69,16 +68,15 @@ pub fn init(context: &PyContext) {
6968
});
7069
}
7170

72-
fn bind_method(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
73-
arg_check!(
74-
vm,
75-
args,
76-
required = [(function, None), (obj, None), (cls, None)]
77-
);
78-
71+
fn bind_method(
72+
function: PyObjectRef,
73+
obj: PyObjectRef,
74+
cls: PyObjectRef,
75+
vm: &VirtualMachine,
76+
) -> PyResult {
7977
if obj.is(&vm.get_none()) && !cls.is(&obj.class()) {
80-
Ok(function.clone())
78+
Ok(function)
8179
} else {
82-
Ok(vm.ctx.new_bound_method(function.clone(), obj.clone()))
80+
Ok(vm.ctx.new_bound_method(function, obj))
8381
}
8482
}

vm/src/obj/objobject.rs

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::objdict::{self, PyDictRef};
22
use super::objlist::PyList;
3-
use super::objstr::{self, PyStringRef};
3+
use super::objstr::PyStringRef;
44
use super::objtype;
55
use crate::function::PyFuncArgs;
66
use crate::obj::objproperty::PropertyBuilder;
@@ -31,69 +31,31 @@ pub fn new_instance(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
3131
Ok(PyObject::new(PyInstance, cls, dict))
3232
}
3333

34-
fn object_eq(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
35-
arg_check!(
36-
vm,
37-
args,
38-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
39-
);
40-
Ok(vm.ctx.not_implemented())
34+
fn object_eq(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
35+
vm.ctx.not_implemented()
4136
}
4237

43-
fn object_ne(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
44-
arg_check!(
45-
vm,
46-
args,
47-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
48-
);
49-
50-
Ok(vm.ctx.not_implemented())
38+
fn object_ne(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
39+
vm.ctx.not_implemented()
5140
}
5241

53-
fn object_lt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
54-
arg_check!(
55-
vm,
56-
args,
57-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
58-
);
59-
60-
Ok(vm.ctx.not_implemented())
42+
fn object_lt(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
43+
vm.ctx.not_implemented()
6144
}
6245

63-
fn object_le(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
64-
arg_check!(
65-
vm,
66-
args,
67-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
68-
);
69-
70-
Ok(vm.ctx.not_implemented())
46+
fn object_le(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
47+
vm.ctx.not_implemented()
7148
}
7249

73-
fn object_gt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
74-
arg_check!(
75-
vm,
76-
args,
77-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
78-
);
79-
80-
Ok(vm.ctx.not_implemented())
50+
fn object_gt(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
51+
vm.ctx.not_implemented()
8152
}
8253

83-
fn object_ge(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
84-
arg_check!(
85-
vm,
86-
args,
87-
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
88-
);
89-
90-
Ok(vm.ctx.not_implemented())
54+
fn object_ge(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
55+
vm.ctx.not_implemented()
9156
}
9257

93-
fn object_hash(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
94-
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.object()))]);
95-
96-
// For now default to non hashable
58+
fn object_hash(_zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
9759
Err(vm.new_type_error("unhashable type".to_string()))
9860
}
9961

@@ -147,15 +109,12 @@ fn object_delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine)
147109
}
148110
}
149111

150-
fn object_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
151-
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.object()))]);
152-
vm.call_method(zelf, "__repr__", vec![])
112+
fn object_str(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
113+
vm.call_method(&zelf, "__repr__", vec![])
153114
}
154115

155-
fn object_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
156-
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
157-
let address = obj.get_id();
158-
Ok(vm.new_str(format!("<{} object at 0x{:x}>", obj.class().name, address)))
116+
fn object_repr(zelf: PyObjectRef, _vm: &VirtualMachine) -> String {
117+
format!("<{} object at 0x{:x}>", zelf.class().name, zelf.get_id())
159118
}
160119

161120
pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyList {
@@ -235,34 +194,26 @@ fn object_dict(object: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDictRef>
235194
}
236195
}
237196

238-
fn object_getattribute(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
239-
arg_check!(
240-
vm,
241-
args,
242-
required = [
243-
(obj, Some(vm.ctx.object())),
244-
(name_str, Some(vm.ctx.str_type()))
245-
]
246-
);
247-
let name = objstr::get_value(&name_str);
197+
fn object_getattribute(obj: PyObjectRef, name_str: PyStringRef, vm: &VirtualMachine) -> PyResult {
198+
let name = &name_str.value;
248199
trace!("object.__getattribute__({:?}, {:?})", obj, name);
249200
let cls = obj.class();
250201

251202
if let Some(attr) = objtype::class_get_attr(&cls, &name) {
252203
let attr_class = attr.class();
253204
if objtype::class_has_attr(&attr_class, "__set__") {
254205
if let Some(descriptor) = objtype::class_get_attr(&attr_class, "__get__") {
255-
return vm.invoke(descriptor, vec![attr, obj.clone(), cls.into_object()]);
206+
return vm.invoke(descriptor, vec![attr, obj, cls.into_object()]);
256207
}
257208
}
258209
}
259210

260211
if let Some(obj_attr) = object_getattr(&obj, &name) {
261212
Ok(obj_attr)
262213
} else if let Some(attr) = objtype::class_get_attr(&cls, &name) {
263-
vm.call_get_descriptor(attr, obj.clone())
214+
vm.call_get_descriptor(attr, obj)
264215
} else if let Some(getter) = objtype::class_get_attr(&cls, "__getattr__") {
265-
vm.invoke(getter, vec![obj.clone(), name_str.clone()])
216+
vm.invoke(getter, vec![obj, name_str.into_object()])
266217
} else {
267218
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
268219
}

0 commit comments

Comments
 (0)