mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #266 from coolreader18/format-mk-module
Format module building in a more readable way
This commit is contained in:
8 files changed
+98
-42
No files matched your search
@@ -610,20 +610,23 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
}
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
// TODO: maybe we can use some clever macro to generate this?
|
||||
let ast_mod = ctx.new_module("ast", ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&ast_mod, "parse", ctx.new_rustfunc(ast_parse));
|
||||
|
||||
ctx.set_attr(
|
||||
&ast_mod,
|
||||
"Module",
|
||||
ctx.new_class("_ast.Module", ctx.object()),
|
||||
);
|
||||
|
||||
// TODO: maybe we can use some clever macro to generate this?
|
||||
ctx.set_attr(
|
||||
&ast_mod,
|
||||
"FunctionDef",
|
||||
ctx.new_class("_ast.FunctionDef", ctx.object()),
|
||||
);
|
||||
ctx.set_attr(&ast_mod, "Call", ctx.new_class("_ast.Call", ctx.object()));
|
||||
|
||||
ast_mod
|
||||
}
|
||||
+64
-38
@@ -359,7 +359,9 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
|
||||
|
||||
//IOBase the abstract base class of the IO Module
|
||||
let io_base = ctx.new_class("IOBase", ctx.object());
|
||||
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
|
||||
@@ -368,64 +370,88 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let raw_io_base = ctx.new_class("RawIOBase", ctx.object());
|
||||
ctx.set_attr(&py_mod, "RawIOBase", raw_io_base.clone());
|
||||
|
||||
let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_io_base,
|
||||
"__init__",
|
||||
ctx.new_rustfunc(buffered_io_base_init),
|
||||
);
|
||||
let buffered_io_base = {
|
||||
let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_io_base,
|
||||
"__init__",
|
||||
ctx.new_rustfunc(buffered_io_base_init),
|
||||
);
|
||||
buffered_io_base
|
||||
};
|
||||
ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
|
||||
|
||||
//TextIO Base has no public constructor
|
||||
let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
|
||||
ctx.set_attr(&text_io_base, "read", ctx.new_rustfunc(text_io_base_read));
|
||||
let text_io_base = {
|
||||
let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
|
||||
ctx.set_attr(&text_io_base, "read", ctx.new_rustfunc(text_io_base_read));
|
||||
text_io_base
|
||||
};
|
||||
ctx.set_attr(&py_mod, "TextIOBase", text_io_base.clone());
|
||||
|
||||
// RawBaseIO Subclasses
|
||||
let file_io = ctx.new_class("FileIO", raw_io_base.clone());
|
||||
ctx.set_attr(&file_io, "__init__", ctx.new_rustfunc(file_io_init));
|
||||
ctx.set_attr(&file_io, "name", ctx.str_type());
|
||||
ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
|
||||
ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_readinto));
|
||||
ctx.set_attr(&file_io, "write", ctx.new_rustfunc(file_io_write));
|
||||
let file_io = {
|
||||
let file_io = ctx.new_class("FileIO", raw_io_base.clone());
|
||||
ctx.set_attr(&file_io, "__init__", ctx.new_rustfunc(file_io_init));
|
||||
ctx.set_attr(&file_io, "name", ctx.str_type());
|
||||
ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
|
||||
ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_readinto));
|
||||
ctx.set_attr(&file_io, "write", ctx.new_rustfunc(file_io_write));
|
||||
file_io
|
||||
};
|
||||
ctx.set_attr(&py_mod, "FileIO", file_io.clone());
|
||||
|
||||
// BufferedIOBase Subclasses
|
||||
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_reader,
|
||||
"read",
|
||||
ctx.new_rustfunc(buffered_reader_read),
|
||||
);
|
||||
let buffered_reader = {
|
||||
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_reader,
|
||||
"read",
|
||||
ctx.new_rustfunc(buffered_reader_read),
|
||||
);
|
||||
buffered_reader
|
||||
};
|
||||
ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
|
||||
|
||||
let buffered_writer = ctx.new_class("BufferedWriter", buffered_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_writer,
|
||||
"write",
|
||||
ctx.new_rustfunc(buffered_writer_write),
|
||||
);
|
||||
let buffered_writer = {
|
||||
let buffered_writer = ctx.new_class("BufferedWriter", buffered_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&buffered_writer,
|
||||
"write",
|
||||
ctx.new_rustfunc(buffered_writer_write),
|
||||
);
|
||||
buffered_writer
|
||||
};
|
||||
ctx.set_attr(&py_mod, "BufferedWriter", buffered_writer.clone());
|
||||
|
||||
//TextIOBase Subclass
|
||||
let text_io_wrapper = ctx.new_class("TextIOWrapper", text_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&text_io_wrapper,
|
||||
"__init__",
|
||||
ctx.new_rustfunc(text_io_wrapper_init),
|
||||
);
|
||||
let text_io_wrapper = {
|
||||
let text_io_wrapper = ctx.new_class("TextIOWrapper", text_io_base.clone());
|
||||
ctx.set_attr(
|
||||
&text_io_wrapper,
|
||||
"__init__",
|
||||
ctx.new_rustfunc(text_io_wrapper_init),
|
||||
);
|
||||
text_io_wrapper
|
||||
};
|
||||
ctx.set_attr(&py_mod, "TextIOWrapper", text_io_wrapper.clone());
|
||||
|
||||
// BytesIO: in-memory bytes
|
||||
let string_io = ctx.new_class("StringIO", io_base.clone());
|
||||
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
|
||||
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
|
||||
let string_io = {
|
||||
let string_io = ctx.new_class("StringIO", io_base.clone());
|
||||
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
|
||||
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
|
||||
string_io
|
||||
};
|
||||
ctx.set_attr(&py_mod, "StringIO", string_io);
|
||||
|
||||
// StringIO: in-memory text
|
||||
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
|
||||
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
|
||||
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
|
||||
let bytes_io = {
|
||||
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
|
||||
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
|
||||
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
|
||||
bytes_io
|
||||
};
|
||||
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
|
||||
|
||||
py_mod
|
||||
|
||||
@@ -233,8 +233,10 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let json_mod = ctx.new_module("json", ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(dumps));
|
||||
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(loads));
|
||||
|
||||
// TODO: Make this a proper type with a constructor
|
||||
let json_decode_error = create_type(
|
||||
"JSONDecodeError",
|
||||
@@ -243,5 +245,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
&ctx.dict_type,
|
||||
);
|
||||
ctx.set_attr(&json_mod, "JSONDecodeError", json_decode_error);
|
||||
|
||||
json_mod
|
||||
}
|
||||
@@ -19,7 +19,9 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
|
||||
|
||||
let keyword_kwlist = ctx.new_list(
|
||||
lexer::get_keywords()
|
||||
.keys()
|
||||
@@ -27,5 +29,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
.collect(),
|
||||
);
|
||||
ctx.set_attr(&py_mod, "kwlist", keyword_kwlist);
|
||||
|
||||
py_mod
|
||||
}
|
||||
@@ -342,7 +342,9 @@ fn struct_unpack(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let py_mod = ctx.new_module(&"struct".to_string(), ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&py_mod, "pack", ctx.new_rustfunc(struct_pack));
|
||||
ctx.set_attr(&py_mod, "unpack", ctx.new_rustfunc(struct_unpack));
|
||||
|
||||
py_mod
|
||||
}
|
||||
@@ -51,9 +51,12 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let py_mod = ctx.new_module("re", ctx.new_scope(None));
|
||||
|
||||
let match_type = ctx.new_class("Match", ctx.object());
|
||||
ctx.set_attr(&py_mod, "Match", match_type);
|
||||
|
||||
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
|
||||
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));
|
||||
|
||||
py_mod
|
||||
}
|
||||
@@ -32,7 +32,9 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let py_mod = ctx.new_module("time", ctx.new_scope(None));
|
||||
|
||||
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
|
||||
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
|
||||
|
||||
py_mod
|
||||
}
|
||||
+17
-3
@@ -39,24 +39,38 @@ fn sys_getsizeof(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
|
||||
let path_list = match env::var_os("PYTHONPATH") {
|
||||
Some(paths) => env::split_paths(&paths)
|
||||
.map(|path| ctx.new_str(path.to_str().unwrap().to_string()))
|
||||
.map(|path| {
|
||||
ctx.new_str(
|
||||
path.to_str()
|
||||
.expect("PYTHONPATH isn't valid unicode")
|
||||
.to_string(),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let path = ctx.new_list(path_list);
|
||||
|
||||
let modules = ctx.new_dict();
|
||||
|
||||
let sys_name = "sys";
|
||||
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
|
||||
|
||||
ctx.set_item(&modules, sys_name, sys_mod.clone());
|
||||
|
||||
ctx.set_item(&sys_mod, "modules", modules);
|
||||
ctx.set_item(&sys_mod, "argv", argv(ctx));
|
||||
ctx.set_item(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
|
||||
ctx.set_item(&sys_mod, "getsizeof", ctx.new_rustfunc(sys_getsizeof));
|
||||
let maxsize = ctx.new_int(std::usize::MAX.to_bigint().unwrap());
|
||||
ctx.set_item(&sys_mod, "maxsize", maxsize);
|
||||
ctx.set_item(
|
||||
&sys_mod,
|
||||
"maxsize",
|
||||
ctx.new_int(std::usize::MAX.to_bigint().unwrap()),
|
||||
);
|
||||
ctx.set_item(&sys_mod, "path", path);
|
||||
ctx.set_item(&sys_mod, "ps1", ctx.new_str(">>>>> ".to_string()));
|
||||
ctx.set_item(&sys_mod, "ps2", ctx.new_str("..... ".to_string()));
|
||||
ctx.set_item(&sys_mod, "_getframe", ctx.new_rustfunc(getframe));
|
||||
|
||||
sys_mod
|
||||
}
|
||||
Reference in new issue
Block a user