Skip to content

Commit ea71428

Browse files
authored
Merge pull request RustPython#1166 from palaviv/write-bytecode
Write bytecode
2 parents 6ca979e + dde40a4 commit ea71428

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
8484
.short("S")
8585
.help("don't imply 'import site' on initialization"),
8686
)
87+
.arg(
88+
Arg::with_name("dont-write-bytecode")
89+
.short("B")
90+
.help("don't write .pyc files on import"),
91+
)
8792
.arg(
8893
Arg::with_name("ignore-environment")
8994
.short("E")
@@ -171,6 +176,12 @@ fn create_settings(matches: &ArgMatches) -> PySettings {
171176
settings.quiet = true;
172177
}
173178

179+
if matches.is_present("dont-write-bytecode")
180+
|| (!ignore_environment && env::var_os("PYTHONDONTWRITEBYTECODE").is_some())
181+
{
182+
settings.dont_write_bytecode = true;
183+
}
184+
174185
settings
175186
}
176187

vm/src/import.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
22
* Import mechanics
33
*/
4+
use rand::Rng;
45

56
use crate::bytecode::CodeObject;
67
use crate::obj::{objcode, objsequence, objstr, objtype};
78
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, PyValue};
89
use crate::scope::Scope;
10+
use crate::version::get_git_revision;
911
use crate::vm::VirtualMachine;
1012
#[cfg(feature = "rustpython-compiler")]
1113
use rustpython_compiler::compile;
@@ -23,6 +25,15 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
2325
let install_external =
2426
vm.get_attribute(importlib.clone(), "_install_external_importers")?;
2527
vm.invoke(install_external, vec![])?;
28+
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
29+
let importlib_external =
30+
vm.import("_frozen_importlib_external", &vm.ctx.new_tuple(vec![]), 0)?;
31+
let mut magic = get_git_revision().into_bytes();
32+
magic.truncate(4);
33+
if magic.len() != 4 {
34+
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
35+
}
36+
vm.set_attr(&importlib_external, "MAGIC_NUMBER", vm.ctx.new_bytes(magic))?;
2637
}
2738
Ok(vm.get_none())
2839
}

vm/src/sysmodule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl SysFlags {
8888
flags.ignore_environment = settings.ignore_environment;
8989
flags.verbose = settings.verbose;
9090
flags.quiet = settings.quiet;
91+
flags.dont_write_bytecode = settings.dont_write_bytecode;
9192
flags
9293
}
9394
}
@@ -322,7 +323,7 @@ settrace() -- set the global debug tracing function
322323
"path_hooks" => ctx.new_list(vec![]),
323324
"path_importer_cache" => ctx.new_dict(),
324325
"pycache_prefix" => vm.get_none(),
325-
"dont_write_bytecode" => vm.new_bool(true),
326+
"dont_write_bytecode" => vm.new_bool(vm.settings.dont_write_bytecode),
326327
"setprofile" => ctx.new_rustfunc(sys_setprofile),
327328
"settrace" => ctx.new_rustfunc(sys_settrace),
328329
"version" => vm.new_str(version::get_version()),

vm/src/vm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ pub struct PySettings {
9090
/// -q
9191
pub quiet: bool,
9292

93+
/// -B
94+
pub dont_write_bytecode: bool,
95+
9396
/// Environment PYTHONPATH and RUSTPYTHONPATH:
9497
pub path_list: Vec<String>,
9598
}
@@ -122,6 +125,7 @@ impl Default for PySettings {
122125
ignore_environment: false,
123126
verbose: 0,
124127
quiet: false,
128+
dont_write_bytecode: false,
125129
path_list: vec![],
126130
}
127131
}

0 commit comments

Comments
 (0)