Skip to content

Commit 582932c

Browse files
committed
Add freeze example
1 parent 3d0c143 commit 582932c

5 files changed

Lines changed: 67 additions & 15 deletions

File tree

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", "freeze"]
1212

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

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 }

freeze/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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<&str, vm::bytecode::FrozenModule> = vm::py_compile_bytecode!(
11+
source = "print(\"Hello world1!\")\n",
12+
module_name = "__main__"
13+
);
14+
15+
vm.run_code_obj(
16+
vm.ctx
17+
.new_code_object(modules.get("__main__").unwrap().code.clone()),
18+
scope,
19+
)?;
20+
21+
Ok(())
22+
}

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};
@@ -31,6 +33,7 @@ use crate::pyobject::{
3133
TypeProtocol,
3234
};
3335
use crate::scope::Scope;
36+
#[cfg(feature = "rustpython-parser")]
3437
use crate::stdlib::ast;
3538
#[cfg(not(target_arch = "wasm32"))]
3639
use crate::stdlib::io::io_open;
@@ -126,6 +129,7 @@ struct CompileArgs {
126129
optimize: OptionalArg<PyIntRef>,
127130
}
128131

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

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

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

@@ -218,6 +223,7 @@ fn builtin_exec(
218223
run_code(vm, source, scope, compile::Mode::Exec)
219224
}
220225

226+
#[cfg(feature = "rustpython-compiler")]
221227
fn run_code(
222228
vm: &VirtualMachine,
223229
source: Either<PyStringRef, PyCodeRef>,
@@ -238,6 +244,7 @@ fn run_code(
238244
vm.run_code_obj(code_obj, scope)
239245
}
240246

247+
#[cfg(feature = "rustpython-compiler")]
241248
fn make_scope(vm: &VirtualMachine, scope: ScopeArgs) -> PyResult<Scope> {
242249
let globals = scope.globals;
243250
let current_scope = vm.current_scope();
@@ -767,6 +774,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
767774
extend_module!(vm, module, {
768775
"eval" => ctx.new_function(builtin_eval),
769776
"exec" => ctx.new_function(builtin_exec),
777+
"compile" => ctx.new_function(builtin_compile),
770778
});
771779
}
772780

@@ -787,7 +795,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
787795
"callable" => ctx.new_function(builtin_callable),
788796
"chr" => ctx.new_function(builtin_chr),
789797
"classmethod" => ctx.classmethod_type(),
790-
"compile" => ctx.new_function(builtin_compile),
791798
"complex" => ctx.complex_type(),
792799
"delattr" => ctx.new_function(builtin_delattr),
793800
"dict" => ctx.dict_type(),

0 commit comments

Comments
 (0)