Skip to content

Key cffi build-script cache on PYO3_BASE_PYTHON when available#14984

Draft
alex wants to merge 1 commit into
pyca:mainfrom
alex:cffi-pyo3-base-python-rerun
Draft

Key cffi build-script cache on PYO3_BASE_PYTHON when available#14984
alex wants to merge 1 commit into
pyca:mainfrom
alex:cffi-pyo3-base-python-rerun

Conversation

@alex

@alex alex commented Jun 8, 2026

Copy link
Copy Markdown
Member

Under PEP 517 build isolation, frontends (pip/uv/build) create venvs at random paths and point PYO3_PYTHON at them. Because cryptography-cffi's build.rs declares cargo:rerun-if-env-changed=PYO3_PYTHON, that path churn makes the build script re-run on every invocation, defeating build caching. This is the same problem tracked upstream in PyO3/pyo3#6113, which proposes a stable PYO3_BASE_PYTHON (≈ sys._base_executable).

This change mirrors the proposed "prefer BASE if present" behavior for this crate's build script: when PYO3_BASE_PYTHON is set, key the rerun on it; otherwise fall back to PYO3_PYTHON.

Why keying on the base interpreter is sound here: a venv cannot change the implementation (CPython/PyPy), version, ABI (free-threaded vs. not), or C headers of its base — and those are the only interpreter-derived inputs this script has. We still invoke PYO3_PYTHON to locate the binary, since build_openssl.py needs cffi, which lives in the build venv rather than the base interpreter. The fallback preserves correct rebuild behavior for manual interpreter switches (e.g. setting PYO3_PYTHON in a dev checkout) when PYO3_BASE_PYTHON is absent.

Note: this is forward-looking — it's harmless until a frontend actually sets PYO3_BASE_PYTHON, at which point caching improves.

🤖 Generated with Claude Code

PEP 517 build isolation creates venvs at random paths and points
PYO3_PYTHON at them, so a `rerun-if-env-changed=PYO3_PYTHON` makes the
build script re-run on every invocation and defeats caching (see
PyO3/pyo3#6113).

When PYO3_BASE_PYTHON (the stable underlying interpreter) is present,
key the rerun on that instead. A venv cannot change the implementation,
version, ABI, or headers of its base interpreter, which is all this
script derives -- so the base path is a sound cache key. We still invoke
PYO3_PYTHON to locate the binary, since the build venv is where cffi is
installed. When PYO3_BASE_PYTHON is absent we fall back to keying on
PYO3_PYTHON so manual interpreter switches still trigger a rebuild.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@alex

alex commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

(Shouldn't land until we depend on a pyo3 release with this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant