Skip to content

Commit d32cd53

Browse files
committed
Move pylib dependency out of vm
1 parent 7b0fe1d commit d32cd53

File tree

11 files changed

+70
-52
lines changed

11 files changed

+70
-52
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ members = [
2222
default = ["threading", "stdlib", "zlib", "importlib", "encodings", "rustpython-parser/lalrpop"]
2323
importlib = ["rustpython-vm/importlib"]
2424
encodings = ["rustpython-vm/encodings"]
25-
stdlib = ["rustpython-stdlib"]
25+
stdlib = ["rustpython-stdlib", "rustpython-pylib"]
2626
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
27-
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
27+
freeze-stdlib = ["rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
2828
jit = ["rustpython-vm/jit"]
2929
threading = ["rustpython-vm/threading", "rustpython-stdlib/threading"]
3030
zlib = ["stdlib", "rustpython-stdlib/zlib"]
@@ -35,7 +35,8 @@ ssl-vendor = ["rustpython-stdlib/ssl-vendor"]
3535
[dependencies]
3636
rustpython-compiler = { path = "compiler", version = "0.1.1" }
3737
rustpython-parser = { path = "compiler/parser", version = "0.1.1" }
38-
rustpython-stdlib = {path = "stdlib", optional = true, default-features = false}
38+
rustpython-pylib = { path = "pylib", optional = true, default-features = false }
39+
rustpython-stdlib = { path = "stdlib", optional = true, default-features = false }
3940
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compiler"] }
4041

4142
cfg-if = "1.0.0"

pylib/Lib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Lib

pylib/src/lib.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,10 @@
66
// so build.rs sets this env var
77
pub const LIB_PATH: &str = match option_env!("win_lib_path") {
88
Some(s) => s,
9-
None => concat!(env!("CARGO_MANIFEST_DIR"), "/../../Lib"),
9+
None => concat!(env!("CARGO_MANIFEST_DIR"), "/../Lib"),
1010
};
1111

12-
use rustpython_compiler_core::FrozenModule;
13-
14-
pub fn frozen_builtins() -> impl Iterator<Item = (String, FrozenModule)> {
15-
rustpython_derive::py_freeze!(
16-
dir = "../vm/Lib/python_builtins",
17-
crate_name = "rustpython_compiler_core"
18-
)
19-
}
20-
21-
#[cfg(not(feature = "freeze-stdlib"))]
22-
pub fn frozen_core() -> impl Iterator<Item = (String, FrozenModule)> {
23-
rustpython_derive::py_freeze!(
24-
dir = "../vm/Lib/core_modules",
25-
crate_name = "rustpython_compiler_core"
26-
)
27-
}
28-
2912
#[cfg(feature = "freeze-stdlib")]
30-
pub fn frozen_stdlib() -> impl Iterator<Item = (String, FrozenModule)> {
13+
pub fn frozen_stdlib() -> impl Iterator<Item = (String, rustpython_compiler_core::FrozenModule)> {
3114
rustpython_derive::py_freeze!(dir = "../Lib", crate_name = "rustpython_compiler_core")
3215
}

src/lib.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,38 @@ fn add_stdlib(vm: &mut VirtualMachine) {
248248
let _ = vm;
249249
#[cfg(feature = "stdlib")]
250250
vm.add_native_modules(rustpython_stdlib::get_module_inits());
251+
252+
// if we're on freeze-stdlib, the core stdlib modules will be included anyway
253+
#[cfg(feature = "freeze-stdlib")]
254+
vm.add_frozen(rustpython_pylib::frozen_stdlib());
255+
256+
#[cfg(not(feature = "freeze-stdlib"))]
257+
{
258+
use rustpython_vm::common::rc::PyRc;
259+
let state = PyRc::get_mut(&mut vm.state).unwrap();
260+
261+
#[allow(clippy::needless_collect)] // false positive
262+
let path_list: Vec<_> = state.settings.path_list.drain(..).collect();
263+
264+
// add the current directory to sys.path
265+
state.settings.path_list.push("".to_owned());
266+
267+
// BUILDTIME_RUSTPYTHONPATH should be set when distributing
268+
if let Some(paths) = option_env!("BUILDTIME_RUSTPYTHONPATH") {
269+
state
270+
.settings
271+
.path_list
272+
.extend(split_paths(paths).map(|path| path.into_os_string().into_string().unwrap()))
273+
} else {
274+
#[cfg(feature = "rustpython-pylib")]
275+
state
276+
.settings
277+
.path_list
278+
.push(rustpython_pylib::LIB_PATH.to_owned())
279+
}
280+
281+
state.settings.path_list.extend(path_list.into_iter());
282+
}
251283
}
252284

253285
/// Create settings by examining command line arguments and environment
@@ -264,21 +296,6 @@ fn create_settings(matches: &ArgMatches) -> Settings {
264296

265297
let ignore_environment = settings.ignore_environment || settings.isolated;
266298

267-
// when rustpython-vm/pylib is enabled, Settings::default().path_list has pylib::LIB_PATH
268-
let maybe_pylib = settings.path_list.pop();
269-
270-
// add the current directory to sys.path
271-
settings.path_list.push("".to_owned());
272-
273-
// BUILDTIME_RUSTPYTHONPATH should be set when distributing
274-
if let Some(paths) = option_env!("BUILDTIME_RUSTPYTHONPATH") {
275-
settings
276-
.path_list
277-
.extend(split_paths(paths).map(|path| path.into_os_string().into_string().unwrap()))
278-
} else {
279-
settings.path_list.extend(maybe_pylib);
280-
}
281-
282299
if !ignore_environment {
283300
settings.path_list.extend(get_paths("RUSTPYTHONPATH"));
284301
settings.path_list.extend(get_paths("PYTHONPATH"));

vm/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ importlib = []
1414
encodings = ["importlib"]
1515
vm-tracing-logging = []
1616
flame-it = ["flame", "flamer"]
17-
freeze-stdlib = ["rustpython-pylib/freeze-stdlib"]
17+
freeze-stdlib = []
1818
jit = ["rustpython-jit"]
1919
threading = ["rustpython-common/threading"]
2020
compiler = ["parser", "codegen", "rustpython-compiler"]
@@ -31,7 +31,6 @@ rustpython-compiler-core = { path = "../compiler/core", version = "0.1.2" }
3131
rustpython-common = { path = "../common" }
3232
rustpython-derive = { path = "../derive", version = "0.1.2" }
3333
rustpython-jit = { path = "../jit", optional = true, version = "0.1.2" }
34-
rustpython-pylib = { path = "../pylib", version = "0.1.0" }
3534

3635
num-complex = { version = "0.4.0", features = ["serde"] }
3736
num-bigint = { version = "0.4.3", features = ["serde"] }

vm/src/frozen.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::bytecode::FrozenModule;
22

3-
pub fn get_module_inits() -> impl Iterator<Item = (String, FrozenModule)> {
3+
pub fn core_frozen_inits() -> impl Iterator<Item = (String, FrozenModule)> {
44
let iter = std::iter::empty();
55
macro_rules! ext_modules {
66
($iter:ident, ($modules:expr)) => {
@@ -20,16 +20,24 @@ pub fn get_module_inits() -> impl Iterator<Item = (String, FrozenModule)> {
2020
// Python modules that the vm calls into, but are not actually part of the stdlib. They could
2121
// in theory be implemented in Rust, but are easiest to do in Python for one reason or another.
2222
// Includes _importlib_bootstrap and _importlib_bootstrap_external
23-
ext_modules!(iter, (rustpython_pylib::frozen_builtins()));
23+
ext_modules!(
24+
iter,
25+
(rustpython_derive::py_freeze!(
26+
dir = "./Lib/python_builtins",
27+
crate_name = "rustpython_compiler_core"
28+
))
29+
);
2430

25-
#[cfg(not(feature = "freeze-stdlib"))]
2631
// core stdlib Python modules that the vm calls into, but are still used in Python
2732
// application code, e.g. copyreg
28-
ext_modules!(iter, (rustpython_pylib::frozen_core()));
29-
30-
// if we're on freeze-stdlib, the core stdlib modules will be included anyway
31-
#[cfg(feature = "freeze-stdlib")]
32-
ext_modules!(iter, (rustpython_pylib::frozen_stdlib()));
33+
#[cfg(not(feature = "freeze-stdlib"))]
34+
ext_modules!(
35+
iter,
36+
(rustpython_derive::py_freeze!(
37+
dir = "./Lib/core_modules",
38+
crate_name = "rustpython_compiler_core"
39+
))
40+
);
3341

3442
iter
3543
}

vm/src/vm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl VirtualMachine {
176176
panic!("Interpreters in same process must share the hash seed");
177177
}
178178

179-
let frozen = frozen::get_module_inits().collect();
179+
let frozen = frozen::core_frozen_inits().collect();
180180
PyRc::get_mut(&mut vm.state).unwrap().frozen = frozen;
181181

182182
vm.builtins

vm/src/vm/setting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Default for Settings {
8989
dev_mode: false,
9090
warn_default_encoding: false,
9191
warnopts: vec![],
92-
path_list: vec![rustpython_pylib::LIB_PATH.to_owned()],
92+
path_list: vec![],
9393
argv: vec![],
9494
hash_seed: None,
9595
stdio_unbuffered: false,

wasm/lib/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ edition = "2021"
1111
crate-type = ["cdylib", "rlib"]
1212

1313
[features]
14-
default = ["freeze-stdlib", "rustpython-stdlib"]
15-
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
14+
default = ["stdlib"]
15+
stdlib = ["freeze-stdlib", "rustpython-pylib", "rustpython-stdlib"]
16+
freeze-stdlib = ["rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
1617
no-start-func = []
1718

1819
[dependencies]
1920
rustpython-common = { path = "../../common" }
2021
rustpython-parser = { path = "../../compiler/parser" }
22+
rustpython-pylib = { path = "../../pylib", default-features = false, optional = true }
2123
rustpython-stdlib = { path = "../../stdlib", default-features = false, optional = true }
2224
# make sure no threading! otherwise wasm build will fail
2325
rustpython-vm = { path = "../../vm", default-features = false, features = ["compiler", "encodings"] }

wasm/lib/src/vm_class.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ impl StoredVirtualMachine {
4242
let mut settings = Settings::default();
4343
settings.allow_external_library = false;
4444
let interp = Interpreter::with_init(settings, |vm| {
45+
#[cfg(feature = "stdlib")]
46+
vm.add_native_modules(rustpython_stdlib::get_module_inits());
47+
48+
#[cfg(feature = "freeze-stdlib")]
49+
vm.add_frozen(rustpython_pylib::frozen_stdlib());
50+
4551
vm.wasm_id = Some(id);
4652

4753
js_module::setup_js_module(vm);

0 commit comments

Comments
 (0)