Skip to content

Commit 8bde882

Browse files
authored
Merge pull request RustPython#1784 from palaviv/freeze
Add frozen executable example
2 parents 9802324 + 81093bb commit 8bde882

File tree

6 files changed

+73
-15
lines changed

6 files changed

+73
-15
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repository = "https://github.com/RustPython/RustPython"
88
license = "MIT"
99

1010
[workspace]
11-
members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode"]
11+
members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "examples/freeze"]
1212

1313
[[bench]]
1414
name = "bench"

examples/freeze/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "rustpython_freeze"
3+
version = "0.1.0-pre-alpha.2"
4+
authors = ["RustPython Team"]
5+
license = "MIT"
6+
description = "A Python-3 (CPython >= 3.5.0) Interpreter written in Rust, compiled to frozen executable"
7+
repository = "https://github.com/RustPython/RustPython/tree/master/freeze"
8+
edition = "2018"
9+
10+
[[bin]]
11+
name = "rustpython_freeze"
12+
path = "src/main.rs"
13+
14+
15+
[dependencies]
16+
rustpython-vm = { path = "../../vm", default-features = false }

examples/freeze/freeze.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import time
2+
3+
print("Hello world!!!", time.time())
4+

examples/freeze/src/main.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::HashMap;
2+
3+
use rustpython_vm as vm;
4+
5+
fn main() -> vm::pyobject::PyResult<()> {
6+
let vm = vm::VirtualMachine::new(vm::PySettings::default());
7+
8+
let scope = vm.new_scope_with_builtins();
9+
10+
let modules: HashMap<String, vm::bytecode::FrozenModule> =
11+
vm::py_compile_bytecode!(file = "freeze.py");
12+
13+
let res = vm.run_code_obj(
14+
vm.ctx
15+
.new_code_object(modules.get("frozen").unwrap().code.clone()),
16+
scope,
17+
);
18+
19+
if let Err(err) = res {
20+
vm::exceptions::print_exception(&vm, &err)
21+
}
22+
23+
Ok(())
24+
}

vm/src/builtins.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use num_bigint::Sign;
1111
use num_traits::{Signed, ToPrimitive, Zero};
1212
#[cfg(feature = "rustpython-compiler")]
1313
use rustpython_compiler::compile;
14+
#[cfg(feature = "rustpython-parser")]
15+
use rustpython_parser::parser;
1416

1517
use crate::exceptions::PyBaseExceptionRef;
1618
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
@@ -32,6 +34,7 @@ use crate::pyobject::{
3234
};
3335
use crate::readline::{Readline, ReadlineResult};
3436
use crate::scope::Scope;
37+
#[cfg(feature = "rustpython-parser")]
3538
use crate::stdlib::ast;
3639
use crate::vm::VirtualMachine;
3740

@@ -125,6 +128,7 @@ struct CompileArgs {
125128
optimize: OptionalArg<PyIntRef>,
126129
}
127130

131+
#[cfg(feature = "rustpython-compiler")]
128132
fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult {
129133
// TODO: compile::compile should probably get bytes
130134
let source = match &args.source {
@@ -138,27 +142,28 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult {
138142
.flags
139143
.map_or(Ok(0), |v| i32::try_from_object(vm, v.into_object()))?;
140144

141-
if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() {
142-
#[cfg(feature = "rustpython-compiler")]
143-
{
145+
#[cfg(feature = "rustpython-parser")]
146+
{
147+
if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() {
144148
let mode = mode_str
145149
.parse::<compile::Mode>()
146150
.map_err(|err| vm.new_value_error(err.to_string()))?;
147151

148152
vm.compile(&source, mode, args.filename.as_str().to_owned())
149153
.map(|o| o.into_object())
150154
.map_err(|err| vm.new_syntax_error(&err))
155+
} else {
156+
let mode = mode_str
157+
.parse::<parser::Mode>()
158+
.map_err(|err| vm.new_value_error(err.to_string()))?;
159+
ast::parse(&vm, &source, mode)
151160
}
152-
#[cfg(not(feature = "rustpython-compiler"))]
153-
{
154-
Err(vm.new_value_error("PyCF_ONLY_AST flag is required without compiler support"))
155-
}
156-
} else {
157-
use rustpython_parser::parser;
158-
let mode = mode_str
159-
.parse::<parser::Mode>()
160-
.map_err(|err| vm.new_value_error(err.to_string()))?;
161-
ast::parse(&vm, &source, mode)
161+
}
162+
#[cfg(not(feature = "rustpython-parser"))]
163+
{
164+
Err(vm.new_value_error(
165+
"PyCF_ONLY_AST flag is not supported without parser support".to_string(),
166+
))
162167
}
163168
}
164169

@@ -217,6 +222,7 @@ fn builtin_exec(
217222
run_code(vm, source, scope, compile::Mode::Exec)
218223
}
219224

225+
#[cfg(feature = "rustpython-compiler")]
220226
fn run_code(
221227
vm: &VirtualMachine,
222228
source: Either<PyStringRef, PyCodeRef>,
@@ -237,6 +243,7 @@ fn run_code(
237243
vm.run_code_obj(code_obj, scope)
238244
}
239245

246+
#[cfg(feature = "rustpython-compiler")]
240247
fn make_scope(vm: &VirtualMachine, scope: ScopeArgs) -> PyResult<Scope> {
241248
let globals = scope.globals;
242249
let current_scope = vm.current_scope();
@@ -776,6 +783,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
776783
extend_module!(vm, module, {
777784
"eval" => ctx.new_function(builtin_eval),
778785
"exec" => ctx.new_function(builtin_exec),
786+
"compile" => ctx.new_function(builtin_compile),
779787
});
780788
}
781789

@@ -796,7 +804,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
796804
"callable" => ctx.new_function(builtin_callable),
797805
"chr" => ctx.new_function(builtin_chr),
798806
"classmethod" => ctx.classmethod_type(),
799-
"compile" => ctx.new_function(builtin_compile),
800807
"complex" => ctx.complex_type(),
801808
"delattr" => ctx.new_function(builtin_delattr),
802809
"dict" => ctx.dict_type(),

0 commit comments

Comments
 (0)