-
Notifications
You must be signed in to change notification settings - Fork 834
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
base: main
Are you sure you want to change the base?
Add contextvars types #5022
Conversation
There was a problem hiding this 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>> { |
There was a problem hiding this comment.
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:
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>> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
if r == 0 { | ||
Ok(()) | ||
} else { | ||
Err(PyErr::fetch(self.py())) | ||
} |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
A scoped API (and/or a guard API that exits on drop) would be really useful |
Added rust types for Python context variables (
Context
,ContextVar
,Token
)