From f3acada4298090b91d97c23433f9daa445631b9a Mon Sep 17 00:00:00 2001 From: HaibinLai <12211612@mail.sustech.edu.cn> Date: Thu, 26 Mar 2026 13:11:21 +0000 Subject: [PATCH] fix: validate Python module syntax during module_manager install --- bmf/cmd/src/module_manager.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/bmf/cmd/src/module_manager.cpp b/bmf/cmd/src/module_manager.cpp index 802ba42c..0740c502 100644 --- a/bmf/cmd/src/module_manager.cpp +++ b/bmf/cmd/src/module_manager.cpp @@ -114,8 +114,32 @@ int install_module(const bmf_sdk::ModuleInfo &info, bool force) { (module_file + bmf_sdk::SharedLibrary::default_extension())) .string(); } else { + // python module module_file = (fs::path(info.module_path) / (module_file + ".py")).string(); + if (fs::exists(module_file)) { + // validate python syntax before installing + std::string check_cmd = + "python3 -m py_compile \"" + module_file + "\" 2>&1"; + FILE *pipe = popen(check_cmd.c_str(), "r"); + if (pipe) { + char buffer[256]; + std::string result; + while (fgets(buffer, sizeof(buffer), pipe) != nullptr) { + result += buffer; + } + int status = pclose(pipe); + if (status != 0) { + BMFLOG(BMF_ERROR) + << "Python syntax check failed for: " + << module_file << std::endl; + if (!result.empty()) { + BMFLOG(BMF_ERROR) << result << std::endl; + } + return -1; + } + } + } } if (!fs::exists(module_file)) { BMFLOG(BMF_ERROR)