Skip to content

Commit a0a1888

Browse files
committed
Draft methods and fields for ContextVar
Things are going to get worse before they get better.
1 parent 7ce7c95 commit a0a1888

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

stdlib/src/contextvars.rs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,91 @@ pub(crate) use _contextvars::make_module;
22

33
#[pymodule]
44
mod _contextvars {
5+
use rustpython_vm::builtins::{PyStrRef, PyTypeRef};
6+
use rustpython_vm::function::OptionalArg;
7+
use rustpython_vm::{PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine};
8+
59
#[pyattr]
6-
#[pyclass(name = "Context")]
7-
#[derive(Debug, Default)]
10+
#[pyclass(name)]
11+
#[derive(Debug, PyValue)]
812
struct Context {}
9-
13+
1014
#[pyimpl]
1115
impl Context {}
12-
16+
1317
#[pyattr]
14-
#[pyclass(name = "ContextVar")]
15-
#[derive(Debug, Default)]
16-
struct ContextVar {}
18+
#[pyclass(name)]
19+
#[derive(Debug, PyValue)]
20+
struct ContextVar {
21+
#[allow(dead_code)] // TODO: RUSTPYTHON
22+
name: String,
23+
#[allow(dead_code)] // TODO: RUSTPYTHON
24+
default: Option<PyObjectRef>,
25+
}
26+
27+
#[derive(FromArgs)]
28+
struct ContextVarOptions {
29+
#[pyarg(positional)]
30+
name: PyStrRef,
31+
#[pyarg(any, optional)]
32+
default: OptionalArg<PyObjectRef>,
33+
}
1734

1835
#[pyimpl]
19-
impl ContextVar {}
36+
impl ContextVar {
37+
#[pymethod(magic)]
38+
fn init(&self, _args: ContextVarOptions, _vm: &VirtualMachine) -> PyResult<()> {
39+
unimplemented!("ContextVar.__init__() is currently under construction")
40+
}
41+
42+
#[pyproperty]
43+
fn name(&self) -> String {
44+
self.name.clone()
45+
}
46+
47+
#[pymethod]
48+
fn get(
49+
&self,
50+
_default: OptionalArg<PyObjectRef>,
51+
_vm: &VirtualMachine,
52+
) -> PyResult<PyObjectRef> {
53+
unimplemented!("ContextVar.get() is currently under construction")
54+
}
55+
56+
#[pymethod]
57+
fn set(&self, _value: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> {
58+
unimplemented!("ContextVar.set() is currently under construction")
59+
}
60+
61+
#[pymethod]
62+
fn reset(
63+
_zelf: PyRef<Self>,
64+
_token: PyRef<ContextToken>,
65+
_vm: &VirtualMachine,
66+
) -> PyResult<()> {
67+
unimplemented!("ContextVar.reset() is currently under construction")
68+
}
69+
70+
#[pyclassmethod(magic)]
71+
fn class_getitem(_cls: PyTypeRef, _key: PyStrRef, _vm: &VirtualMachine) -> PyResult<()> {
72+
unimplemented!("ContextVar.__class_getitem__() is currently under construction")
73+
}
74+
75+
#[pymethod(magic)]
76+
fn repr(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> String {
77+
unimplemented!("<ContextVar name={{}} default={{}} at {{}}")
78+
// format!(
79+
// "<ContextVar name={} default={:?} at {:#x}>",
80+
// zelf.name.as_str(),
81+
// zelf.default.map_or("", |x| PyStr::from(*x).as_str()),
82+
// zelf.get_id()
83+
// )
84+
}
85+
}
2086

2187
#[pyattr]
2288
#[pyclass(name = "Token")]
23-
#[derive(Debug, Default)]
89+
#[derive(Debug, PyValue)]
2490
struct ContextToken {}
2591

2692
#[pyimpl]

0 commit comments

Comments
 (0)