Add freeze example

This commit is contained in:
Aviv Palivoda committed 2020-02-29 11:36:27 +02:00
1 parent 3d0c143425
commit 582932c8a8
5 files changed
+67 -15

No files matched your search

Generated
+7
View File
@@ -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"
+1 -1
View File
@@ -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"
+16
View File
@@ -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 }
+22
View File
@@ -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(())
}
+21 -14
View File
@@ -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<PyIntRef>,
}
#[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::<compile::Mode>()
.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::<parser::Mode>()
.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::<parser::Mode>()
.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<PyStringRef, PyCodeRef>,
@@ -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<Scope> {
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(),