diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index a5dda94c44..f78e18250b 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -464,8 +464,8 @@ impl PyContext { .unwrap() } - pub fn new_class(&self, name: &str, base: PyClassRef, flags: PyTpFlags) -> PyClassRef { - create_type_with_flags(name, &self.type_type(), &base, flags) + pub fn new_class(&self, name: &str, base: &PyClassRef, flags: PyTpFlags) -> PyClassRef { + create_type_with_flags(name, &self.type_type(), base, flags) } pub fn new_namespace(&self) -> PyObjectRef { @@ -1481,10 +1481,10 @@ pub trait PyClassImpl: PyClassDef { } fn make_class(ctx: &PyContext) -> PyClassRef { - Self::make_class_with_base(ctx, Self::NAME, ctx.object()) + Self::make_class_with_base(ctx, Self::NAME, &ctx.object()) } - fn make_class_with_base(ctx: &PyContext, name: &str, base: PyClassRef) -> PyClassRef { + fn make_class_with_base(ctx: &PyContext, name: &str, base: &PyClassRef) -> PyClassRef { let py_class = ctx.new_class(name, base, Self::TP_FLAGS); Self::extend_class(ctx, &py_class); py_class diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 81ba3a05fa..05c0325e87 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -644,72 +644,72 @@ pub(crate) fn parse(vm: &VirtualMachine, source: &str, mode: Mode) -> PyResult { pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; - let ast_base = py_class!(ctx, "AST", ctx.object(), PyTpFlags::HAS_DICT, {}); + let ast_base = py_class!(ctx, "AST", &ctx.object(), PyTpFlags::HAS_DICT, {}); py_module!(vm, MODULE_NAME, { // TODO: There's got to be a better way! - "alias" => py_class!(ctx, "alias", ast_base.clone(), {}), - "arg" => py_class!(ctx, "arg", ast_base.clone(), {}), - "arguments" => py_class!(ctx, "arguments", ast_base.clone(), {}), - "AnnAssign" => py_class!(ctx, "AnnAssign", ast_base.clone(), {}), - "Assign" => py_class!(ctx, "Assign", ast_base.clone(), {}), - "AugAssign" => py_class!(ctx, "AugAssign", ast_base.clone(), {}), - "AsyncFor" => py_class!(ctx, "AsyncFor", ast_base.clone(), {}), - "AsyncFunctionDef" => py_class!(ctx, "AsyncFunctionDef", ast_base.clone(), {}), - "AsyncWith" => py_class!(ctx, "AsyncWith", ast_base.clone(), {}), - "Assert" => py_class!(ctx, "Assert", ast_base.clone(), {}), - "Attribute" => py_class!(ctx, "Attribute", ast_base.clone(), {}), - "Await" => py_class!(ctx, "Await", ast_base.clone(), {}), - "BinOp" => py_class!(ctx, "BinOp", ast_base.clone(), {}), - "BoolOp" => py_class!(ctx, "BoolOp", ast_base.clone(), {}), - "Break" => py_class!(ctx, "Break", ast_base.clone(), {}), - "Bytes" => py_class!(ctx, "Bytes", ast_base.clone(), {}), - "Call" => py_class!(ctx, "Call", ast_base.clone(), {}), - "ClassDef" => py_class!(ctx, "ClassDef", ast_base.clone(), {}), - "Compare" => py_class!(ctx, "Compare", ast_base.clone(), {}), - "comprehension" => py_class!(ctx, "comprehension", ast_base.clone(), {}), - "Continue" => py_class!(ctx, "Continue", ast_base.clone(), {}), - "Delete" => py_class!(ctx, "Delete", ast_base.clone(), {}), - "Dict" => py_class!(ctx, "Dict", ast_base.clone(), {}), - "DictComp" => py_class!(ctx, "DictComp", ast_base.clone(), {}), - "Ellipsis" => py_class!(ctx, "Ellipsis", ast_base.clone(), {}), - "Expr" => py_class!(ctx, "Expr", ast_base.clone(), {}), - "ExceptHandler" => py_class!(ctx, "ExceptHandler", ast_base.clone(), {}), - "For" => py_class!(ctx, "For", ast_base.clone(), {}), - "FormattedValue" => py_class!(ctx, "FormattedValue", ast_base.clone(), {}), - "FunctionDef" => py_class!(ctx, "FunctionDef", ast_base.clone(), {}), - "GeneratorExp" => py_class!(ctx, "GeneratorExp", ast_base.clone(), {}), - "Global" => py_class!(ctx, "Global", ast_base.clone(), {}), - "If" => py_class!(ctx, "If", ast_base.clone(), {}), - "IfExp" => py_class!(ctx, "IfExp", ast_base.clone(), {}), - "Import" => py_class!(ctx, "Import", ast_base.clone(), {}), - "ImportFrom" => py_class!(ctx, "ImportFrom", ast_base.clone(), {}), - "JoinedStr" => py_class!(ctx, "JoinedStr", ast_base.clone(), {}), - "keyword" => py_class!(ctx, "keyword", ast_base.clone(), {}), - "Lambda" => py_class!(ctx, "Lambda", ast_base.clone(), {}), - "List" => py_class!(ctx, "List", ast_base.clone(), {}), - "ListComp" => py_class!(ctx, "ListComp", ast_base.clone(), {}), - "Module" => py_class!(ctx, "Module", ast_base.clone(), {}), - "Name" => py_class!(ctx, "Name", ast_base.clone(), {}), - "NameConstant" => py_class!(ctx, "NameConstant", ast_base.clone(), {}), - "Nonlocal" => py_class!(ctx, "Nonlocal", ast_base.clone(), {}), - "Num" => py_class!(ctx, "Num", ast_base.clone(), {}), - "Pass" => py_class!(ctx, "Pass", ast_base.clone(), {}), - "Raise" => py_class!(ctx, "Raise", ast_base.clone(), {}), - "Return" => py_class!(ctx, "Return", ast_base.clone(), {}), - "Set" => py_class!(ctx, "Set", ast_base.clone(), {}), - "SetComp" => py_class!(ctx, "SetComp", ast_base.clone(), {}), - "Starred" => py_class!(ctx, "Starred", ast_base.clone(), {}), - "Starred" => py_class!(ctx, "Starred", ast_base.clone(), {}), - "Str" => py_class!(ctx, "Str", ast_base.clone(), {}), - "Subscript" => py_class!(ctx, "Subscript", ast_base.clone(), {}), - "Try" => py_class!(ctx, "Try", ast_base.clone(), {}), - "Tuple" => py_class!(ctx, "Tuple", ast_base.clone(), {}), - "UnaryOp" => py_class!(ctx, "UnaryOp", ast_base.clone(), {}), - "While" => py_class!(ctx, "While", ast_base.clone(), {}), - "With" => py_class!(ctx, "With", ast_base.clone(), {}), - "withitem" => py_class!(ctx, "withitem", ast_base.clone(), {}), - "Yield" => py_class!(ctx, "Yield", ast_base.clone(), {}), - "YieldFrom" => py_class!(ctx, "YieldFrom", ast_base.clone(), {}), + "alias" => py_class!(ctx, "alias", &ast_base, {}), + "arg" => py_class!(ctx, "arg", &ast_base, {}), + "arguments" => py_class!(ctx, "arguments", &ast_base, {}), + "AnnAssign" => py_class!(ctx, "AnnAssign", &ast_base, {}), + "Assign" => py_class!(ctx, "Assign", &ast_base, {}), + "AugAssign" => py_class!(ctx, "AugAssign", &ast_base, {}), + "AsyncFor" => py_class!(ctx, "AsyncFor", &ast_base, {}), + "AsyncFunctionDef" => py_class!(ctx, "AsyncFunctionDef", &ast_base, {}), + "AsyncWith" => py_class!(ctx, "AsyncWith", &ast_base, {}), + "Assert" => py_class!(ctx, "Assert", &ast_base, {}), + "Attribute" => py_class!(ctx, "Attribute", &ast_base, {}), + "Await" => py_class!(ctx, "Await", &ast_base, {}), + "BinOp" => py_class!(ctx, "BinOp", &ast_base, {}), + "BoolOp" => py_class!(ctx, "BoolOp", &ast_base, {}), + "Break" => py_class!(ctx, "Break", &ast_base, {}), + "Bytes" => py_class!(ctx, "Bytes", &ast_base, {}), + "Call" => py_class!(ctx, "Call", &ast_base, {}), + "ClassDef" => py_class!(ctx, "ClassDef", &ast_base, {}), + "Compare" => py_class!(ctx, "Compare", &ast_base, {}), + "comprehension" => py_class!(ctx, "comprehension", &ast_base, {}), + "Continue" => py_class!(ctx, "Continue", &ast_base, {}), + "Delete" => py_class!(ctx, "Delete", &ast_base, {}), + "Dict" => py_class!(ctx, "Dict", &ast_base, {}), + "DictComp" => py_class!(ctx, "DictComp", &ast_base, {}), + "Ellipsis" => py_class!(ctx, "Ellipsis", &ast_base, {}), + "Expr" => py_class!(ctx, "Expr", &ast_base, {}), + "ExceptHandler" => py_class!(ctx, "ExceptHandler", &ast_base, {}), + "For" => py_class!(ctx, "For", &ast_base, {}), + "FormattedValue" => py_class!(ctx, "FormattedValue", &ast_base, {}), + "FunctionDef" => py_class!(ctx, "FunctionDef", &ast_base, {}), + "GeneratorExp" => py_class!(ctx, "GeneratorExp", &ast_base, {}), + "Global" => py_class!(ctx, "Global", &ast_base, {}), + "If" => py_class!(ctx, "If", &ast_base, {}), + "IfExp" => py_class!(ctx, "IfExp", &ast_base, {}), + "Import" => py_class!(ctx, "Import", &ast_base, {}), + "ImportFrom" => py_class!(ctx, "ImportFrom", &ast_base, {}), + "JoinedStr" => py_class!(ctx, "JoinedStr", &ast_base, {}), + "keyword" => py_class!(ctx, "keyword", &ast_base, {}), + "Lambda" => py_class!(ctx, "Lambda", &ast_base, {}), + "List" => py_class!(ctx, "List", &ast_base, {}), + "ListComp" => py_class!(ctx, "ListComp", &ast_base, {}), + "Module" => py_class!(ctx, "Module", &ast_base, {}), + "Name" => py_class!(ctx, "Name", &ast_base, {}), + "NameConstant" => py_class!(ctx, "NameConstant", &ast_base, {}), + "Nonlocal" => py_class!(ctx, "Nonlocal", &ast_base, {}), + "Num" => py_class!(ctx, "Num", &ast_base, {}), + "Pass" => py_class!(ctx, "Pass", &ast_base, {}), + "Raise" => py_class!(ctx, "Raise", &ast_base, {}), + "Return" => py_class!(ctx, "Return", &ast_base, {}), + "Set" => py_class!(ctx, "Set", &ast_base, {}), + "SetComp" => py_class!(ctx, "SetComp", &ast_base, {}), + "Starred" => py_class!(ctx, "Starred", &ast_base, {}), + "Starred" => py_class!(ctx, "Starred", &ast_base, {}), + "Str" => py_class!(ctx, "Str", &ast_base, {}), + "Subscript" => py_class!(ctx, "Subscript", &ast_base, {}), + "Try" => py_class!(ctx, "Try", &ast_base, {}), + "Tuple" => py_class!(ctx, "Tuple", &ast_base, {}), + "UnaryOp" => py_class!(ctx, "UnaryOp", &ast_base, {}), + "While" => py_class!(ctx, "While", &ast_base, {}), + "With" => py_class!(ctx, "With", &ast_base, {}), + "withitem" => py_class!(ctx, "withitem", &ast_base, {}), + "Yield" => py_class!(ctx, "Yield", &ast_base, {}), + "YieldFrom" => py_class!(ctx, "YieldFrom", &ast_base, {}), "AST" => ast_base, "PyCF_ONLY_AST" => ctx.new_int(PY_COMPILE_FLAG_AST_ONLY), }) diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 5001859db9..078dc80284 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -877,7 +877,7 @@ mod fileio { } pub fn make_fileio(ctx: &crate::pyobject::PyContext, raw_io_base: PyClassRef) -> PyClassRef { - FileIO::make_class_with_base(ctx, FileIO::NAME, raw_io_base) + FileIO::make_class_with_base(ctx, FileIO::NAME, &raw_io_base) } } @@ -1268,7 +1268,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; // IOBase the abstract base class of the IO Module - let io_base = py_class!(ctx, "_IOBase", ctx.object(), { + let io_base = py_class!(ctx, "_IOBase", &ctx.object(), { "__enter__" => ctx.new_method(io_base_cm_enter), "__exit__" => ctx.new_method(io_base_cm_exit), "seekable" => ctx.new_method(io_base_seekable), @@ -1289,17 +1289,17 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { }); // IOBase Subclasses - let raw_io_base = py_class!(ctx, "_RawIOBase", io_base.clone(), { + let raw_io_base = py_class!(ctx, "_RawIOBase", &io_base, { "read" => ctx.new_method(raw_io_base_read), }); - let buffered_io_base = py_class!(ctx, "_BufferedIOBase", io_base.clone(), {}); + let buffered_io_base = py_class!(ctx, "_BufferedIOBase", &io_base, {}); //TextIO Base has no public constructor - let text_io_base = py_class!(ctx, "_TextIOBase", io_base.clone(), {}); + let text_io_base = py_class!(ctx, "_TextIOBase", &io_base, {}); // BufferedIOBase Subclasses - let buffered_reader = py_class!(ctx, "BufferedReader", buffered_io_base.clone(), { + let buffered_reader = py_class!(ctx, "BufferedReader", &buffered_io_base, { //workaround till the buffered classes can be fixed up to be more //consistent with the python model //For more info see: https://github.com/RustPython/RustPython/issues/547 @@ -1314,7 +1314,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "mode" => ctx.new_readonly_getset("mode", buffered_io_base_mode), }); - let buffered_writer = py_class!(ctx, "BufferedWriter", buffered_io_base.clone(), { + let buffered_writer = py_class!(ctx, "BufferedWriter", &buffered_io_base, { //workaround till the buffered classes can be fixed up to be more //consistent with the python model //For more info see: https://github.com/RustPython/RustPython/issues/547 @@ -1330,7 +1330,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { }); //TextIOBase Subclass - let text_io_wrapper = py_class!(ctx, "TextIOWrapper", text_io_base.clone(), { + let text_io_wrapper = py_class!(ctx, "TextIOWrapper", &text_io_base, { "__init__" => ctx.new_method(text_io_wrapper_init), "seekable" => ctx.new_method(text_io_wrapper_seekable), "seek" => ctx.new_method(text_io_wrapper_seek), @@ -1344,7 +1344,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { }); //StringIO: in-memory text - let string_io = py_class!(ctx, "StringIO", text_io_base.clone(), { + let string_io = py_class!(ctx, "StringIO", &text_io_base, { "__module__" => ctx.new_str("_io"), (slot new) => string_io_new, "seek" => ctx.new_method(PyStringIORef::seek), @@ -1360,7 +1360,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { }); //BytesIO: in-memory bytes - let bytes_io = py_class!(ctx, "BytesIO", buffered_io_base.clone(), { + let bytes_io = py_class!(ctx, "BytesIO", &buffered_io_base, { (slot new) => bytes_io_new, "read" => ctx.new_method(PyBytesIORef::read), "read1" => ctx.new_method(PyBytesIORef::read), diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index 5dcf4d27ff..64629ac907 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -972,7 +972,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { let struct_error = ctx.new_class( "struct.error", - ctx.exceptions.exception_type.clone(), + &ctx.exceptions.exception_type, Default::default(), ); diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 2e44a8a96a..c8d2b381dc 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -654,12 +654,12 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; let socket_timeout = ctx.new_class( "socket.timeout", - vm.ctx.exceptions.os_error.clone(), + &vm.ctx.exceptions.os_error, Default::default(), ); let socket_gaierror = ctx.new_class( "socket.gaierror", - vm.ctx.exceptions.os_error.clone(), + &vm.ctx.exceptions.os_error, Default::default(), ); diff --git a/wasm/lib/src/wasm_builtins.rs b/wasm/lib/src/wasm_builtins.rs index 2366ff9036..6f6f9ec04d 100644 --- a/wasm/lib/src/wasm_builtins.rs +++ b/wasm/lib/src/wasm_builtins.rs @@ -32,7 +32,7 @@ pub fn make_stdout_object( let flush_method = ctx.new_method(|_self: PyObjectRef| {}); // there's not really any point to storing this class so that there's a consistent type object, // we just want a half-decent repr() output - let cls = py_class!(ctx, "JSStdout", vm.ctx.object(), { + let cls = py_class!(ctx, "JSStdout", &vm.ctx.object(), { "write" => write_method, "flush" => flush_method, });