Description
Summary
The contextvars
module, from PEP 567, is necessary to upgrade asyncio
(and decimal
) to CPython 3.8. I started #2950 to implement contextvars
, but the PR has since stalled. (@DimitrisJim urged me to open this issue.)
Detailed Explanation and Rationale
PEP 567, implemented in CPython 3.7, introduced the contextvars
module and context variables, which are conceptually similar to thread-local variables. However, unlike thread-local variables, context variables correctly support asynchronous code. Following that implementation, the asyncio
and decimal
modules were re-written to take advantage of this new capability. As part of our endeavors to get as much of the codebase covered by the CPython test suite as possible (see #1671), and out of a desire support CPython 3.8 or later (see the README), I think it's necessary to add the test_asyncio
module to the CPython test suite. The addition of the CPython 3.8 version of test_asyncio
would benefit from upgrading the asyncio
module itself to that same version. (@coolreader18 and I have noticed that test failures were often alleviated by upgrading the relevant Python module.) However, asyncio
has a dependency — contextvars
— that we don't have yet. Accordingly, I have started a draft PR (#2950) to implement this module here, but I got stymied when I was drafting the Rust implementation because I am unfamiliar with how RustPython deals with thread safety. For context, here are the parts of the C implementation that I think are relevant:
- https://github.com/python/cpython/blob/3.8/Include/context.h
- https://github.com/python/cpython/blob/3.8/Python/context.c
- https://github.com/python/cpython/blob/3.8/Python/clinic/context.c.h
- https://github.com/python/cpython/blob/3.8/Modules/_contextvarsmodule.c
- https://github.com/python/cpython/blob/3.8/Modules/clinic/_contextvarsmodule.c.h
- https://github.com/python/cpython/blob/3.8/Include/internal/pycore_context.h
Unresolved Questions
- What is the meaning of
PyContext
(defined inpyobject.rs
), and does it have any bearing on the scope of PEP 567? - The scaffolding is present, but how should it be fleshed out?
- How would each
Context
object relate to a thread in RustPython?