Skip to content

update PySet_GET_SIZE and similar for free-threaded Python#6230

Merged
davidhewitt merged 3 commits into
PyO3:mainfrom
davidhewitt:atomic-load-macros
Jul 24, 2026
Merged

update PySet_GET_SIZE and similar for free-threaded Python#6230
davidhewitt merged 3 commits into
PyO3:mainfrom
davidhewitt:atomic-load-macros

Conversation

@davidhewitt

Copy link
Copy Markdown
Member

I got a ping from a Codex security scan after #6226 that the FFI definitions were not implemented properly for free-threaded Python.

I tried to make the shape of the corrected implementations match CPython as much as possible; this required adding some atomic helpers and various _CAST functions.

@davidhewitt davidhewitt mentioned this pull request Jul 24, 2026
10 tasks
@codspeed-hq

codspeed-hq Bot commented Jul 24, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.24%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 139 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
list_nth 8 µs 8.9 µs -10.24%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing davidhewitt:atomic-load-macros (b6e338c) with main (c85bc26)

Open in CodSpeed

@davidhewitt
davidhewitt enabled auto-merge July 24, 2026 14:42
@davidhewitt

Copy link
Copy Markdown
Member Author

CI failure was caused by the atomic function being only available under the version-specific API, setobject.rs needed to be split to have the cpython/setobject.rs portion (longstanding tech debt).

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a couple nits but otherwise this does match CPython.

#[cfg(not(any(PyPy, GraalPy)))]
pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyListObject)).ob_item.offset(i) = v;
*(*_PyList_CAST(op)).ob_item.offset(i) = v;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

return _Py_atomic_load_ssize_relaxed(&(_PyVarObject_CAST(byte_array)->ob_size));
#[cfg(not(any(PyPy, GraalPy)))]
pub unsafe fn PyByteArray_AS_STRING(op: *mut PyObject) -> *mut c_char {
(*_PyByteArray_CAST(op)).ob_start

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

preexisting issue, but, on 3.14 and older the C macro still returns a pointer to a static empty string when Py_SIZE(self) == 0, and an empty bytearray's ob_start is NULL on those versions:

https://github.com/python/cpython/blob/ffa3ba98d922765f8e3990f327662f1d4254e6bd/Include/cpython/bytearrayobject.h#L20-L27

So on ≤ 3.14 this binding can return NULL where CPython's macro never does. Only matters for raw FFI users, pyo3 doesn't use this.

@@ -0,0 +1 @@
Fix FFI definitions `PyByteArray_GET_SIZE`, `PyList_GET_SIZE`, and `PySet_GET_SIZE` to use an atomic load for free-threaded Python.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should there maybe also be a release note for PySet_MIN_SIZE being (correctly) moved out of limited API visibility?

@davidhewitt
davidhewitt added this pull request to the merge queue Jul 24, 2026
Merged via the queue into PyO3:main with commit 470b294 Jul 24, 2026
48 of 50 checks passed
@davidhewitt
davidhewitt deleted the atomic-load-macros branch July 24, 2026 21:22
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