Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 88 additions & 131 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,137 +702,94 @@ fn builtin_sum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// builtin___import__

pub fn make_module(ctx: &PyContext) -> PyObjectRef {
let mod_name = "__builtins__";
let py_mod = ctx.new_module(mod_name, ctx.new_scope(None));

//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
ctx.set_attr(&py_mod, "__name__", ctx.new_str(String::from("__main__")));

ctx.set_attr(&py_mod, "abs", ctx.new_rustfunc(builtin_abs));
ctx.set_attr(&py_mod, "all", ctx.new_rustfunc(builtin_all));
ctx.set_attr(&py_mod, "any", ctx.new_rustfunc(builtin_any));
ctx.set_attr(&py_mod, "bin", ctx.new_rustfunc(builtin_bin));
ctx.set_attr(&py_mod, "bool", ctx.bool_type());
ctx.set_attr(&py_mod, "bytearray", ctx.bytearray_type());
ctx.set_attr(&py_mod, "bytes", ctx.bytes_type());
ctx.set_attr(&py_mod, "callable", ctx.new_rustfunc(builtin_callable));
ctx.set_attr(&py_mod, "chr", ctx.new_rustfunc(builtin_chr));
ctx.set_attr(&py_mod, "classmethod", ctx.classmethod_type());
ctx.set_attr(&py_mod, "compile", ctx.new_rustfunc(builtin_compile));
ctx.set_attr(&py_mod, "complex", ctx.complex_type());
ctx.set_attr(&py_mod, "delattr", ctx.new_rustfunc(builtin_delattr));
ctx.set_attr(&py_mod, "dict", ctx.dict_type());
ctx.set_attr(&py_mod, "divmod", ctx.new_rustfunc(builtin_divmod));
ctx.set_attr(&py_mod, "dir", ctx.new_rustfunc(builtin_dir));
ctx.set_attr(&py_mod, "enumerate", ctx.enumerate_type());
ctx.set_attr(&py_mod, "eval", ctx.new_rustfunc(builtin_eval));
ctx.set_attr(&py_mod, "exec", ctx.new_rustfunc(builtin_exec));
ctx.set_attr(&py_mod, "float", ctx.float_type());
ctx.set_attr(&py_mod, "frozenset", ctx.frozenset_type());
ctx.set_attr(&py_mod, "filter", ctx.filter_type());
ctx.set_attr(&py_mod, "format", ctx.new_rustfunc(builtin_format));
ctx.set_attr(&py_mod, "getattr", ctx.new_rustfunc(builtin_getattr));
ctx.set_attr(&py_mod, "hasattr", ctx.new_rustfunc(builtin_hasattr));
ctx.set_attr(&py_mod, "hash", ctx.new_rustfunc(builtin_hash));
ctx.set_attr(&py_mod, "hex", ctx.new_rustfunc(builtin_hex));
ctx.set_attr(&py_mod, "id", ctx.new_rustfunc(builtin_id));
ctx.set_attr(&py_mod, "int", ctx.int_type());
ctx.set_attr(&py_mod, "isinstance", ctx.new_rustfunc(builtin_isinstance));
ctx.set_attr(&py_mod, "issubclass", ctx.new_rustfunc(builtin_issubclass));
ctx.set_attr(&py_mod, "iter", ctx.new_rustfunc(builtin_iter));
ctx.set_attr(&py_mod, "len", ctx.new_rustfunc(builtin_len));
ctx.set_attr(&py_mod, "list", ctx.list_type());
ctx.set_attr(&py_mod, "locals", ctx.new_rustfunc(builtin_locals));
ctx.set_attr(&py_mod, "map", ctx.map_type());
ctx.set_attr(&py_mod, "max", ctx.new_rustfunc(builtin_max));
ctx.set_attr(&py_mod, "memoryview", ctx.memoryview_type());
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
ctx.set_attr(&py_mod, "object", ctx.object());
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
ctx.set_attr(&py_mod, "ord", ctx.new_rustfunc(builtin_ord));
ctx.set_attr(&py_mod, "next", ctx.new_rustfunc(builtin_next));
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(builtin_pow));
ctx.set_attr(&py_mod, "print", ctx.new_rustfunc(builtin_print));
ctx.set_attr(&py_mod, "property", ctx.property_type());
ctx.set_attr(&py_mod, "range", ctx.range_type());
ctx.set_attr(&py_mod, "repr", ctx.new_rustfunc(builtin_repr));
ctx.set_attr(&py_mod, "reversed", ctx.new_rustfunc(builtin_reversed));
ctx.set_attr(&py_mod, "round", ctx.new_rustfunc(builtin_round));
ctx.set_attr(&py_mod, "set", ctx.set_type());
ctx.set_attr(&py_mod, "setattr", ctx.new_rustfunc(builtin_setattr));
ctx.set_attr(&py_mod, "slice", ctx.slice_type());
ctx.set_attr(&py_mod, "staticmethod", ctx.staticmethod_type());
ctx.set_attr(&py_mod, "str", ctx.str_type());
ctx.set_attr(&py_mod, "sum", ctx.new_rustfunc(builtin_sum));
ctx.set_attr(&py_mod, "super", ctx.super_type());
ctx.set_attr(&py_mod, "tuple", ctx.tuple_type());
ctx.set_attr(&py_mod, "type", ctx.type_type());
ctx.set_attr(&py_mod, "zip", ctx.zip_type());

// Constants
ctx.set_attr(&py_mod, "NotImplemented", ctx.not_implemented.clone());

// Exceptions:
ctx.set_attr(
&py_mod,
"BaseException",
ctx.exceptions.base_exception_type.clone(),
);
ctx.set_attr(&py_mod, "Exception", ctx.exceptions.exception_type.clone());
ctx.set_attr(
&py_mod,
"ArithmeticError",
ctx.exceptions.arithmetic_error.clone(),
);
ctx.set_attr(
&py_mod,
"AssertionError",
ctx.exceptions.assertion_error.clone(),
);
ctx.set_attr(
&py_mod,
"AttributeError",
ctx.exceptions.attribute_error.clone(),
);
ctx.set_attr(&py_mod, "NameError", ctx.exceptions.name_error.clone());
ctx.set_attr(
&py_mod,
"OverflowError",
ctx.exceptions.overflow_error.clone(),
);
ctx.set_attr(
&py_mod,
"RuntimeError",
ctx.exceptions.runtime_error.clone(),
);
ctx.set_attr(
&py_mod,
"NotImplementedError",
ctx.exceptions.not_implemented_error.clone(),
);
ctx.set_attr(&py_mod, "TypeError", ctx.exceptions.type_error.clone());
ctx.set_attr(&py_mod, "ValueError", ctx.exceptions.value_error.clone());
ctx.set_attr(&py_mod, "IndexError", ctx.exceptions.index_error.clone());
ctx.set_attr(&py_mod, "ImportError", ctx.exceptions.import_error.clone());
ctx.set_attr(
&py_mod,
"FileNotFoundError",
ctx.exceptions.file_not_found_error.clone(),
);
ctx.set_attr(
&py_mod,
"StopIteration",
ctx.exceptions.stop_iteration.clone(),
);
ctx.set_attr(
&py_mod,
"ZeroDivisionError",
ctx.exceptions.zero_division_error.clone(),
);
ctx.set_attr(&py_mod, "KeyError", ctx.exceptions.key_error.clone());

py_mod
py_module!(ctx, "__builtins__", {
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
"__name__" => ctx.new_str(String::from("__main__")),

"abs" => ctx.new_rustfunc(builtin_abs),
"all" => ctx.new_rustfunc(builtin_all),
"any" => ctx.new_rustfunc(builtin_any),
"bin" => ctx.new_rustfunc(builtin_bin),
"bool" => ctx.bool_type(),
"bytearray" => ctx.bytearray_type(),
"bytes" => ctx.bytes_type(),
"callable" => ctx.new_rustfunc(builtin_callable),
"chr" => ctx.new_rustfunc(builtin_chr),
"classmethod" => ctx.classmethod_type(),
"compile" => ctx.new_rustfunc(builtin_compile),
"complex" => ctx.complex_type(),
"delattr" => ctx.new_rustfunc(builtin_delattr),
"dict" => ctx.dict_type(),
"divmod" => ctx.new_rustfunc(builtin_divmod),
"dir" => ctx.new_rustfunc(builtin_dir),
"enumerate" => ctx.enumerate_type(),
"eval" => ctx.new_rustfunc(builtin_eval),
"exec" => ctx.new_rustfunc(builtin_exec),
"float" => ctx.float_type(),
"frozenset" => ctx.frozenset_type(),
"filter" => ctx.filter_type(),
"format" => ctx.new_rustfunc(builtin_format),
"getattr" => ctx.new_rustfunc(builtin_getattr),
"hasattr" => ctx.new_rustfunc(builtin_hasattr),
"hash" => ctx.new_rustfunc(builtin_hash),
"hex" => ctx.new_rustfunc(builtin_hex),
"id" => ctx.new_rustfunc(builtin_id),
"int" => ctx.int_type(),
"isinstance" => ctx.new_rustfunc(builtin_isinstance),
"issubclass" => ctx.new_rustfunc(builtin_issubclass),
"iter" => ctx.new_rustfunc(builtin_iter),
"len" => ctx.new_rustfunc(builtin_len),
"list" => ctx.list_type(),
"locals" => ctx.new_rustfunc(builtin_locals),
"map" => ctx.map_type(),
"max" => ctx.new_rustfunc(builtin_max),
"memoryview" => ctx.memoryview_type(),
"min" => ctx.new_rustfunc(builtin_min),
"object" => ctx.object(),
"oct" => ctx.new_rustfunc(builtin_oct),
"open" => ctx.new_rustfunc(io_open),
"ord" => ctx.new_rustfunc(builtin_ord),
"next" => ctx.new_rustfunc(builtin_next),
"pow" => ctx.new_rustfunc(builtin_pow),
"print" => ctx.new_rustfunc(builtin_print),
"property" => ctx.property_type(),
"range" => ctx.range_type(),
"repr" => ctx.new_rustfunc(builtin_repr),
"reversed" => ctx.new_rustfunc(builtin_reversed),
"round" => ctx.new_rustfunc(builtin_round),
"set" => ctx.set_type(),
"setattr" => ctx.new_rustfunc(builtin_setattr),
"slice" => ctx.slice_type(),
"staticmethod" => ctx.staticmethod_type(),
"str" => ctx.str_type(),
"sum" => ctx.new_rustfunc(builtin_sum),
"super" => ctx.super_type(),
"tuple" => ctx.tuple_type(),
"type" => ctx.type_type(),
"zip" => ctx.zip_type(),

// Constants
"NotImplemented" => ctx.not_implemented.clone(),

// Exceptions:
"BaseException" => ctx.exceptions.base_exception_type.clone(),
"Exception" => ctx.exceptions.exception_type.clone(),
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
"AssertionError" => ctx.exceptions.assertion_error.clone(),
"AttributeError" => ctx.exceptions.attribute_error.clone(),
"NameError" => ctx.exceptions.name_error.clone(),
"OverflowError" => ctx.exceptions.overflow_error.clone(),
"RuntimeError" => ctx.exceptions.runtime_error.clone(),
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
"TypeError" => ctx.exceptions.type_error.clone(),
"ValueError" => ctx.exceptions.value_error.clone(),
"IndexError" => ctx.exceptions.index_error.clone(),
"ImportError" => ctx.exceptions.import_error.clone(),
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
"KeyError" => ctx.exceptions.key_error.clone(),
})
}

pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ fn int_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn int_imag(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.int_type()))]);
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.int_type()))]);
let value = BigInt::from(0);
Ok(vm.ctx.new_int(value))
}
Expand Down
13 changes: 6 additions & 7 deletions vm/src/stdlib/dis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::super::obj::objcode;
use super::super::obj::objtype;
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::super::vm::VirtualMachine;
use crate::obj::objcode;
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;

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

pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module("dis", ctx.new_scope(None));
ctx.set_attr(&py_mod, "disassemble", ctx.new_rustfunc(dis_disassemble));
py_mod
py_module!(ctx, "dis", {
"disassemble" => ctx.new_rustfunc(dis_disassemble)
})
}
27 changes: 13 additions & 14 deletions vm/src/sysmodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
};
let path = ctx.new_list(path_list);

let modules = ctx.new_dict();

let sys_name = "sys";
let sys_doc = "This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

Expand Down Expand Up @@ -121,20 +118,22 @@ setprofile() -- set the global profiling function
setrecursionlimit() -- set the max recursion depth for the interpreter
settrace() -- set the global debug tracing function
";
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
let modules = ctx.new_dict();
let sys_name = "sys";
let sys_mod = py_module!(ctx, sys_name, {
"argv" => argv(ctx),
"getrefcount" => ctx.new_rustfunc(sys_getrefcount),
"getsizeof" => ctx.new_rustfunc(sys_getsizeof),
"maxsize" => ctx.new_int(std::usize::MAX),
"path" => path,
"ps1" => ctx.new_str(">>>>> ".to_string()),
"ps2" => ctx.new_str("..... ".to_string()),
"__doc__" => ctx.new_str(sys_doc.to_string()),
"_getframe" => ctx.new_rustfunc(getframe),
});

ctx.set_item(&modules, sys_name, sys_mod.clone());

ctx.set_attr(&sys_mod, "modules", modules);
ctx.set_attr(&sys_mod, "argv", argv(ctx));
ctx.set_attr(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
ctx.set_attr(&sys_mod, "getsizeof", ctx.new_rustfunc(sys_getsizeof));
ctx.set_attr(&sys_mod, "maxsize", ctx.new_int(std::usize::MAX));
ctx.set_attr(&sys_mod, "path", path);
ctx.set_attr(&sys_mod, "ps1", ctx.new_str(">>>>> ".to_string()));
ctx.set_attr(&sys_mod, "ps2", ctx.new_str("..... ".to_string()));
ctx.set_attr(&sys_mod, "__doc__", ctx.new_str(sys_doc.to_string()));
ctx.set_attr(&sys_mod, "_getframe", ctx.new_rustfunc(getframe));

sys_mod
}