From 582932c8a885f7702e8530f69b29dfbb9b6b7f9b Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 29 Feb 2020 11:36:27 +0200 Subject: [PATCH 1/3] Add freeze example --- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- freeze/Cargo.toml | 16 ++++++++++++++++ freeze/src/main.rs | 22 ++++++++++++++++++++++ vm/src/builtins.rs | 35 +++++++++++++++++++++-------------- 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 freeze/Cargo.toml create mode 100644 freeze/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index cc9f4166f9..1eb4d15695 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1557,6 +1557,13 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustpython_freeze" +version = "0.1.0-pre-alpha.2" +dependencies = [ + "rustpython-vm", +] + [[package]] name = "rustpython_wasm" version = "0.1.0-pre-alpha.2" diff --git a/Cargo.toml b/Cargo.toml index d26f57e996..612a891a94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/RustPython/RustPython" license = "MIT" [workspace] -members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode"] +members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "freeze"] [[bench]] name = "bench" diff --git a/freeze/Cargo.toml b/freeze/Cargo.toml new file mode 100644 index 0000000000..cb914c3cc1 --- /dev/null +++ b/freeze/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rustpython_freeze" +version = "0.1.0-pre-alpha.2" +authors = ["RustPython Team"] +license = "MIT" +description = "A Python-3 (CPython >= 3.5.0) Interpreter written in Rust, compiled to frozen executable" +repository = "https://github.com/RustPython/RustPython/tree/master/freeze" +edition = "2018" + +[[bin]] +name = "rustpython_freeze" +path = "src/main.rs" + + +[dependencies] +rustpython-vm = { path = "../vm", default-features = false } diff --git a/freeze/src/main.rs b/freeze/src/main.rs new file mode 100644 index 0000000000..1699f2d3cf --- /dev/null +++ b/freeze/src/main.rs @@ -0,0 +1,22 @@ +use std::collections::HashMap; + +use rustpython_vm as vm; + +fn main() -> vm::pyobject::PyResult<()> { + let vm = vm::VirtualMachine::new(vm::PySettings::default()); + + let scope = vm.new_scope_with_builtins(); + + let modules: HashMap<&str, vm::bytecode::FrozenModule> = vm::py_compile_bytecode!( + source = "print(\"Hello world1!\")\n", + module_name = "__main__" + ); + + vm.run_code_obj( + vm.ctx + .new_code_object(modules.get("__main__").unwrap().code.clone()), + scope, + )?; + + Ok(()) +} diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index 84fd82af64..ee8099c107 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -11,6 +11,8 @@ use num_bigint::Sign; use num_traits::{Signed, ToPrimitive, Zero}; #[cfg(feature = "rustpython-compiler")] use rustpython_compiler::compile; +#[cfg(feature = "rustpython-parser")] +use rustpython_parser::parser; use crate::exceptions::PyBaseExceptionRef; use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs}; @@ -31,6 +33,7 @@ use crate::pyobject::{ TypeProtocol, }; use crate::scope::Scope; +#[cfg(feature = "rustpython-parser")] use crate::stdlib::ast; #[cfg(not(target_arch = "wasm32"))] use crate::stdlib::io::io_open; @@ -126,6 +129,7 @@ struct CompileArgs { optimize: OptionalArg, } +#[cfg(feature = "rustpython-compiler")] fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult { // TODO: compile::compile should probably get bytes let source = match &args.source { @@ -139,9 +143,9 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult { .flags .map_or(Ok(0), |v| i32::try_from_object(vm, v.into_object()))?; - if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() { - #[cfg(feature = "rustpython-compiler")] - { + #[cfg(feature = "rustpython-parser")] + { + if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() { let mode = mode_str .parse::() .map_err(|err| vm.new_value_error(err.to_string()))?; @@ -149,17 +153,18 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult { vm.compile(&source, mode, args.filename.as_str().to_owned()) .map(|o| o.into_object()) .map_err(|err| vm.new_syntax_error(&err)) + } else { + let mode = mode_str + .parse::() + .map_err(|err| vm.new_value_error(err.to_string()))?; + ast::parse(&vm, &source, mode) } - #[cfg(not(feature = "rustpython-compiler"))] - { - Err(vm.new_value_error("PyCF_ONLY_AST flag is required without compiler support")) - } - } else { - use rustpython_parser::parser; - let mode = mode_str - .parse::() - .map_err(|err| vm.new_value_error(err.to_string()))?; - ast::parse(&vm, &source, mode) + } + #[cfg(not(feature = "rustpython-parser"))] + { + Err(vm.new_value_error( + "PyCF_ONLY_AST flag is not supported without parser support".to_string(), + )) } } @@ -218,6 +223,7 @@ fn builtin_exec( run_code(vm, source, scope, compile::Mode::Exec) } +#[cfg(feature = "rustpython-compiler")] fn run_code( vm: &VirtualMachine, source: Either, @@ -238,6 +244,7 @@ fn run_code( vm.run_code_obj(code_obj, scope) } +#[cfg(feature = "rustpython-compiler")] fn make_scope(vm: &VirtualMachine, scope: ScopeArgs) -> PyResult { let globals = scope.globals; let current_scope = vm.current_scope(); @@ -767,6 +774,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { extend_module!(vm, module, { "eval" => ctx.new_function(builtin_eval), "exec" => ctx.new_function(builtin_exec), + "compile" => ctx.new_function(builtin_compile), }); } @@ -787,7 +795,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { "callable" => ctx.new_function(builtin_callable), "chr" => ctx.new_function(builtin_chr), "classmethod" => ctx.classmethod_type(), - "compile" => ctx.new_function(builtin_compile), "complex" => ctx.complex_type(), "delattr" => ctx.new_function(builtin_delattr), "dict" => ctx.dict_type(), From e95a871d2eadbecc9f4a05d663ad3b4e85ec3723 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 29 Feb 2020 12:48:13 +0200 Subject: [PATCH 2/3] Freeze from python script --- freeze/freeze.py | 4 ++++ freeze/src/main.rs | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 freeze/freeze.py diff --git a/freeze/freeze.py b/freeze/freeze.py new file mode 100644 index 0000000000..c600ebf959 --- /dev/null +++ b/freeze/freeze.py @@ -0,0 +1,4 @@ +import time + +print("Hello world!!!", time.time()) + diff --git a/freeze/src/main.rs b/freeze/src/main.rs index 1699f2d3cf..0bd9f8f5f0 100644 --- a/freeze/src/main.rs +++ b/freeze/src/main.rs @@ -7,16 +7,18 @@ fn main() -> vm::pyobject::PyResult<()> { let scope = vm.new_scope_with_builtins(); - let modules: HashMap<&str, vm::bytecode::FrozenModule> = vm::py_compile_bytecode!( - source = "print(\"Hello world1!\")\n", - module_name = "__main__" + let modules: HashMap = + vm::py_compile_bytecode!(file = "freeze.py"); + + let res = vm.run_code_obj( + vm.ctx + .new_code_object(modules.get("frozen").unwrap().code.clone()), + scope, ); - vm.run_code_obj( - vm.ctx - .new_code_object(modules.get("__main__").unwrap().code.clone()), - scope, - )?; + if let Err(err) = res { + vm::exceptions::print_exception(&vm, &err) + } Ok(()) } From 81093bb6f4bc3794af3cd853390920fba5a93c98 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 6 Mar 2020 10:05:47 +0200 Subject: [PATCH 3/3] Move freeze example to examples folder --- Cargo.toml | 2 +- {freeze => examples/freeze}/Cargo.toml | 2 +- {freeze => examples/freeze}/freeze.py | 0 {freeze => examples/freeze}/src/main.rs | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename {freeze => examples/freeze}/Cargo.toml (85%) rename {freeze => examples/freeze}/freeze.py (100%) rename {freeze => examples/freeze}/src/main.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 612a891a94..7429f8ba06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/RustPython/RustPython" license = "MIT" [workspace] -members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "freeze"] +members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "examples/freeze"] [[bench]] name = "bench" diff --git a/freeze/Cargo.toml b/examples/freeze/Cargo.toml similarity index 85% rename from freeze/Cargo.toml rename to examples/freeze/Cargo.toml index cb914c3cc1..514569895a 100644 --- a/freeze/Cargo.toml +++ b/examples/freeze/Cargo.toml @@ -13,4 +13,4 @@ path = "src/main.rs" [dependencies] -rustpython-vm = { path = "../vm", default-features = false } +rustpython-vm = { path = "../../vm", default-features = false } diff --git a/freeze/freeze.py b/examples/freeze/freeze.py similarity index 100% rename from freeze/freeze.py rename to examples/freeze/freeze.py diff --git a/freeze/src/main.rs b/examples/freeze/src/main.rs similarity index 100% rename from freeze/src/main.rs rename to examples/freeze/src/main.rs