Enable free-threaded CPython support #262
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Declares the C extension as GIL-optional for CPython 3.13+ free-threaded builds and enables cibuildwheel to build free-threaded wheels.
Changes
C Extension (
src/numpy_quaternion.c)PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED)call in module initializationPy_GIL_DISABLEDis defined (free-threaded builds)CI Workflow (
.github/workflows/build.yml)CIBW_ENABLE: "cpython-freethreading"to enable building 't' ABI wheels for Python 3.13+Thread Safety Considerations
This declares the extension GIL-optional but does not guarantee thread safety. Areas requiring review:
pyquaternion_get_vecandpyquaternion_get_componentsreturn NumPy array views directly intoPyQuaternionstructsOriginal prompt
Goal
Make the moble/quaternion C extension declare itself safe for free-threaded CPython builds and enable cibuildwheel to build free-threading wheels. This requires a small guarded change to the module initialization in src/numpy_quaternion.c and a CI update so cibuildwheel builds the free-threading 't' wheels.
Background
Required changes (detailed and actionable)
Additional notes and follow-up recommendations
The guarded call only tells the interpreter the extension claims it doesn't need the GIL. You must still ensure the code is thread-safe. I inspected the repository and identified patterns that need careful review:
CI: After this PR, run CI with an actual free-threaded Python (cibuildwheel will build free-threaded wheels on supported images when CIBW_ENABLE is set) and run tests, especially multi-threaded stress tests and scenarios that create NumPy views into PyQuaternion instances from different threads.
What I'll include in the PR
Files to change
Testing to run after merging
This pull request was created as a result of the following prompt from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.