Skip to content

scripts/lib 中两处 f-string 写法与旧版 Python 不兼容,会触发 SyntaxError #4

@stabey

Description

@stabey

问题描述

当前仓库 main 分支(我本地验证时为 v3.3.0 / 750ae4b)中,scripts/lib 下有两处 Python 代码在 较老的 Python 版本 下会在导入/编译阶段直接触发 SyntaxError,导致相关 SSH 功能无法正常使用。

具体位置:

  • scripts/lib/native_ssh_client.py:380
  • scripts/lib/paramiko_client.py:1170

对应代码分别类似:

yield f"[STDERR] {line.rstrip('\\n')}"

以及:

yield f"[STDERR] {line.rstrip('\\\\n')}"

版本情况

我本地实际测试结果:

  • python3Python 3.9.6:会报错
  • python3.14Python 3.14.3:可正常通过编译

也就是说,这个问题更准确地说是一个 旧版本 Python 兼容性问题,并不是所有 Python 版本都会报错。

复现方式

在较老 Python(我这里是 3.9.6)下执行:

python3 -m py_compile scripts/lib/native_ssh_client.py
python3 -m py_compile scripts/lib/paramiko_client.py

我本地得到的结果:

File \"scripts/lib/native_ssh_client.py\", line 380
  yield f\"[STDERR] {line.rstrip('\\n')}\"
                                       ^
SyntaxError: f-string expression part cannot include a backslash
File \"scripts/lib/paramiko_client.py\", line 1170
  yield f\"[STDERR] {line.rstrip('\\\\n')}\"
                                        ^
SyntaxError: f-string expression part cannot include a backslash

而在 python3.14.3 下,这两个文件都可以通过:

python3.14 -m py_compile scripts/lib/native_ssh_client.py
python3.14 -m py_compile scripts/lib/paramiko_client.py

影响

我这里执行:

python3 ~/.claude/skills/ssh-skill/scripts/ssh_execute.py rn "true"

在修复前会因为导入 scripts/lib/* 里的代码而直接失败,还没真正开始 SSH 连接就中断了。

引入时间

我查了一下 git 历史,这两个问题行都是在下面这个提交中引入的:

  • f6118807 - fix: 修复 .gitignore 误排除 scripts/lib 目录导致核心模块缺失

兼容性修复建议

如果项目希望兼容旧版 Python,可以避免在这两处使用这种写法,改成例如:

yield "[STDERR] " + line.rstrip("\n")

或者:

clean = line.rstrip("\n")
yield f"[STDERR] {clean}"

这样在旧版 Python 上也能正常工作。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions