mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #2950 from fanninpm/contextvars
Add contextvars from CPython 3.8
This commit is contained in:
4
Lib/contextvars.py
vendored
Normal file
4
Lib/contextvars.py
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
from _contextvars import Context, ContextVar, Token, copy_context
|
||||
|
||||
|
||||
__all__ = ('Context', 'ContextVar', 'Token', 'copy_context')
|
||||
1110
Lib/test/test_context.py
vendored
Normal file
1110
Lib/test/test_context.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
191
stdlib/src/contextvars.rs
Normal file
191
stdlib/src/contextvars.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
pub(crate) use _contextvars::make_module;
|
||||
|
||||
#[pymodule]
|
||||
mod _contextvars {
|
||||
use rustpython_vm::builtins::{PyFunction, PyStrRef, PyTypeRef};
|
||||
use rustpython_vm::function::{ArgCallable, FuncArgs, OptionalArg};
|
||||
use rustpython_vm::{PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine};
|
||||
|
||||
#[pyattr]
|
||||
#[pyclass(name)]
|
||||
#[derive(Debug, PyValue)]
|
||||
struct Context {}
|
||||
|
||||
#[pyimpl]
|
||||
impl Context {
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, _vm: &VirtualMachine) -> PyResult<()> {
|
||||
unimplemented!("Context.__init__ is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn run(
|
||||
&self,
|
||||
_callable: ArgCallable,
|
||||
_args: FuncArgs,
|
||||
_vm: &VirtualMachine,
|
||||
) -> PyResult<PyFunction> {
|
||||
unimplemented!("Context.run is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn copy(&self, _vm: &VirtualMachine) -> PyResult<Context> {
|
||||
unimplemented!("Context.copy is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn getitem(&self, _var: PyObjectRef) -> PyResult<PyObjectRef> {
|
||||
unimplemented!("Context.__getitem__ is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, _var: PyObjectRef) -> PyResult<bool> {
|
||||
unimplemented!("Context.__contains__ is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
unimplemented!("Context.__len__ is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn iter(&self) -> PyResult {
|
||||
unimplemented!("Context.__iter__ is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn get(
|
||||
&self,
|
||||
_key: PyObjectRef,
|
||||
_default: OptionalArg<PyObjectRef>,
|
||||
) -> PyResult<PyObjectRef> {
|
||||
unimplemented!("Context.get is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn keys(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> Vec<PyObjectRef> {
|
||||
unimplemented!("Context.keys is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn values(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> Vec<PyObjectRef> {
|
||||
unimplemented!("Context.values is currently under construction")
|
||||
}
|
||||
}
|
||||
|
||||
#[pyattr]
|
||||
#[pyclass(name)]
|
||||
#[derive(Debug, PyValue)]
|
||||
struct ContextVar {
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
name: String,
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
default: Option<PyObjectRef>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
struct ContextVarOptions {
|
||||
#[pyarg(positional)]
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
name: PyStrRef,
|
||||
#[pyarg(any, optional)]
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
default: OptionalArg<PyObjectRef>,
|
||||
}
|
||||
|
||||
#[pyimpl]
|
||||
impl ContextVar {
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, _args: ContextVarOptions, _vm: &VirtualMachine) -> PyResult<()> {
|
||||
unimplemented!("ContextVar.__init__() is currently under construction")
|
||||
}
|
||||
|
||||
#[pyproperty]
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn get(
|
||||
&self,
|
||||
_default: OptionalArg<PyObjectRef>,
|
||||
_vm: &VirtualMachine,
|
||||
) -> PyResult<PyObjectRef> {
|
||||
unimplemented!("ContextVar.get() is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn set(&self, _value: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> {
|
||||
unimplemented!("ContextVar.set() is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn reset(
|
||||
_zelf: PyRef<Self>,
|
||||
_token: PyRef<ContextToken>,
|
||||
_vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
unimplemented!("ContextVar.reset() is currently under construction")
|
||||
}
|
||||
|
||||
#[pyclassmethod(magic)]
|
||||
fn class_getitem(_cls: PyTypeRef, _key: PyStrRef, _vm: &VirtualMachine) -> PyResult<()> {
|
||||
unimplemented!("ContextVar.__class_getitem__() is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn repr(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> String {
|
||||
unimplemented!("<ContextVar name={{}} default={{}} at {{}}")
|
||||
// format!(
|
||||
// "<ContextVar name={} default={:?} at {:#x}>",
|
||||
// zelf.name.as_str(),
|
||||
// zelf.default.map_or("", |x| PyStr::from(*x).as_str()),
|
||||
// zelf.get_id()
|
||||
// )
|
||||
}
|
||||
}
|
||||
|
||||
#[pyattr]
|
||||
#[pyclass(name = "Token")]
|
||||
#[derive(Debug, PyValue)]
|
||||
struct ContextToken {}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
struct ContextTokenOptions {
|
||||
#[pyarg(positional)]
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
context: PyObjectRef,
|
||||
#[pyarg(positional)]
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
var: PyObjectRef,
|
||||
#[pyarg(positional)]
|
||||
#[allow(dead_code)] // TODO: RUSTPYTHON
|
||||
old_value: PyObjectRef,
|
||||
}
|
||||
|
||||
#[pyimpl]
|
||||
impl ContextToken {
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, _args: ContextTokenOptions, _vm: &VirtualMachine) -> PyResult<()> {
|
||||
unimplemented!("Token.__init__() is currently under construction")
|
||||
}
|
||||
|
||||
#[pyproperty]
|
||||
fn var(&self, _vm: &VirtualMachine) -> PyObjectRef {
|
||||
unimplemented!("Token.var() is currently under construction")
|
||||
}
|
||||
|
||||
#[pyproperty]
|
||||
fn old_value(&self, _vm: &VirtualMachine) -> PyObjectRef {
|
||||
unimplemented!("Token.old_value() is currently under construction")
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn repr(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> String {
|
||||
unimplemented!("<Token {{}}var={{}} at {{}}>")
|
||||
}
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn copy_context() {}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod array;
|
||||
mod binascii;
|
||||
mod bisect;
|
||||
mod cmath;
|
||||
mod contextvars;
|
||||
mod csv;
|
||||
mod dis;
|
||||
mod gc;
|
||||
@@ -78,6 +79,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
|
||||
"binascii" => binascii::make_module,
|
||||
"_bisect" => bisect::make_module,
|
||||
"cmath" => cmath::make_module,
|
||||
"_contextvars" => contextvars::make_module,
|
||||
"_csv" => csv::make_module,
|
||||
"_dis" => dis::make_module,
|
||||
"gc" => gc::make_module,
|
||||
|
||||
Reference in New Issue
Block a user