Skip to content

checksharedmods uses the wrong Python interpreter in native builds with --with-build-python #162308

Description

@superajun-wsj

Bug report

Description

[PR #161530](#161530) proposes setting _PYTHON_HOST_PLATFORM only during cross-compilation.

Previously, when --with-build-python was specified, _PYTHON_HOST_PLATFORM was set even for native builds (to an empty string). This caused incorrect platform information, as described in [#140266](#140266).

It also caused Tools/build/check_extension_modules.py to treat native builds as cross-compilation:

default="_PYTHON_HOST_PLATFORM" in os.environ

Consequently, it used check_module_cross() instead of check_module_import(), skipping actual extension module imports.

PR #161530 corrects this behavior, but also exposes another issue in Makefile.pre.in:

checksharedmods: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON)
    @$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py

In a native build, PYTHON_FOR_BUILD points to the external interpreter when --with-build-python is specified, rather than the newly built ./python.

This can cause incorrect extension module validation if the two interpreters have different module configurations.

Example

Consider a native build where:

  • The external build Python has _bisect and _bz2 as shared extensions.
  • The target CPython builds these modules as built-ins.

When check_module_import() runs under the external build Python, it fails with:

[ERROR] _bisect failed to import: '_bisect' is not a built-in module
[ERROR] Cannot mark builtin module '_bisect' as failed!
[ERROR] _bz2 failed to import: '_bz2' is not a built-in module
[ERROR] Cannot mark builtin module '_bz2' as failed!

These errors reflect the external build Python's module configuration, not necessarily a problem with the target CPython.

Expected behavior

For native builds, checksharedmods should use the newly built target interpreter (./$(BUILDPYTHON)) to validate extension modules, regardless of whether --with-build-python is specified.

For cross-compilation, it should continue using the external build Python with cross-compilation checks.

Would it make sense to adjust the checksharedmods recipe to select the interpreter based on whether the build is cross-compiling, rather than always using PYTHON_FOR_BUILD?

CPython versions tested on:

3.9 ~ 3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. itamaro commented on Nov 21, 2026

    @itamaro
    Contributor

    I agree this is worth fixing before gh-161530 lands. That PR would expose the issue by making checksharedmods try to import the target's extensions with the external build Python.

    Using the newly built Python for checksharedmods in native builds sounds reasonable to me. I haven't reviewed gh-162337 closely yet.

    @superajun-wsj, I noticed you closed gh-162311. Did you run into anything we should take into account when reviewing gh-162337, or were you planning a follow-up?

  2. superajun-wsj commented on Nov 21, 2026

    @superajun-wsj
    Author

    @itamaro Thank you for your reply. We will keep an eye on this issue. #162311 was closed because we need to complete the CLA process first. Of course, we warmly welcome patches from other contributors that can resolve this issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildThe build process and cross-buildtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions