chore: 允许 python-binding 不加载动态库而是直接返回#1159
Conversation
There was a problem hiding this comment.
Hey - 我在这里给出了一些整体性的反馈:
internal_mode检查会将任何非空字符串(包括 "0" 或 "false")视为真,从而跳过Library.open;建议把条件写得更明确一些(例如,与特定值如 "1" 或 "true" 对比),以避免由于环境变量配置错误而导致的意外行为。- 为了让导入时的行为更加清晰,你可以把
Library.open的逻辑和internal_mode检查一起封装到一个小的辅助函数中(例如maybe_open_library()),这样调用方和未来的代码阅读者就更容易理解动态库在什么情况下会被加载或跳过。
给 AI Agent 的提示
Please address the comments from this code review:
## Overall Comments
- The `internal_mode` check will treat any non-empty string (including "0" or "false") as truthy and skip `Library.open`; consider making the condition explicit (e.g., compare against a specific value like "1" or "true") to avoid surprising behavior from misconfigured environment variables.
- To make the import-time behavior clearer, you might wrap the `Library.open` logic and the `internal_mode` check in a small helper function (e.g., `maybe_open_library()`), so callers and future readers can more easily understand when the dynamic library is loaded or skipped.帮我变得更有用!请在每条评论上点 👍 或 👎,我会基于你的反馈改进后续的评审。
Original comment in English
Hey - I've left some high level feedback:
- The
internal_modecheck will treat any non-empty string (including "0" or "false") as truthy and skipLibrary.open; consider making the condition explicit (e.g., compare against a specific value like "1" or "true") to avoid surprising behavior from misconfigured environment variables. - To make the import-time behavior clearer, you might wrap the
Library.openlogic and theinternal_modecheck in a small helper function (e.g.,maybe_open_library()), so callers and future readers can more easily understand when the dynamic library is loaded or skipped.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `internal_mode` check will treat any non-empty string (including "0" or "false") as truthy and skip `Library.open`; consider making the condition explicit (e.g., compare against a specific value like "1" or "true") to avoid surprising behavior from misconfigured environment variables.
- To make the import-time behavior clearer, you might wrap the `Library.open` logic and the `internal_mode` check in a small helper function (e.g., `maybe_open_library()`), so callers and future readers can more easily understand when the dynamic library is loaded or skipped.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR adds an opt-out mechanism for the Python binding’s import-time initialization so that importing maa can skip calling Library.open() (controlled via an environment variable), enabling future internal tooling workflows that shouldn’t touch dynamic libraries by default.
Changes:
- Read a new environment variable
MAAFW_INTERNAL_MODE. - Skip
Library.open(__PATH, agent_server=False)when internal mode is enabled.
| internal_mode = os.environ.get("MAAFW_INTERNAL_MODE") | ||
|
|
||
| if env_path: | ||
| __PATH = Path(env_path) | ||
| else: | ||
| __PATH = Path(Path(__file__).parent, "bin") | ||
|
|
There was a problem hiding this comment.
internal_mode = os.environ.get("MAAFW_INTERNAL_MODE") combined with if not internal_mode: treats any non-empty string as enabled, so values like "0" / "false" will still skip Library.open() unexpectedly. Consider parsing explicit truthy values (e.g. "1"/"true") or checking for exact expected value to avoid surprising behavior.
| internal_mode = os.environ.get("MAAFW_INTERNAL_MODE") | |
| if env_path: | |
| __PATH = Path(env_path) | |
| else: | |
| __PATH = Path(Path(__file__).parent, "bin") | |
| internal_mode_value = os.environ.get("MAAFW_INTERNAL_MODE") | |
| internal_mode = ( | |
| str(internal_mode_value).strip().lower() in {"1", "true", "yes", "on"} | |
| if internal_mode_value is not None | |
| else False | |
| ) |
| env_path = os.environ.get("MAAFW_BINARY_PATH") | ||
| internal_mode = os.environ.get("MAAFW_INTERNAL_MODE") | ||
|
|
||
| if env_path: | ||
| __PATH = Path(env_path) | ||
| else: | ||
| __PATH = Path(Path(__file__).parent, "bin") | ||
|
|
||
| Library.open(__PATH, agent_server=False) | ||
| if not internal_mode: | ||
| Library.open(__PATH, agent_server=False) |
There was a problem hiding this comment.
MAAFW_INTERNAL_MODE is introduced as a new import-time switch but its semantics/expected values aren’t documented in-code. Adding a brief comment describing what it does and which values are accepted (and how to manually initialize Library.open() when enabled) would make this easier to maintain and use correctly.
|
smallest? |
|
总觉得在解决一些奇怪的问题( |
|
不需要这个了 可以用 importlib |
|
还好没合(小声蛐蛐) |
后面要用的妙妙工具 暂时没想到好的字段名
Summary by Sourcery
新功能:
Original summary in English
Summary by Sourcery
New Features:
Original summary in English
Summary by Sourcery
新功能:
Original summary in English
Summary by Sourcery
New Features: