Skip to content

Add contextvars types #5022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add contextvars types #5022

wants to merge 2 commits into from

Conversation

mailmindlin
Copy link

Added rust types for Python context variables (Context, ContextVar, Token)

Copy link
Member

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Thanks!

  • Can you add an extra line to the new files? (it's a linux thing..)
  • Can you add a compelling doc example? What would you use this for?
  • It seems nice to have a scoped api for this, so that users don't need to be calling enter and exit, and a scoped api can also call exit if the users code panics in between calls to enter and exit.


impl PyContextVar {
/// Create new ContextVar with no default
pub fn new<'py>(py: Python<'py>, name: &'static CStr) -> PyResult<Bound<'py, PyContextVar>> {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need the 'static lifetime:

Suggested change
pub fn new<'py>(py: Python<'py>, name: &'static CStr) -> PyResult<Bound<'py, PyContextVar>> {
pub fn new<'py>(py: Python<'py>, name: &CStr) -> PyResult<Bound<'py, PyContextVar>> {

Copy link
Author

Choose a reason for hiding this comment

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

This doesn't need the 'static lifetime:

That would be both reasonable and convenient, but I can't find the required lifetime on the cpython docs so I figured 'static was safe.

Copy link
Member

Choose a reason for hiding this comment

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

The string immediately gets converted into a python string so a 'static lifetime is not needed.

https://github.com/python/cpython/blob/bab1398a47f6d0cfc1be70497f306874c749ef7c/Python/context.c#L260-L270

Copy link
Member

Choose a reason for hiding this comment

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

You should not read the cpython source code and use it to make conclusions about what requirements need to be upheld or not. That's implementation details, after all.

However, if a 'static lifetime is necessary the docs will mention it. For example at PyMemberDef.

Comment on lines +117 to +121
if r == 0 {
Ok(())
} else {
Err(PyErr::fetch(self.py()))
}
Copy link
Member

Choose a reason for hiding this comment

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

You can use error_on_minusone for this (same for the other instances).


impl<'py> PyContextVarMethods<'py> for Bound<'py, PyContextVar> {
fn name(&self) -> Bound<'py, PyString> {
self.getattr("name")
Copy link
Member

Choose a reason for hiding this comment

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

You can use https://docs.rs/pyo3/latest/pyo3/macro.intern.html for string literals like these.

@juntyr
Copy link
Contributor

juntyr commented Mar 31, 2025

A scoped API (and/or a guard API that exits on drop) would be really useful

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.

4 participants