#2017·serena

在查找引用符号时,如果引用文件的编码与项目编码不同,会发生 UnicodeDecodeError(严重崩溃)

作者: davalillo创建于 2026年9月11日更新于 2026年9月11日
  1. 创建一个具有混合编码的项目:
bash
mkdir mixed-encoding-project && cd mixed-encoding-project
cat > main_utf8.py << 'EOF'
from utf16_module import helper_function
def main():
    helper_function(42)
if __name__ == "__main__":
    main()
EOF
cat > utf16_module.py << 'EOF'
# UTF-16 文件带有 BOM (如 PowerShell 或某些 Windows 编辑器保存的文件)
def helper_function(value):
    print("helper called with", value)
def caller_function():
    helper_function(42)
def main_entry():
    caller_function()
EOF
# 将 utf16_module.py 转换为 UTF-16 LE 文件带有 BOM:
python3 -c "
data = open('utf16_module.py', 'rb').read()
open('utf16_module.py', 'wb').write(b'\xff\xfe' + data.decode('utf-8').encode('utf-16-le'))
"
2. 使用 Serena 启动项目(Python LSP 后端,默认在 .serena/project.yml 中设置为 encoding: utf-8)。
3. 为 utf16_module.py 中的 caller_function 执行 find_referencing_symbols。该引用(来自 main_entry)位于 **UTF-16 文件本身**,因此工具必须读取它以生成 content_around_reference。