Skip to content

feat(ffi): add Python 3.14 context watcher API#6204

Merged
davidhewitt merged 5 commits into
PyO3:mainfrom
florentinl:agent/python-314-context-watcher
Jul 22, 2026
Merged

feat(ffi): add Python 3.14 context watcher API#6204
davidhewitt merged 5 commits into
PyO3:mainfrom
florentinl:agent/python-314-context-watcher

Conversation

@florentinl

@florentinl florentinl commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Python 3.14 added a context watcher API for observing contextvars.Context switches.

This exposes the raw declarations in pyo3-ffi.

Relevant links:

@florentinl
florentinl force-pushed the agent/python-314-context-watcher branch from 6535a6a to 5c25a54 Compare July 15, 2026 11:40
@florentinl
florentinl marked this pull request as ready for review July 15, 2026 12:07
@bschoenmaeckers

Copy link
Copy Markdown
Member

This file should be moved to the cpython folder. To mirror upstream.

@florentinl

florentinl commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

This makes sense to me. Should i do it in this PR ? Or move it in a prior PR and then add the watcher API ?

@bschoenmaeckers

bschoenmaeckers commented Jul 15, 2026

Copy link
Copy Markdown
Member

This makes sense to me. Should i do it in this PR ? Or move it in a prior PR and then add the watcher API ?

Yes please, you can do it here.

@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: ed420e5f6f

ℹ️ 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".

Comment thread pyo3-ffi/src/cpython/mod.rs
@florentinl

florentinl commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

The RustPython bindings still need to stay outside of the cpython module which is marked with #[cfg(not(any(Py_LIMITED_API, RustPython)))]. So I kept the previous context.rs just for RustPython for this reason.

Is this the correct approach in these cases ?

@bschoenmaeckers

Copy link
Copy Markdown
Member

The RustPython bindings still need to stay outside of the cpython module which is marked with #[cfg(not(any(Py_LIMITED_API, RustPython)))]. So I kept the previous context.rs just for RustPython for this reason.

Is this the correct approach in these cases ?

RustPython only supports abi3/abi3t so these functions are not exposed. So you can remove all RustPython cfg attributes.

Comment thread pyo3-ffi/src/lib.rs Outdated
@florentinl
florentinl force-pushed the agent/python-314-context-watcher branch from da0c232 to 454b758 Compare July 15, 2026 15:23
@bschoenmaeckers

Copy link
Copy Markdown
Member

Thanks! LGTM

@florentinl

Copy link
Copy Markdown
Contributor Author

As I don't have write access, please merge it when it makes sense. Thanks !

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, looks correct to me with two small suggestions

Comment thread pyo3-ffi/src/cpython/context.rs Outdated
Comment on lines +8 to +17
// Use the C enum's integer representation to permit future event values.
#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
pub type PyContextEvent = c_uint;

#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
pub const Py_CONTEXT_SWITCHED: PyContextEvent = 1;

#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
pub type PyContext_WatchCallback =
unsafe extern "C" fn(event: PyContextEvent, obj: *mut PyObject) -> c_int;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be great to have these definitions match the ordering in the 3.15 header please (i.e. they should come after PyContext_Exit)

@florentinl florentinl Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I reordered them in 25e6605. Thank you.

Comment thread newsfragments/6204.added.md Outdated
@@ -0,0 +1 @@
Add the Python 3.14 context watcher API to `pyo3-ffi`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be great to list these directly; e.g. "Add FFI definitions PyContext_AddWatcher and PyContext_ClearWatcher on Python 3.14+"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I changed the note in commit 25e6605. Thank you.

@florentinl
florentinl requested a review from davidhewitt July 20, 2026 09:40

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@davidhewitt
davidhewitt added this pull request to the merge queue Jul 22, 2026
Merged via the queue into PyO3:main with commit 3214cb2 Jul 22, 2026
48 of 50 checks passed
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