Skip to content

Commit 0cbcc70

Browse files
committed
builtins - use py_module.
1 parent 2ab90b2 commit 0cbcc70

File tree

1 file changed

+88
-131
lines changed

1 file changed

+88
-131
lines changed

vm/src/builtins.rs

Lines changed: 88 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -702,137 +702,94 @@ fn builtin_sum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
702702
// builtin___import__
703703

704704
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
705-
let mod_name = "__builtins__";
706-
let py_mod = ctx.new_module(mod_name, ctx.new_scope(None));
707-
708-
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
709-
ctx.set_attr(&py_mod, "__name__", ctx.new_str(String::from("__main__")));
710-
711-
ctx.set_attr(&py_mod, "abs", ctx.new_rustfunc(builtin_abs));
712-
ctx.set_attr(&py_mod, "all", ctx.new_rustfunc(builtin_all));
713-
ctx.set_attr(&py_mod, "any", ctx.new_rustfunc(builtin_any));
714-
ctx.set_attr(&py_mod, "bin", ctx.new_rustfunc(builtin_bin));
715-
ctx.set_attr(&py_mod, "bool", ctx.bool_type());
716-
ctx.set_attr(&py_mod, "bytearray", ctx.bytearray_type());
717-
ctx.set_attr(&py_mod, "bytes", ctx.bytes_type());
718-
ctx.set_attr(&py_mod, "callable", ctx.new_rustfunc(builtin_callable));
719-
ctx.set_attr(&py_mod, "chr", ctx.new_rustfunc(builtin_chr));
720-
ctx.set_attr(&py_mod, "classmethod", ctx.classmethod_type());
721-
ctx.set_attr(&py_mod, "compile", ctx.new_rustfunc(builtin_compile));
722-
ctx.set_attr(&py_mod, "complex", ctx.complex_type());
723-
ctx.set_attr(&py_mod, "delattr", ctx.new_rustfunc(builtin_delattr));
724-
ctx.set_attr(&py_mod, "dict", ctx.dict_type());
725-
ctx.set_attr(&py_mod, "divmod", ctx.new_rustfunc(builtin_divmod));
726-
ctx.set_attr(&py_mod, "dir", ctx.new_rustfunc(builtin_dir));
727-
ctx.set_attr(&py_mod, "enumerate", ctx.enumerate_type());
728-
ctx.set_attr(&py_mod, "eval", ctx.new_rustfunc(builtin_eval));
729-
ctx.set_attr(&py_mod, "exec", ctx.new_rustfunc(builtin_exec));
730-
ctx.set_attr(&py_mod, "float", ctx.float_type());
731-
ctx.set_attr(&py_mod, "frozenset", ctx.frozenset_type());
732-
ctx.set_attr(&py_mod, "filter", ctx.filter_type());
733-
ctx.set_attr(&py_mod, "format", ctx.new_rustfunc(builtin_format));
734-
ctx.set_attr(&py_mod, "getattr", ctx.new_rustfunc(builtin_getattr));
735-
ctx.set_attr(&py_mod, "hasattr", ctx.new_rustfunc(builtin_hasattr));
736-
ctx.set_attr(&py_mod, "hash", ctx.new_rustfunc(builtin_hash));
737-
ctx.set_attr(&py_mod, "hex", ctx.new_rustfunc(builtin_hex));
738-
ctx.set_attr(&py_mod, "id", ctx.new_rustfunc(builtin_id));
739-
ctx.set_attr(&py_mod, "int", ctx.int_type());
740-
ctx.set_attr(&py_mod, "isinstance", ctx.new_rustfunc(builtin_isinstance));
741-
ctx.set_attr(&py_mod, "issubclass", ctx.new_rustfunc(builtin_issubclass));
742-
ctx.set_attr(&py_mod, "iter", ctx.new_rustfunc(builtin_iter));
743-
ctx.set_attr(&py_mod, "len", ctx.new_rustfunc(builtin_len));
744-
ctx.set_attr(&py_mod, "list", ctx.list_type());
745-
ctx.set_attr(&py_mod, "locals", ctx.new_rustfunc(builtin_locals));
746-
ctx.set_attr(&py_mod, "map", ctx.map_type());
747-
ctx.set_attr(&py_mod, "max", ctx.new_rustfunc(builtin_max));
748-
ctx.set_attr(&py_mod, "memoryview", ctx.memoryview_type());
749-
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
750-
ctx.set_attr(&py_mod, "object", ctx.object());
751-
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
752-
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
753-
ctx.set_attr(&py_mod, "ord", ctx.new_rustfunc(builtin_ord));
754-
ctx.set_attr(&py_mod, "next", ctx.new_rustfunc(builtin_next));
755-
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(builtin_pow));
756-
ctx.set_attr(&py_mod, "print", ctx.new_rustfunc(builtin_print));
757-
ctx.set_attr(&py_mod, "property", ctx.property_type());
758-
ctx.set_attr(&py_mod, "range", ctx.range_type());
759-
ctx.set_attr(&py_mod, "repr", ctx.new_rustfunc(builtin_repr));
760-
ctx.set_attr(&py_mod, "reversed", ctx.new_rustfunc(builtin_reversed));
761-
ctx.set_attr(&py_mod, "round", ctx.new_rustfunc(builtin_round));
762-
ctx.set_attr(&py_mod, "set", ctx.set_type());
763-
ctx.set_attr(&py_mod, "setattr", ctx.new_rustfunc(builtin_setattr));
764-
ctx.set_attr(&py_mod, "slice", ctx.slice_type());
765-
ctx.set_attr(&py_mod, "staticmethod", ctx.staticmethod_type());
766-
ctx.set_attr(&py_mod, "str", ctx.str_type());
767-
ctx.set_attr(&py_mod, "sum", ctx.new_rustfunc(builtin_sum));
768-
ctx.set_attr(&py_mod, "super", ctx.super_type());
769-
ctx.set_attr(&py_mod, "tuple", ctx.tuple_type());
770-
ctx.set_attr(&py_mod, "type", ctx.type_type());
771-
ctx.set_attr(&py_mod, "zip", ctx.zip_type());
772-
773-
// Constants
774-
ctx.set_attr(&py_mod, "NotImplemented", ctx.not_implemented.clone());
775-
776-
// Exceptions:
777-
ctx.set_attr(
778-
&py_mod,
779-
"BaseException",
780-
ctx.exceptions.base_exception_type.clone(),
781-
);
782-
ctx.set_attr(&py_mod, "Exception", ctx.exceptions.exception_type.clone());
783-
ctx.set_attr(
784-
&py_mod,
785-
"ArithmeticError",
786-
ctx.exceptions.arithmetic_error.clone(),
787-
);
788-
ctx.set_attr(
789-
&py_mod,
790-
"AssertionError",
791-
ctx.exceptions.assertion_error.clone(),
792-
);
793-
ctx.set_attr(
794-
&py_mod,
795-
"AttributeError",
796-
ctx.exceptions.attribute_error.clone(),
797-
);
798-
ctx.set_attr(&py_mod, "NameError", ctx.exceptions.name_error.clone());
799-
ctx.set_attr(
800-
&py_mod,
801-
"OverflowError",
802-
ctx.exceptions.overflow_error.clone(),
803-
);
804-
ctx.set_attr(
805-
&py_mod,
806-
"RuntimeError",
807-
ctx.exceptions.runtime_error.clone(),
808-
);
809-
ctx.set_attr(
810-
&py_mod,
811-
"NotImplementedError",
812-
ctx.exceptions.not_implemented_error.clone(),
813-
);
814-
ctx.set_attr(&py_mod, "TypeError", ctx.exceptions.type_error.clone());
815-
ctx.set_attr(&py_mod, "ValueError", ctx.exceptions.value_error.clone());
816-
ctx.set_attr(&py_mod, "IndexError", ctx.exceptions.index_error.clone());
817-
ctx.set_attr(&py_mod, "ImportError", ctx.exceptions.import_error.clone());
818-
ctx.set_attr(
819-
&py_mod,
820-
"FileNotFoundError",
821-
ctx.exceptions.file_not_found_error.clone(),
822-
);
823-
ctx.set_attr(
824-
&py_mod,
825-
"StopIteration",
826-
ctx.exceptions.stop_iteration.clone(),
827-
);
828-
ctx.set_attr(
829-
&py_mod,
830-
"ZeroDivisionError",
831-
ctx.exceptions.zero_division_error.clone(),
832-
);
833-
ctx.set_attr(&py_mod, "KeyError", ctx.exceptions.key_error.clone());
834-
835-
py_mod
705+
py_module!(ctx, "__builtins__", {
706+
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
707+
"__name__" => ctx.new_str(String::from("__main__")),
708+
709+
"abs" => ctx.new_rustfunc(builtin_abs),
710+
"all" => ctx.new_rustfunc(builtin_all),
711+
"any" => ctx.new_rustfunc(builtin_any),
712+
"bin" => ctx.new_rustfunc(builtin_bin),
713+
"bool" => ctx.bool_type(),
714+
"bytearray" => ctx.bytearray_type(),
715+
"bytes" => ctx.bytes_type(),
716+
"callable" => ctx.new_rustfunc(builtin_callable),
717+
"chr" => ctx.new_rustfunc(builtin_chr),
718+
"classmethod" => ctx.classmethod_type(),
719+
"compile" => ctx.new_rustfunc(builtin_compile),
720+
"complex" => ctx.complex_type(),
721+
"delattr" => ctx.new_rustfunc(builtin_delattr),
722+
"dict" => ctx.dict_type(),
723+
"divmod" => ctx.new_rustfunc(builtin_divmod),
724+
"dir" => ctx.new_rustfunc(builtin_dir),
725+
"enumerate" => ctx.enumerate_type(),
726+
"eval" => ctx.new_rustfunc(builtin_eval),
727+
"exec" => ctx.new_rustfunc(builtin_exec),
728+
"float" => ctx.float_type(),
729+
"frozenset" => ctx.frozenset_type(),
730+
"filter" => ctx.filter_type(),
731+
"format" => ctx.new_rustfunc(builtin_format),
732+
"getattr" => ctx.new_rustfunc(builtin_getattr),
733+
"hasattr" => ctx.new_rustfunc(builtin_hasattr),
734+
"hash" => ctx.new_rustfunc(builtin_hash),
735+
"hex" => ctx.new_rustfunc(builtin_hex),
736+
"id" => ctx.new_rustfunc(builtin_id),
737+
"int" => ctx.int_type(),
738+
"isinstance" => ctx.new_rustfunc(builtin_isinstance),
739+
"issubclass" => ctx.new_rustfunc(builtin_issubclass),
740+
"iter" => ctx.new_rustfunc(builtin_iter),
741+
"len" => ctx.new_rustfunc(builtin_len),
742+
"list" => ctx.list_type(),
743+
"locals" => ctx.new_rustfunc(builtin_locals),
744+
"map" => ctx.map_type(),
745+
"max" => ctx.new_rustfunc(builtin_max),
746+
"memoryview" => ctx.memoryview_type(),
747+
"min" => ctx.new_rustfunc(builtin_min),
748+
"object" => ctx.object(),
749+
"oct" => ctx.new_rustfunc(builtin_oct),
750+
"open" => ctx.new_rustfunc(io_open),
751+
"ord" => ctx.new_rustfunc(builtin_ord),
752+
"next" => ctx.new_rustfunc(builtin_next),
753+
"pow" => ctx.new_rustfunc(builtin_pow),
754+
"print" => ctx.new_rustfunc(builtin_print),
755+
"property" => ctx.property_type(),
756+
"range" => ctx.range_type(),
757+
"repr" => ctx.new_rustfunc(builtin_repr),
758+
"reversed" => ctx.new_rustfunc(builtin_reversed),
759+
"round" => ctx.new_rustfunc(builtin_round),
760+
"set" => ctx.set_type(),
761+
"setattr" => ctx.new_rustfunc(builtin_setattr),
762+
"slice" => ctx.slice_type(),
763+
"staticmethod" => ctx.staticmethod_type(),
764+
"str" => ctx.str_type(),
765+
"sum" => ctx.new_rustfunc(builtin_sum),
766+
"super" => ctx.super_type(),
767+
"tuple" => ctx.tuple_type(),
768+
"type" => ctx.type_type(),
769+
"zip" => ctx.zip_type(),
770+
771+
// Constants
772+
"NotImplemented" => ctx.not_implemented.clone(),
773+
774+
// Exceptions:
775+
"BaseException" => ctx.exceptions.base_exception_type.clone(),
776+
"Exception" => ctx.exceptions.exception_type.clone(),
777+
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
778+
"AssertionError" => ctx.exceptions.assertion_error.clone(),
779+
"AttributeError" => ctx.exceptions.attribute_error.clone(),
780+
"NameError" => ctx.exceptions.name_error.clone(),
781+
"OverflowError" => ctx.exceptions.overflow_error.clone(),
782+
"RuntimeError" => ctx.exceptions.runtime_error.clone(),
783+
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
784+
"TypeError" => ctx.exceptions.type_error.clone(),
785+
"ValueError" => ctx.exceptions.value_error.clone(),
786+
"IndexError" => ctx.exceptions.index_error.clone(),
787+
"ImportError" => ctx.exceptions.import_error.clone(),
788+
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
789+
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
790+
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
791+
"KeyError" => ctx.exceptions.key_error.clone(),
792+
})
836793
}
837794

838795
pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)