修复:输出前缀绑定替换保留原引号,避免 Python -c 字符串字面量变裸标识符(zh01 NameError)

This commit is contained in:
potaser
2026-08-29 23:57:45 +08:00
parent 6a4db21891
commit f712a657c0
2 changed files with 43 additions and 1 deletions
@@ -1536,6 +1536,20 @@ def _shell_word_pattern(literal: str) -> re.Pattern[str]:
)
def _shell_word_replacement(replacement: str) -> Callable[[re.Match[str]], str]:
"""替换函数:保留原文中字面量自带的引号。
``shlex.quote`` 在 shell 上下文是正确的,但 ``python -c "...'a.fa'..."`` 里的
字符串字面量替换后必须保留引号,否则会变成裸标识符导致 NameError。
"""
def _replace(match: re.Match[str]) -> str:
quote = match.group("quote") or ""
return f"{quote}{replacement}{quote}"
return _replace
def _script_positional_args(contract: dict[str, Any], script: str) -> list[str]:
try:
tokens = shlex.split(str(contract.get("original_command") or ""))
@@ -1710,7 +1724,7 @@ def _render_script_updates(
"PATCH_TARGET_INVALID", f"invalid match count for {literal}"
)
updated, count = _shell_word_pattern(literal).subn(
shlex.quote(replacement), text
_shell_word_replacement(replacement), text
)
if count < expected:
raise RuntimeBindingError(