From c643ae4b955d3db881cbbe57a326261390ff127a Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Wed, 10 Jun 2026 11:35:06 -0400 Subject: [PATCH] PYTHON-5839 - PyMongo wheels have multiple versions of C extensions --- .github/workflows/dist.yml | 3 +++ tools/clean.py | 38 +++++++++----------------------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/.github/workflows/dist.yml b/.github/workflows/dist.yml index 0b8947c2d3..9979758153 100644 --- a/.github/workflows/dist.yml +++ b/.github/workflows/dist.yml @@ -78,6 +78,9 @@ jobs: - name: Build wheels env: CIBW_BUILD: ${{ matrix.buildplat[2] }} + # Strip any in-place C extensions from previous builds + # so they don't leak into the next wheel. + CIBW_BEFORE_BUILD: python ./tools/clean.py run: python -m cibuildwheel --output-dir wheelhouse - name: Assert all versions in wheelhouse diff --git a/tools/clean.py b/tools/clean.py index b6e1867a0a..b3e9799b80 100644 --- a/tools/clean.py +++ b/tools/clean.py @@ -1,4 +1,4 @@ -# Copyright 2009-2015 MongoDB, Inc. +# Copyright 2009-present MongoDB, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,37 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Clean up script for build artifacts. +"""Remove built C extensions from the source tree. -Only really intended to be used by internal build scripts. +Used as cibuildwheel's before-build hook so artifacts from one Python +version do not leak into the wheel built for the next. """ from __future__ import annotations -import sys from pathlib import Path -try: - Path("pymongo/_cmessage.so").unlink() - Path("bson/_cbson.so").unlink() -except BaseException: # noqa: S110 - pass - -try: - Path("pymongo/_cmessage.pyd").unlink() - Path("bson/_cbson.pyd").unlink() -except BaseException: # noqa: S110 - pass - -try: - from pymongo import _cmessage # type: ignore[attr-defined] # noqa: F401 - - sys.exit("could still import _cmessage") -except ImportError: - pass - -try: - from bson import _cbson # type: ignore[attr-defined] # noqa: F401 - - sys.exit("could still import _cbson") -except ImportError: - pass +for pkg in ("bson", "pymongo"): + for pattern in ("*.so", "*.pyd"): + for path in Path(pkg).glob(pattern): + path.unlink() + print(f"removed {path}")