如何提交 Pillow PR 并通过 CI?

解读

面试官问“如何提交 Pillow PR 并通过 CI”,表面看是流程题,实则考察候选人对开源协作规范、Python 生态、图像处理库质量门槛的综合理解。Pillow 作为事实上的“CIO”,其主仓库对代码风格、测试覆盖、平台兼容性、回归测试要求极高;国内候选人往往熟悉 Git 基本操作,却忽视“让 CI 绿灯”背后的细节:如 Tidelift 合规、Docker 镜像一致性、测试图片版权、多平台 wheel 构建等。答不出“为什么本地跑通,CI 仍可能挂”就很难拿到高分。

知识点

  1. Pillow 组织与分支模型:主仓库 python-pillow/Pillow,默认主分支 main,只接受 PR,不接受直接推送。
  2. 开发环境:官方推荐 tox + pytest,需安装 libjpeg-dev zlib-dev libtiff-dev libwebp-dev openjpeg libimagequant 等系统依赖;国内 Debian/Ubuntu 镜像源常缺 libimagequant-dev,需源码编译或 conda-forge。
  3. 代码风格:Black 22 版以上格式化、isort 5 排序、flake8 过检;PR 差异行必须 0 警告。
  4. 测试策略:
    ‑ 单元测试放在 Tests/ 目录,命名 test_*.py,单测必须自给自足(不依赖外部网络)。
    ‑ 新增功能必须同步提交测试图片,图片需 CC0 或 Pillow 自有版权,大小 < 50 KB;禁止随手拍或网络下载。
    ‑ 回归测试用 assert_image_equal、assert_image_similar,差异像素阈值默认 0。
  5. CI 矩阵:GitHub Actions 驱动,覆盖 Linux(x86_64、aarch64)、macOS(x86_64、arm64)、Windows(x86、x64),Python 3.9-3.12,以及 PyPy3.9;另加一行“wheels” job 构建多平台 wheel。任何 job 失败即整单红灯。
  6. 变更日志与文档:CHANGES.rst 按版本倒序写入简要条目;若改公开 API,需在 docs/references/ 对应 rst 文件更新,并运行 make html 确保无 Sphinx 警告。
  7. 签署 CLA:Pillow 采用 Apache 2.0,首次 PR 需评论“I have read the CLA Document and I hereby sign the CLA”,否则机器人 block。
  8. 性能基准:若改动热路径(如 resize、convert),需在 PR 描述贴 python -m pytest Tests/test_image.py::test_resize -v --benchmark-only 结果,下降 > 5 % 会被要求解释或优化。
  9. 回滚策略:合并后 24 h 内官方发现 regression 可强制 revert,作者需在 12 h 内响应修复或提供 hotfix PR。

答案

步骤化落地,兼顾国内网络痛点:

  1. 一次性准备
    a. 配置 GitHub 账号并开启 2FA,否则无法推送。
    b. 在本地 /etc/hosts 追加 140.82.113.3 github.com 缓解 DNS 污染。
    c. 创建 fork 并克隆:
    git clone https://github.com/<你的ID>/Pillow.git && cd Pillow
    d. 添加主仓库为上游:
    git remote add upstream https://github.com/python-pillow/Pillow.git

  2. 构建可复现环境(以 Ubuntu 22.04 为例)

    sudo apt-get update && sudo apt-get install -y \
      libjpeg-dev zlib1g-dev libtiff-dev libwebp-dev \
      libopenjp2-7-dev libimagequant-dev libxcb-dev libfreetype6-dev \
      python3-dev python3-venv
    
    python3 -m venv venv
    source venv/bin/activate
    pip install -U pip tox
    

    若 libimagequant-dev 找不到,用 conda install -c conda-forge libimagequant 后 export CFLAGS="-I$CONDA_PREFIX/include" 再安装 Pillow。

  3. 创建特性分支
    git checkout -b fix-webp-encoder-crash
    命名规则:动词-格式-简述,禁止拼音或无意义缩写。

  4. 编码与自测
    ‑ 修改核心代码前,先跑一遍全量测试确保基线:
    tox -e py311 -- -x
    ‑ 编写业务代码,同步在 Tests/test_file_webp.py 新增 test_encoder_crash();测试图片放 Tests/images/webp_encoder_crash.webp,执行
    python -m pytest Tests/test_file_webp.py::test_encoder_crash -v
    ‑ Black + isort + flake8 零警告:
    tox -e lint

  5. 本地多平台预检(可选但加分)
    利用 GitHub Actions 的 act 工具在本地模拟:
    act -j TestLinux --container-arch linux/amd64
    提前发现“Windows 路径分隔符”或“macOS 大小写”问题。

  6. 提交与推送

    git add -A
    git commit -m "Fix WebP encoder crash on large metadata  
    
    - Add bounds check for chunk size
    - Add regression test
    - Pass CI on all platforms"
    git push origin fix-webp-encoder-crash
    

    注意 commit message 英文、50 字内首行概述,空一行后列 bullet。

  7. 打开 PR 模板 checklist
    勾选:
    ‑ I have read the CLA Document and I hereby sign the CLA.
    ‑ I have added tests for my changes.
    ‑ I have updated documentation if necessary.
    ‑ My changes generate no new warnings.
    在描述里贴本地 tox -e py311 结果截图(文本复制即可),并 @ 相关维护者。

  8. 跟进 CI
    国内常见失败点:
    ‑ test_file_libtiff.py 因网络拉取 libtiff-4.5.0.tar.gz 超时;解决:在 PR 评论 /retry 或手动重跑。
    ‑ aarch64 容器 OOM;官方会自动重跑,无需改动代码。
    若出现测试失败,先确认是否 flaky,再 push fix commit;禁止 force-push 覆盖历史,保持 review 可追溯。

  9. 代码审查与合并
    维护者提出修改意见后,48 小时内响应;如需大改,可关闭当前 PR 重新打开新 PR,避免冗长折返。通过审查后,Squash Merge 到 main,自动触发 wheel 构建并上传 PyPI nightly。

拓展思考

  1. 国内镜像加速:清华大学 TUNA 镜像站已同步 Pillow 依赖包,可在 tox.ini 里注入 PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple,但注意与官方 CI 保持一致,勿将临时镜像配置提交到 PR。
  2. 私有 CI 预检:企业如阿里、腾讯内部 GitLab 可镜像 Pillow 仓库,用自研 ARM 池提前跑 test_image.py,降低公开 CI 排队时间;但需确保不外泄测试图片版权。
  3. 回归测试可视化:可写脚本把 assert_image_similar 的差异图上传至企业 OSS,生成 MR 评论预览,方便审查者肉眼确认。
  4. 安全响应:Pillow 每年发布安全公告(如 CVE-2023-4427),若你的 PR 涉及解码器边界修复,应同步申请 CVE 编号,并在 CHANGES.rst 标注“Security: Fixed …”,提升个人开源影响力。