Skip to content

chore: 允许 python-binding 不加载动态库而是直接返回#1159

Closed
weinibuliu wants to merge 1 commit intomainfrom
chore/python/noopen
Closed

chore: 允许 python-binding 不加载动态库而是直接返回#1159
weinibuliu wants to merge 1 commit intomainfrom
chore/python/noopen

Conversation

@weinibuliu
Copy link
Copy Markdown
Member

@weinibuliu weinibuliu commented Feb 27, 2026

后面要用的妙妙工具 暂时没想到好的字段名

Summary by Sourcery

新功能:

  • 引入一个环境变量,使 Python 绑定在运行时无需打开底层动态链接库。
Original summary in English

Summary by Sourcery

New Features:

  • Introduce an environment variable to allow running the Python binding without opening the underlying dynamic library.
- 引入一个环境变量,使得在不打开底层动态库的情况下运行 Python 绑定成为可能。
Original summary in English

Summary by Sourcery

新功能:

  • 引入一个环境变量,使 Python 绑定在运行时无需打开底层动态链接库。
Original summary in English

Summary by Sourcery

New Features:

  • Introduce an environment variable to allow running the Python binding without opening the underlying dynamic library.

@weinibuliu weinibuliu marked this pull request as ready for review February 27, 2026 19:47
Copilot AI review requested due to automatic review settings February 27, 2026 19:47
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sourcery 对开源项目是免费的 —— 如果你觉得我们的评审对你有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会基于你的反馈改进后续的评审。
Original comment in English

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +7 to 13
internal_mode = os.environ.get("MAAFW_INTERNAL_MODE")

if env_path:
__PATH = Path(env_path)
else:
__PATH = Path(Path(__file__).parent, "bin")

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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
)

Copilot uses AI. Check for mistakes.
Comment on lines 6 to +15
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)
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@MistEO
Copy link
Copy Markdown
Member

MistEO commented Feb 28, 2026

smallest?

@neko-para
Copy link
Copy Markdown
Contributor

总觉得在解决一些奇怪的问题(

@weinibuliu
Copy link
Copy Markdown
Member Author

不需要这个了 可以用 importlib

@weinibuliu weinibuliu closed this Mar 7, 2026
@MistEO
Copy link
Copy Markdown
Member

MistEO commented Mar 7, 2026

还好没合(小声蛐蛐)

@weinibuliu weinibuliu deleted the chore/python/noopen branch March 20, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants