From 40ac4ca0be370a7e7f27b1d230d62af7d70be3c7 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Tue, 29 Oct 2019 21:45:10 -0500 Subject: [PATCH] Reorganize vm/Lib and frozen.rs --- {vm/Lib => Lib/importlib}/_bootstrap.py | 0 .../importlib}/_bootstrap_external.py | 0 vm/Lib/README.md | 6 +++ vm/Lib/core_modules/copyreg.py | 1 + vm/Lib/{ => python_builtins}/__reducelib.py | 0 vm/Lib/python_builtins/_frozen_importlib.py | 1 + .../_frozen_importlib_external.py | 1 + vm/src/frozen.rs | 41 ++++++++++--------- 8 files changed, 31 insertions(+), 19 deletions(-) rename {vm/Lib => Lib/importlib}/_bootstrap.py (100%) rename {vm/Lib => Lib/importlib}/_bootstrap_external.py (100%) create mode 100644 vm/Lib/README.md create mode 120000 vm/Lib/core_modules/copyreg.py rename vm/Lib/{ => python_builtins}/__reducelib.py (100%) create mode 120000 vm/Lib/python_builtins/_frozen_importlib.py create mode 120000 vm/Lib/python_builtins/_frozen_importlib_external.py diff --git a/vm/Lib/_bootstrap.py b/Lib/importlib/_bootstrap.py similarity index 100% rename from vm/Lib/_bootstrap.py rename to Lib/importlib/_bootstrap.py diff --git a/vm/Lib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py similarity index 100% rename from vm/Lib/_bootstrap_external.py rename to Lib/importlib/_bootstrap_external.py diff --git a/vm/Lib/README.md b/vm/Lib/README.md new file mode 100644 index 0000000000..84fb705783 --- /dev/null +++ b/vm/Lib/README.md @@ -0,0 +1,6 @@ +# Frozen Python modules + +A collection of Python modules frozen into the VM at compile time. + +See the comments in [`frozen.rs`](../src/frozen.rs) for explanations of the +different files and subdirectories in this directory. diff --git a/vm/Lib/core_modules/copyreg.py b/vm/Lib/core_modules/copyreg.py new file mode 120000 index 0000000000..4ac7f40c43 --- /dev/null +++ b/vm/Lib/core_modules/copyreg.py @@ -0,0 +1 @@ +../../../Lib/copyreg.py \ No newline at end of file diff --git a/vm/Lib/__reducelib.py b/vm/Lib/python_builtins/__reducelib.py similarity index 100% rename from vm/Lib/__reducelib.py rename to vm/Lib/python_builtins/__reducelib.py diff --git a/vm/Lib/python_builtins/_frozen_importlib.py b/vm/Lib/python_builtins/_frozen_importlib.py new file mode 120000 index 0000000000..d1d4364aba --- /dev/null +++ b/vm/Lib/python_builtins/_frozen_importlib.py @@ -0,0 +1 @@ +../../../Lib/importlib/_bootstrap.py \ No newline at end of file diff --git a/vm/Lib/python_builtins/_frozen_importlib_external.py b/vm/Lib/python_builtins/_frozen_importlib_external.py new file mode 120000 index 0000000000..ba8aff5805 --- /dev/null +++ b/vm/Lib/python_builtins/_frozen_importlib_external.py @@ -0,0 +1 @@ +../../../Lib/importlib/_bootstrap_external.py \ No newline at end of file diff --git a/vm/src/frozen.rs b/vm/src/frozen.rs index d0b6a38d86..31b563ea93 100644 --- a/vm/src/frozen.rs +++ b/vm/src/frozen.rs @@ -3,30 +3,33 @@ use std::collections::HashMap; pub fn get_module_inits() -> HashMap { let mut modules = HashMap::new(); - modules.extend(py_compile_bytecode!( + + macro_rules! ext_modules { + ($($t:tt)*) => { + modules.extend(py_compile_bytecode!($($t)*)); + }; + } + + ext_modules!( source = "initialized = True; print(\"Hello world!\")\n", module_name = "__hello__", - )); - modules.extend(py_compile_bytecode!( - file = "Lib/_bootstrap.py", - module_name = "_frozen_importlib", - )); - modules.extend(py_compile_bytecode!( - file = "Lib/_bootstrap_external.py", - module_name = "_frozen_importlib_external", - )); - modules.extend(py_compile_bytecode!( - file = "../Lib/copyreg.py", - module_name = "copyreg", - )); - modules.extend(py_compile_bytecode!( - file = "Lib/__reducelib.py", - module_name = "__reducelib", - )); + ); + // Python modules that the vm calls into, but are not actually part of the stdlib. They could + // in theory be implemented in Rust, but are easiest to do in Python for one reason or another. + // Includes _importlib_bootstrap and _importlib_bootstrap_external + ext_modules!(dir = "Lib/python_builtins/"); + + #[cfg(not(feature = "freeze-stdlib"))] + { + // core stdlib Python modules that the vm calls into, but are still used in Python + // application code, e.g. copyreg + ext_modules!(dir = "Lib/core_modules/"); + } + // if we're on freeze-stdlib, the core stdlib modules will be included anyway #[cfg(feature = "freeze-stdlib")] { - modules.extend(py_compile_bytecode!(dir = "../Lib/")); + ext_modules!(dir = "../Lib/"); } modules