Skip to content

Restore free-threaded C API import test with atomic module insertion - #9498

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:upgrade/atomic-add-module-ref-20261031
Oct 31, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:upgrade/atomic-add-module-ref-20261031

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

  • Restore CPython 3.15 test_free_threading.test_capi through scripts/update_lib, retaining its canonical class/method/assertions and 10 × 4 concurrent-call loop.
  • Make PyImport_AddModuleRef use an atomic exact-dict VM helper: return the insertion winner, conditionally replace non-modules by identity, and re-probe after a lost race. Module creation and reference destruction stay outside dict payload locks. Dict subclasses keep their callback dispatch; non-dict sys.modules support is unchanged.
  • Add an explicit, generic migrate --new-file-adaptations path for introducing reviewed import/binding adapters into a previously absent canonical file. Exact source SHA, normalized target, unique non-overlapping source blocks, protected AST, and exclusive destination creation are required. Ordinary migration defaults remain unchanged.

The sole capability adapter uses existing support.get_attribute for an absent executable C API export. It does not skip supported-but-broken implementations or alter the test body. This does not duplicate #9270's CI/Windows-export changes.

Validation

  • Host updater suite: 233 tests passed, including metadata/path/hash/block/AST/symlink/overwrite rejection coverage.
  • Ruff formatting/check and diff whitespace checks passed.
  • Linux CAPI-enabled locked/offline Cargo check and strict Clippy (-D warnings) passed, with actual VM opt-level 3 invocations.
  • Linux CAPI-enabled CLI build passed. ELF export is defined FUNC GLOBAL DEFAULT PyImport_AddModuleRef; independently bound static loader closure excludes shared libpython.
  • Canonical test: pinned CPython d220409a free-threaded reference and corrected RustPython each ran exactly one test with zero skips/failures/errors. Original 10 × 4 body was unchanged. FT GIL state stayed disabled before/after ctypes/support imports and the test, without forcing a GIL override. Owned process/thread cleanup and source/artifact snapshots were checked.
  • Actual updater generation replay matched bytes; canonical class/function AST matched pinned CPython.

Limits

One bounded canonical concurrency test is not proof of all interleavings or complete CPython C API compatibility. Non-module/subclass/generic-mapping runtime corner cases were not exercised. Generic mappings remain unsupported. Exportless builds report a capability skip, not native validation; Windows export completeness is separate (#9270).

Validated locally on Linux against main 713e14cfc78d06372c68a1804240de52a8340548. Full workspace/crate-local CAPI suites and macOS/Windows execution were not run; the native behavioral check was intentionally restricted to the unchanged canonical method.

AI assistance

OpenAI Codex assisted with source investigation, implementation, updater migration, test orchestration, and independent agent review. The validation above was actually executed in the Linux environment; this does not claim human manual verification or validation on untested platforms.

Use exact-dict conditional module insertion and non-module replacement, preserving dict-subclass mapping callbacks. Restore the canonical Python 3.15 test through an explicit generic new-file updater adaptation with source hash and protected AST checks.

Assisted-by: OpenAI Codex:model version unavailable
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[ ] test: cpython/Lib/test/test_free_threading (TODO: 1)

dependencies:

dependent tests: (no tests depend on free_threading)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@codspeed

codspeed Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:upgrade/atomic-add-module-ref-20261031 (8c48276) with main (713e14c)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone marked this pull request as ready for review October 31, 2026 23:17
@youknowone
youknowone merged commit 69ee493 into RustPython:main Oct 31, 2026
30 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T23:26:19.617786Z 8c48276 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c482764ae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".



# TODO: RUSTPYTHON; executable C API exports are optional.
_PyImport_AddModuleRef = get_attribute(ctypes.pythonapi, "PyImport_AddModuleRef")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the canonical test's missing-export failure visible

When the executable does not export PyImport_AddModuleRef, get_attribute() converts the lookup's AttributeError into SkipTest during module import, bypassing the entire concurrency test. This is a test-logic change rather than an expected-failure/TODO marker edit permitted by AGENTS.md, and it can conceal an accidental export regression in a C-API-enabled build. Preserve the canonical binding and address optional export availability in the Rust/build configuration or test-selection setup instead.

AGENTS.md reference: AGENTS.md:L277-L282

Useful? React with 👍 / 👎.

@@ -0,0 +1,48 @@
import ctypes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for builds without ctypes before loading this module

On musl builds, this unconditional import raises ModuleNotFoundError before either the symbol probe or the threading decorator can run: Lib/ctypes/__init__.py imports _ctypes, but the VM explicitly excludes its registration on musl in crates/vm/src/stdlib/mod.rs:119-129. The free-threading package remains discoverable because RustPython advertises Py_GIL_DISABLED = 1, so restoring this file adds a test-collection error on that supported target. Dependency availability needs to be accounted for before loading this module, not only when looking up the C API symbol.

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants