From 4dd7a6950d17f31af5943041330e17d96f8674ea Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 26 Feb 2023 19:02:05 +0900 Subject: [PATCH] Fix py_freeze! to run library with __main__ without __init__ --- derive-impl/src/compile_bytecode.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/derive-impl/src/compile_bytecode.rs b/derive-impl/src/compile_bytecode.rs index e72ad2bf9db..1aa3df9006f 100644 --- a/derive-impl/src/compile_bytecode.rs +++ b/derive-impl/src/compile_bytecode.rs @@ -167,6 +167,7 @@ impl CompilationSource { } else if file_name.ends_with(".py") { let stem = path.file_stem().unwrap().to_str().unwrap(); let is_init = stem == "__init__"; + let is_main = stem == "__main__"; let module_name = if is_init { parent.clone() } else if parent.is_empty() { @@ -215,6 +216,16 @@ impl CompilationSource { Err(e) => return Err(e), }; + if is_main && !code_map.contains_key(&parent) { + // FIXME: the parent module doesn't need to be a full module copy. + code_map.insert( + parent.clone(), + FrozenModule { + code: code.clone(), + package: true, + }, + ); + } code_map.insert( module_name, FrozenModule {