From fa8d3524bfd341ab2f44408f75fb2c61632b9214 Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Sat, 23 Mar 2019 17:49:49 +0100 Subject: [PATCH] Use extend_class macro even more --- vm/src/exceptions.rs | 17 ++++++-------- vm/src/obj/objgenerator.rs | 16 ++++--------- vm/src/obj/objiter.rs | 20 ++++++++-------- vm/src/obj/objobject.rs | 47 +++++++++++++++++--------------------- vm/src/obj/objsuper.rs | 16 ++++--------- vm/src/obj/objtuple.rs | 38 +++++++++++++++--------------- 6 files changed, 69 insertions(+), 85 deletions(-) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 332629964..3eb962edf 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -159,15 +159,12 @@ impl ExceptionZoo { pub fn init(context: &PyContext) { let base_exception_type = &context.exceptions.base_exception_type; - context.set_attr( - base_exception_type, - "__init__", - context.new_rustfunc(exception_init), - ); + extend_class!(context, base_exception_type, { + "__init__" => context.new_rustfunc(exception_init) + }); + let exception_type = &context.exceptions.exception_type; - context.set_attr( - exception_type, - "__str__", - context.new_rustfunc(exception_str), - ); + extend_class!(context, exception_type, { + "__str__" => context.new_rustfunc(exception_str) + }); } diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index ccb19a32c..b766aafd4 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -22,17 +22,11 @@ impl PyValue for PyGenerator { pub fn init(context: &PyContext) { let generator_type = &context.generator_type; - context.set_attr( - generator_type, - "__iter__", - context.new_rustfunc(generator_iter), - ); - context.set_attr( - generator_type, - "__next__", - context.new_rustfunc(generator_next), - ); - context.set_attr(generator_type, "send", context.new_rustfunc(generator_send)); + extend_class!(context, generator_type, { + "__iter__" => context.new_rustfunc(generator_iter), + "__next__" => context.new_rustfunc(generator_next), + "send" => context.new_rustfunc(generator_send) + }); } pub fn new_generator(frame: PyObjectRef, vm: &VirtualMachine) -> PyGeneratorRef { diff --git a/vm/src/obj/objiter.rs b/vm/src/obj/objiter.rs index 7c2341173..282ccf146 100644 --- a/vm/src/obj/objiter.rs +++ b/vm/src/obj/objiter.rs @@ -96,11 +96,7 @@ pub fn iter_type_init(context: &PyContext, iter_type: &PyClassRef) { let cloned_iter_type = iter_type.clone(); move |vm: &VirtualMachine, args: PyFuncArgs| contains(vm, args, cloned_iter_type.clone()) }; - context.set_attr( - iter_type, - "__contains__", - context.new_rustfunc(contains_func), - ); + let iter_func = { let cloned_iter_type = iter_type.clone(); move |vm: &VirtualMachine, args: PyFuncArgs| { @@ -113,7 +109,11 @@ pub fn iter_type_init(context: &PyContext, iter_type: &PyClassRef) { Ok(iter.clone()) } }; - context.set_attr(iter_type, "__iter__", context.new_rustfunc(iter_func)); + + extend_class!(context, iter_type, { + "__contains__" => context.new_rustfunc(contains_func), + "__iter__" => context.new_rustfunc(iter_func) + }); } // Sequence iterator: @@ -179,7 +179,9 @@ pub fn init(context: &PyContext) { In the second form, the callable is called until it returns the sentinel."; iter_type_init(context, iter_type); - context.set_attr(iter_type, "__new__", context.new_rustfunc(iter_new)); - context.set_attr(iter_type, "__next__", context.new_rustfunc(iter_next)); - context.set_attr(iter_type, "__doc__", context.new_str(iter_doc.to_string())); + extend_class!(context, iter_type, { + "__new__" => context.new_rustfunc(iter_new), + "__next__" => context.new_rustfunc(iter_next), + "__doc__" => context.new_str(iter_doc.to_string()) + }); } diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index 81fdc0b3f..e0ff9b20c 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -183,36 +183,31 @@ pub fn init(context: &PyContext) { let object = &context.object; let object_doc = "The most base type"; - context.set_attr(object, "__new__", context.new_rustfunc(new_instance)); - context.set_attr(object, "__init__", context.new_rustfunc(object_init)); - context.set_attr( - object, - "__class__", + extend_class!(context, object, { + "__new__" => context.new_rustfunc(new_instance), + "__init__" => context.new_rustfunc(object_init), + "__class__" => PropertyBuilder::new(context) .add_getter(object_class) .add_setter(object_class_setter) .create(), - ); - context.set_attr(object, "__eq__", context.new_rustfunc(object_eq)); - context.set_attr(object, "__ne__", context.new_rustfunc(object_ne)); - context.set_attr(object, "__lt__", context.new_rustfunc(object_lt)); - context.set_attr(object, "__le__", context.new_rustfunc(object_le)); - context.set_attr(object, "__gt__", context.new_rustfunc(object_gt)); - context.set_attr(object, "__ge__", context.new_rustfunc(object_ge)); - context.set_attr(object, "__setattr__", context.new_rustfunc(object_setattr)); - context.set_attr(object, "__delattr__", context.new_rustfunc(object_delattr)); - context.set_attr(object, "__dict__", context.new_property(object_dict)); - context.set_attr(object, "__dir__", context.new_rustfunc(object_dir)); - context.set_attr(object, "__hash__", context.new_rustfunc(object_hash)); - context.set_attr(object, "__str__", context.new_rustfunc(object_str)); - context.set_attr(object, "__repr__", context.new_rustfunc(object_repr)); - context.set_attr(object, "__format__", context.new_rustfunc(object_format)); - context.set_attr( - object, - "__getattribute__", - context.new_rustfunc(object_getattribute), - ); - context.set_attr(object, "__doc__", context.new_str(object_doc.to_string())); + "__eq__" => context.new_rustfunc(object_eq), + "__ne__" => context.new_rustfunc(object_ne), + "__lt__" => context.new_rustfunc(object_lt), + "__le__" => context.new_rustfunc(object_le), + "__gt__" => context.new_rustfunc(object_gt), + "__ge__" => context.new_rustfunc(object_ge), + "__setattr__" => context.new_rustfunc(object_setattr), + "__delattr__" => context.new_rustfunc(object_delattr), + "__dict__" => context.new_property(object_dict), + "__dir__" => context.new_rustfunc(object_dir), + "__hash__" => context.new_rustfunc(object_hash), + "__str__" => context.new_rustfunc(object_str), + "__repr__" => context.new_rustfunc(object_repr), + "__format__" => context.new_rustfunc(object_format), + "__getattribute__" => context.new_rustfunc(object_getattribute), + "__doc__" => context.new_str(object_doc.to_string()) + }); } fn object_init(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objsuper.rs b/vm/src/obj/objsuper.rs index 97fdd367e..c3fba5eeb 100644 --- a/vm/src/obj/objsuper.rs +++ b/vm/src/obj/objsuper.rs @@ -48,17 +48,11 @@ pub fn init(context: &PyContext) { def cmeth(cls, arg):\n \ super().cmeth(arg)\n"; - context.set_attr(super_type, "__new__", context.new_rustfunc(super_new)); - context.set_attr( - super_type, - "__getattribute__", - context.new_rustfunc(super_getattribute), - ); - context.set_attr( - super_type, - "__doc__", - context.new_str(super_doc.to_string()), - ); + extend_class!(context, super_type, { + "__new__" => context.new_rustfunc(super_new), + "__getattribute__" => context.new_rustfunc(super_getattribute), + "__doc__" => context.new_str(super_doc.to_string()), + }); } fn super_getattribute(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 92a90913a..e5bf5c1ee 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -233,22 +233,24 @@ pub fn init(context: &PyContext) { tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object."; - context.set_attr(tuple_type, "__add__", context.new_rustfunc(PyTupleRef::add)); - context.set_attr(tuple_type, "__bool__", context.new_rustfunc(PyTupleRef::bool)); - context.set_attr(tuple_type, "__eq__", context.new_rustfunc(PyTupleRef::eq)); - context.set_attr(tuple_type,"__contains__",context.new_rustfunc(PyTupleRef::contains)); - context.set_attr(tuple_type,"__getitem__",context.new_rustfunc(PyTupleRef::getitem)); - context.set_attr(tuple_type, "__hash__", context.new_rustfunc(PyTupleRef::hash)); - context.set_attr(tuple_type, "__iter__", context.new_rustfunc(PyTupleRef::iter)); - context.set_attr(tuple_type, "__len__", context.new_rustfunc(PyTupleRef::len)); - context.set_attr(tuple_type, "__new__", context.new_rustfunc(tuple_new)); - context.set_attr(tuple_type, "__mul__", context.new_rustfunc(PyTupleRef::mul)); - context.set_attr(tuple_type, "__repr__", context.new_rustfunc(PyTupleRef::repr)); - context.set_attr(tuple_type, "count", context.new_rustfunc(PyTupleRef::count)); - context.set_attr(tuple_type, "__lt__", context.new_rustfunc(PyTupleRef::lt)); - context.set_attr(tuple_type, "__le__", context.new_rustfunc(PyTupleRef::le)); - context.set_attr(tuple_type, "__gt__", context.new_rustfunc(PyTupleRef::gt)); - context.set_attr(tuple_type, "__ge__", context.new_rustfunc(PyTupleRef::ge)); - context.set_attr(tuple_type,"__doc__",context.new_str(tuple_doc.to_string())); - context.set_attr(tuple_type, "index", context.new_rustfunc(PyTupleRef::index)); + extend_class!(context, tuple_type, { + "__add__" => context.new_rustfunc(PyTupleRef::add), + "__bool__" => context.new_rustfunc(PyTupleRef::bool), + "__eq__" => context.new_rustfunc(PyTupleRef::eq), + "__contains__" => context.new_rustfunc(PyTupleRef::contains), + "__getitem__" => context.new_rustfunc(PyTupleRef::getitem), + "__hash__" => context.new_rustfunc(PyTupleRef::hash), + "__iter__" => context.new_rustfunc(PyTupleRef::iter), + "__len__" => context.new_rustfunc(PyTupleRef::len), + "__new__" => context.new_rustfunc(tuple_new), + "__mul__" => context.new_rustfunc(PyTupleRef::mul), + "__repr__" => context.new_rustfunc(PyTupleRef::repr), + "count" => context.new_rustfunc(PyTupleRef::count), + "__lt__" => context.new_rustfunc(PyTupleRef::lt), + "__le__" => context.new_rustfunc(PyTupleRef::le), + "__gt__" => context.new_rustfunc(PyTupleRef::gt), + "__ge__" => context.new_rustfunc(PyTupleRef::ge), + "__doc__" => context.new_str(tuple_doc.to_string()), + "index" => context.new_rustfunc(PyTupleRef::index) + }); }