Skip to content

Commit 2d19486

Browse files
Merge pull request #520 from RustPython/py_module
Py module
2 parents 2df7414 + 0cbcc70 commit 2d19486

File tree

4 files changed

+108
-153
lines changed

4 files changed

+108
-153
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 {

vm/src/obj/objint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ fn int_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
615615
}
616616

617617
fn int_imag(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
618-
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.int_type()))]);
618+
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.int_type()))]);
619619
let value = BigInt::from(0);
620620
Ok(vm.ctx.new_int(value))
621621
}

vm/src/stdlib/dis.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::super::obj::objcode;
2-
use super::super::obj::objtype;
3-
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
4-
use super::super::vm::VirtualMachine;
1+
use crate::obj::objcode;
2+
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
3+
use crate::vm::VirtualMachine;
54

65
fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
76
arg_check!(vm, args, required = [(co, Some(vm.ctx.code_type()))]);
@@ -12,7 +11,7 @@ fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1211
}
1312

1413
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
15-
let py_mod = ctx.new_module("dis", ctx.new_scope(None));
16-
ctx.set_attr(&py_mod, "disassemble", ctx.new_rustfunc(dis_disassemble));
17-
py_mod
14+
py_module!(ctx, "dis", {
15+
"disassemble" => ctx.new_rustfunc(dis_disassemble)
16+
})
1817
}

vm/src/sysmodule.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
4949
};
5050
let path = ctx.new_list(path_list);
5151

52-
let modules = ctx.new_dict();
53-
54-
let sys_name = "sys";
5552
let sys_doc = "This module provides access to some objects used or maintained by the
5653
interpreter and to functions that interact strongly with the interpreter.
5754
@@ -121,20 +118,22 @@ setprofile() -- set the global profiling function
121118
setrecursionlimit() -- set the max recursion depth for the interpreter
122119
settrace() -- set the global debug tracing function
123120
";
124-
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
121+
let modules = ctx.new_dict();
122+
let sys_name = "sys";
123+
let sys_mod = py_module!(ctx, sys_name, {
124+
"argv" => argv(ctx),
125+
"getrefcount" => ctx.new_rustfunc(sys_getrefcount),
126+
"getsizeof" => ctx.new_rustfunc(sys_getsizeof),
127+
"maxsize" => ctx.new_int(std::usize::MAX),
128+
"path" => path,
129+
"ps1" => ctx.new_str(">>>>> ".to_string()),
130+
"ps2" => ctx.new_str("..... ".to_string()),
131+
"__doc__" => ctx.new_str(sys_doc.to_string()),
132+
"_getframe" => ctx.new_rustfunc(getframe),
133+
});
125134

126135
ctx.set_item(&modules, sys_name, sys_mod.clone());
127-
128136
ctx.set_attr(&sys_mod, "modules", modules);
129-
ctx.set_attr(&sys_mod, "argv", argv(ctx));
130-
ctx.set_attr(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
131-
ctx.set_attr(&sys_mod, "getsizeof", ctx.new_rustfunc(sys_getsizeof));
132-
ctx.set_attr(&sys_mod, "maxsize", ctx.new_int(std::usize::MAX));
133-
ctx.set_attr(&sys_mod, "path", path);
134-
ctx.set_attr(&sys_mod, "ps1", ctx.new_str(">>>>> ".to_string()));
135-
ctx.set_attr(&sys_mod, "ps2", ctx.new_str("..... ".to_string()));
136-
ctx.set_attr(&sys_mod, "__doc__", ctx.new_str(sys_doc.to_string()));
137-
ctx.set_attr(&sys_mod, "_getframe", ctx.new_rustfunc(getframe));
138137

139138
sys_mod
140139
}

0 commit comments

Comments
 (0)