File tree Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,11 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
84
84
. short ( "S" )
85
85
. help ( "don't imply 'import site' on initialization" ) ,
86
86
)
87
+ . arg (
88
+ Arg :: with_name ( "dont-write-bytecode" )
89
+ . short ( "B" )
90
+ . help ( "don't write .pyc files on import" ) ,
91
+ )
87
92
. arg (
88
93
Arg :: with_name ( "ignore-environment" )
89
94
. short ( "E" )
@@ -171,6 +176,12 @@ fn create_settings(matches: &ArgMatches) -> PySettings {
171
176
settings. quiet = true ;
172
177
}
173
178
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
+
174
185
settings
175
186
}
176
187
Original file line number Diff line number Diff line change 1
1
/*
2
2
* Import mechanics
3
3
*/
4
+ use rand:: Rng ;
4
5
5
6
use crate :: bytecode:: CodeObject ;
6
7
use crate :: obj:: { objcode, objsequence, objstr, objtype} ;
7
8
use crate :: pyobject:: { ItemProtocol , PyObjectRef , PyResult , PyValue } ;
8
9
use crate :: scope:: Scope ;
10
+ use crate :: version:: get_git_revision;
9
11
use crate :: vm:: VirtualMachine ;
10
12
#[ cfg( feature = "rustpython-compiler" ) ]
11
13
use rustpython_compiler:: compile;
@@ -23,6 +25,15 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
23
25
let install_external =
24
26
vm. get_attribute ( importlib. clone ( ) , "_install_external_importers" ) ?;
25
27
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) ) ?;
26
37
}
27
38
Ok ( vm. get_none ( ) )
28
39
}
Original file line number Diff line number Diff line change @@ -88,6 +88,7 @@ impl SysFlags {
88
88
flags. ignore_environment = settings. ignore_environment ;
89
89
flags. verbose = settings. verbose ;
90
90
flags. quiet = settings. quiet ;
91
+ flags. dont_write_bytecode = settings. dont_write_bytecode ;
91
92
flags
92
93
}
93
94
}
@@ -322,7 +323,7 @@ settrace() -- set the global debug tracing function
322
323
"path_hooks" => ctx. new_list( vec![ ] ) ,
323
324
"path_importer_cache" => ctx. new_dict( ) ,
324
325
"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 ) ,
326
327
"setprofile" => ctx. new_rustfunc( sys_setprofile) ,
327
328
"settrace" => ctx. new_rustfunc( sys_settrace) ,
328
329
"version" => vm. new_str( version:: get_version( ) ) ,
Original file line number Diff line number Diff line change @@ -90,6 +90,9 @@ pub struct PySettings {
90
90
/// -q
91
91
pub quiet : bool ,
92
92
93
+ /// -B
94
+ pub dont_write_bytecode : bool ,
95
+
93
96
/// Environment PYTHONPATH and RUSTPYTHONPATH:
94
97
pub path_list : Vec < String > ,
95
98
}
@@ -122,6 +125,7 @@ impl Default for PySettings {
122
125
ignore_environment : false ,
123
126
verbose : 0 ,
124
127
quiet : false ,
128
+ dont_write_bytecode : false ,
125
129
path_list : vec ! [ ] ,
126
130
}
127
131
}
You can’t perform that action at this time.
0 commit comments