rustpython-stdlib 0.6.0

RustPython standard libraries in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
pub(crate) use _contextvars::PyContext;
pub(crate) use _contextvars::module_def;

#[pymodule]
mod _contextvars {
    use crate::vm::{
        AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
        atomic_func,
        builtins::{PyGenericAlias, PyList, PyStr, PyStrRef, PyType, PyTypeRef},
        class::StaticType,
        class_or_notimplemented,
        common::{
            hash::PyHash,
            lock::{LazyLock, PyMutex},
            wtf8::Wtf8Buf,
        },
        function::{FuncArgs, OptionalArg, PyComparisonValue},
        protocol::{PyMappingMethods, PySequenceMethods},
        types::{
            AsMapping, AsSequence, Comparable, Constructor, Hashable, Iterable, PyComparisonOp,
            Representable,
        },
    };
    use core::sync::atomic::{AtomicBool, AtomicI64, AtomicUsize, Ordering};
    use indexmap::IndexMap;

    // TODO: Real hamt implementation
    type Hamt = IndexMap<PyRef<ContextVar>, PyObjectRef, rapidhash::quality::RandomState>;

    #[pyclass(no_attr, name = "Hamt", module = "contextvars")]
    #[derive(Debug, PyPayload)]
    pub(crate) struct HamtObject {
        hamt: PyMutex<Hamt>,
    }

    #[pyclass]
    impl HamtObject {}

    impl Default for HamtObject {
        fn default() -> Self {
            Self {
                hamt: PyMutex::new(Hamt::default()),
            }
        }
    }

    #[derive(Debug)]
    struct ContextInner {
        idx: AtomicUsize,
        vars: PyRef<HamtObject>,
        // PyObject *ctx_weakreflist;
        entered: AtomicBool,
    }

    #[pyattr]
    #[pyclass(name = "Context", unhashable = true)]
    #[derive(Debug, PyPayload)]
    pub(crate) struct PyContext {
        // not to confuse with vm::Context
        inner: ContextInner,
    }

    #[derive(FromArgs)]
    struct ContextGetArgs {
        #[pyarg(positional)]
        key: PyObjectRef,
        #[pyarg(positional, optional)]
        default: Option<PyObjectRef>,
    }

    impl PyContext {
        fn empty(vm: &VirtualMachine) -> Self {
            Self {
                inner: ContextInner {
                    idx: AtomicUsize::new(usize::MAX),
                    vars: HamtObject::default().into_ref(&vm.ctx),
                    entered: AtomicBool::new(false),
                },
            }
        }

        fn borrow_vars(&self) -> impl core::ops::DerefMut<Target = Hamt> + '_ {
            self.inner.vars.hamt.lock()
        }

        fn borrow_vars_mut(&self) -> impl core::ops::DerefMut<Target = Hamt> + '_ {
            self.inner.vars.hamt.lock()
        }

        pub(crate) fn enter(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
            // A context is entered by one thread at a time, so the check and the
            // claim have to be a single step.
            if zelf
                .inner
                .entered
                .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
                .is_err()
            {
                return Err(vm.new_runtime_error(format!(
                    "cannot enter context: {} is already entered",
                    zelf.as_object().repr(vm)?
                )));
            }

            let mut ctxs = vm.context_stack.borrow_mut();
            zelf.inner.idx.store(ctxs.len(), Ordering::Relaxed);
            ctxs.push(zelf.to_owned().into());

            Ok(())
        }

        pub(crate) fn exit(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
            if !zelf.inner.entered.load(Ordering::Acquire) {
                return Err(vm.new_runtime_error(format!(
                    "cannot exit context: {} is not entered",
                    zelf.as_object().repr(vm)?
                )));
            }

            let mut ctxs = vm.context_stack.borrow_mut();
            ctxs.pop_if(|ctx| ctx.is(zelf)).map(drop).ok_or_else(|| {
                vm.new_runtime_error(
                    "cannot exit context: thread state references a different context object",
                )
            })?;
            zelf.inner.entered.store(false, Ordering::Release);

            Ok(())
        }

        fn current(vm: &VirtualMachine) -> PyRef<Self> {
            let mut ctxs = vm.context_stack.borrow_mut();
            if let Some(ctx) = ctxs.last() {
                ctx.clone().downcast::<Self>().unwrap()
            } else {
                let ctx = Self::empty(vm);
                ctx.inner.idx.store(0, Ordering::Relaxed);
                ctx.inner.entered.store(true, Ordering::Release);
                let ctx = ctx.into_ref(&vm.ctx);
                ctxs.push(ctx.clone().into());
                ctx
            }
        }

        fn contains(&self, needle: &Py<ContextVar>) -> bool {
            let vars = self.borrow_vars();
            vars.get(needle).is_some()
        }

        fn get_inner(&self, needle: &Py<ContextVar>) -> Option<PyObjectRef> {
            let vars = self.borrow_vars();
            vars.get(needle).map(|o| o.to_owned())
        }
    }

    fn context_check_key_type<'a>(
        key: &'a crate::vm::PyObject,
        vm: &VirtualMachine,
    ) -> PyResult<&'a Py<ContextVar>> {
        match key.downcast_ref::<ContextVar>() {
            Some(var) => Ok(var),
            None => Err(vm.new_type_error(format!(
                "a ContextVar key was expected, got {}",
                key.repr(vm)?
            ))),
        }
    }

    #[pyclass(with(Constructor, AsMapping, AsSequence, Iterable, Comparable))]
    impl PyContext {
        #[pymethod]
        fn run(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
            let (callable, rest) = args
                .args
                .split_first()
                .ok_or_else(|| vm.new_type_error("run() missing 1 required positional argument"))?;
            let rest = FuncArgs {
                args: rest.to_vec(),
                kwargs: args.kwargs,
            };
            Self::enter(zelf, vm)?;
            let result = callable.call(rest, vm);
            Self::exit(zelf, vm)?;
            result
        }

        #[pymethod]
        fn copy(zelf: &Py<Self>, vm: &VirtualMachine) -> Self {
            // Deep copy the vars - clone the underlying Hamt data, not just the PyRef
            let vars_copy = HamtObject {
                hamt: PyMutex::new(zelf.inner.vars.hamt.lock().clone()),
            };
            Self {
                inner: ContextInner {
                    idx: AtomicUsize::new(usize::MAX),
                    vars: vars_copy.into_ref(&vm.ctx),
                    entered: AtomicBool::new(false),
                },
            }
        }

        fn __getitem__(
            &self,
            var: PyRef<ContextVar>,
            vm: &VirtualMachine,
        ) -> PyResult<PyObjectRef> {
            let item = self.borrow_vars().get(&*var).map(|item| item.to_owned());
            item.ok_or_else(|| vm.new_key_error(var.into()))
        }

        fn __len__(&self) -> usize {
            self.borrow_vars().len()
        }

        #[pymethod]
        fn get(
            zelf: &Py<Self>,
            args: ContextGetArgs,
            vm: &VirtualMachine,
        ) -> PyResult<Option<PyObjectRef>> {
            let key = context_check_key_type(&args.key, vm)?;
            let found = zelf.get_inner(key);
            if found.is_some() {
                Ok(found)
            } else {
                Ok(args.default)
            }
        }

        // TODO: wrong return type
        #[pymethod]
        fn keys(zelf: &Py<Self>) -> Vec<PyObjectRef> {
            let vars = zelf.borrow_vars();
            vars.keys().map(|key| key.to_owned().into()).collect()
        }

        // TODO: wrong return type
        #[pymethod]
        fn values(zelf: PyRef<Self>) -> Vec<PyObjectRef> {
            let vars = zelf.borrow_vars();
            vars.values().map(|value| value.to_owned()).collect()
        }

        // TODO: wrong return type
        #[pymethod]
        fn items(zelf: PyRef<Self>, vm: &VirtualMachine) -> Vec<PyObjectRef> {
            let vars = zelf.borrow_vars();
            vars.iter()
                .map(|(k, v)| vm.ctx.new_tuple(vec![k.clone().into(), v.clone()]).into())
                .collect()
        }
    }

    impl Constructor for PyContext {
        type Args = FuncArgs;

        fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
            if !args.args.is_empty() || !args.kwargs.is_empty() {
                return Err(vm.new_type_error("Context() does not accept any arguments"));
            }
            Self::empty(vm).into_ref_with_type(vm, cls).map(Into::into)
        }

        fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
            unreachable!("use slot_new")
        }
    }

    impl AsMapping for PyContext {
        fn as_mapping() -> &'static PyMappingMethods {
            static AS_MAPPING: PyMappingMethods = PyMappingMethods {
                length: atomic_func!(|mapping, _vm| Ok(
                    PyContext::mapping_downcast(mapping).__len__()
                )),
                subscript: atomic_func!(|mapping, needle, vm| {
                    let needle = context_check_key_type(needle, vm)?;
                    PyContext::mapping_downcast(mapping)
                        .get_inner(needle)
                        .ok_or_else(|| vm.new_key_error(needle.to_owned().into()))
                }),
                ass_subscript: None,
            };
            &AS_MAPPING
        }
    }

    impl AsSequence for PyContext {
        fn as_sequence() -> &'static PySequenceMethods {
            static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
                contains: atomic_func!(|seq, target, vm| {
                    let target = context_check_key_type(target, vm)?;
                    Ok(PyContext::sequence_downcast(seq).contains(target))
                }),
                ..PySequenceMethods::NOT_IMPLEMENTED
            });
            &AS_SEQUENCE
        }
    }

    impl Iterable for PyContext {
        fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
            let vars = zelf.borrow_vars();
            let keys: Vec<PyObjectRef> = vars.keys().map(|k| k.clone().into()).collect();
            let list = vm.ctx.new_list(keys);
            <PyList as Iterable>::iter(list, vm)
        }
    }

    impl Comparable for PyContext {
        fn cmp(
            zelf: &Py<Self>,
            other: &PyObject,
            op: PyComparisonOp,
            vm: &VirtualMachine,
        ) -> PyResult<PyComparisonValue> {
            let other = class_or_notimplemented!(Self, other);
            op.eq_only(|| Ok(vars_eq(zelf, other, vm)?.into()))
        }
    }

    fn vars_eq(left: &Py<PyContext>, right: &Py<PyContext>, vm: &VirtualMachine) -> PyResult<bool> {
        if left.is(right) {
            return Ok(true);
        }
        // Snapshot first: value __eq__ may re-enter Context.run / ContextVar.set.
        let pairs: Vec<(PyObjectRef, PyObjectRef)> = {
            let left_vars = left.borrow_vars();
            let right_vars = right.borrow_vars();
            if left_vars.len() != right_vars.len() {
                return Ok(false);
            }
            let mut pairs = Vec::with_capacity(left_vars.len());
            for (key, left_value) in left_vars.iter() {
                let Some(right_value) = right_vars.get(key) else {
                    return Ok(false);
                };
                pairs.push((left_value.clone(), right_value.clone()));
            }
            pairs
        };
        for (left_value, right_value) in pairs {
            if !vm.bool_eq(&left_value, &right_value)? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    #[pyattr]
    #[pyclass(name, traverse)]
    #[derive(PyPayload)]
    struct ContextVar {
        #[pymember]
        name: PyStrRef,
        default: Option<PyObjectRef>,
        #[pytraverse(skip)]
        cached: PyMutex<Option<ContextVarCache>>,
        #[pytraverse(skip)]
        cached_id: AtomicUsize, // cached_tsid in CPython
        #[pytraverse(skip)]
        hash: AtomicI64,
    }

    impl core::fmt::Debug for ContextVar {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            f.debug_struct("ContextVar").finish()
        }
    }

    impl PartialEq for ContextVar {
        fn eq(&self, other: &Self) -> bool {
            core::ptr::eq(self, other)
        }
    }
    impl Eq for ContextVar {}

    #[derive(Debug)]
    struct ContextVarCache {
        object: PyObjectRef, // value; cached in CPython
        idx: usize,          // Context index; cached_tsver in CPython
    }

    impl ContextVar {
        fn delete(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
            let cached = zelf.cached.lock().take();
            drop(cached);

            let ctx = PyContext::current(vm);

            let removed = ctx.borrow_vars_mut().swap_remove(zelf);
            let existed = removed.is_some();
            drop(removed);
            if !existed {
                // TODO:
                // PyErr_SetObject(PyExc_LookupError, (PyObject *)var);
                return Err(vm.new_lookup_error(zelf.as_object().repr(vm)?.as_wtf8().to_owned()));
            }

            Ok(())
        }

        // contextvar_set
        fn set_inner(zelf: &Py<Self>, value: PyObjectRef, vm: &VirtualMachine) {
            let ctx = PyContext::current(vm);

            let replaced = ctx.borrow_vars_mut().insert(zelf.to_owned(), value.clone());
            drop(replaced);

            // cached and cached_id are one snapshot: another thread's get()
            // must not observe a new id with an old value, or the reverse.
            let cache = ContextVarCache {
                object: value,
                idx: ctx.inner.idx.load(Ordering::Relaxed),
            };
            let mut cached = zelf.cached.lock();
            zelf.cached_id.store(ctx.get_id(), Ordering::SeqCst);
            let replaced = cached.replace(cache);
            drop(cached);
            drop(replaced);
        }

        fn generate_hash(zelf: &Py<Self>, name_hash: PyHash) -> PyHash {
            let pointer_hash = crate::common::hash::hash_pointer(zelf.as_object().get_id());
            let hash = pointer_hash ^ name_hash;
            if hash == -1 { -2 } else { hash }
        }
    }

    #[pyclass(with(Constructor, Hashable, Representable))]
    impl ContextVar {
        #[pymethod]
        fn get(
            zelf: &Py<Self>,
            default: OptionalArg<PyObjectRef>,
            vm: &VirtualMachine,
        ) -> PyResult<Option<PyObjectRef>> {
            // The replaced cache entry comes back out so that dropping it, which
            // can run a __del__ that calls back in, happens with no lock held.
            let (found, replaced) = (|| {
                let ctxs = vm.context_stack.borrow();
                let Some(ctx_obj) = ctxs.last() else {
                    return (None, None);
                };
                let ctx = ctx_obj.downcast_ref::<PyContext>().unwrap();
                let mut cached = zelf.cached.lock();
                if let Some(cached) = &*cached
                    && zelf.cached_id.load(Ordering::SeqCst) == ctx.get_id()
                    && cached.idx + 1 == ctxs.len()
                {
                    return (Some(cached.object.clone()), None);
                }
                let Some(obj) = ctx.borrow_vars().get(zelf).map(|obj| obj.to_owned()) else {
                    return (None, None);
                };
                zelf.cached_id.store(ctx.get_id(), Ordering::SeqCst);

                let replaced = cached.replace(ContextVarCache {
                    object: obj.clone(),
                    idx: ctxs.len() - 1,
                });

                (Some(obj), replaced)
            })();
            drop(replaced);

            let value = if let Some(value) = found {
                value
            } else if let Some(default) = default.into_option() {
                default
            } else if let Some(default) = zelf.default.as_ref() {
                default.clone()
            } else {
                return Err(vm.new_lookup_error(zelf.as_object().repr(vm)?.as_wtf8().to_owned()));
            };
            Ok(Some(value))
        }

        #[pymethod]
        fn set(zelf: &Py<Self>, value: PyObjectRef, vm: &VirtualMachine) -> PyRef<ContextToken> {
            let ctx = PyContext::current(vm);

            let old_value = ctx.borrow_vars().get(zelf).map(|v| v.to_owned());
            let token = ContextToken {
                ctx,
                var: zelf.to_owned(),
                old_value,
                used: false.into(),
            };

            // ctx.vars borrow must be released
            Self::set_inner(zelf, value, vm);

            token.into_ref(&vm.ctx)
        }

        #[pymethod]
        fn reset(zelf: &Py<Self>, token: PyRef<ContextToken>, vm: &VirtualMachine) -> PyResult<()> {
            if token.used.load(Ordering::Acquire) {
                return Err(vm.new_runtime_error(format!(
                    "{} has already been used once",
                    token.as_object().repr(vm)?
                )));
            }

            if !zelf.is(&token.var) {
                return Err(vm.new_value_error(format!(
                    "{} was created by a different ContextVar",
                    token.var.as_object().repr(vm)?
                )));
            }

            let ctx = PyContext::current(vm);
            if !ctx.is(&token.ctx) {
                return Err(vm.new_value_error(format!(
                    "{} was created in a different Context",
                    token.var.as_object().repr(vm)?
                )));
            }

            token.used.store(true, Ordering::Release);

            if let Some(old_value) = &token.old_value {
                Self::set_inner(zelf, old_value.clone(), vm);
            } else {
                Self::delete(zelf, vm)?;
            }
            Ok(())
        }

        #[pyclassmethod]
        fn __class_getitem__(
            cls: PyTypeRef,
            object: PyObjectRef,
            vm: &VirtualMachine,
        ) -> PyResult<PyGenericAlias> {
            PyGenericAlias::from_args(cls, object, vm)
        }
    }

    impl Constructor for ContextVar {
        type Args = FuncArgs;

        fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
            let mut args = args;
            if args.args.len() != 1 {
                return Err(vm.new_type_error(format!(
                    "ContextVar() takes exactly 1 argument ({} given)",
                    args.args.len()
                )));
            }
            let default = args.take_keyword("default");
            if let Some((name, _)) = args.kwargs.first() {
                return Err(vm.new_type_error(format!(
                    "ContextVar() got an unexpected keyword argument '{name}'"
                )));
            }

            let name = args.args.swap_remove(0);
            let name = name
                .downcast::<PyStr>()
                .map_err(|_| vm.new_type_error("context variable name must be a str"))?;
            let name_hash = name.as_object().hash(vm)?;

            let var = Self {
                name,
                default,
                cached_id: 0.into(),
                cached: PyMutex::new(None),
                hash: AtomicI64::new(0),
            };
            let py_var = var.into_ref_with_type(vm, cls)?;
            let hash = Self::generate_hash(&py_var, name_hash);
            py_var.hash.store(hash, Ordering::Relaxed);
            Ok(py_var.into())
        }

        fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
            unreachable!("use slot_new")
        }
    }

    impl core::hash::Hash for ContextVar {
        #[inline]
        fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
            self.hash.load(Ordering::Relaxed).hash(state)
        }
    }

    impl Hashable for ContextVar {
        #[inline]
        fn hash(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyHash> {
            Ok(zelf.hash.load(Ordering::Relaxed))
        }
    }

    impl Representable for ContextVar {
        fn repr_wtf8(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
            let name = zelf.name.as_object().repr(vm)?;
            let id = zelf.get_id();
            let mut result = Wtf8Buf::from("<ContextVar name=");
            result.push_wtf8(name.as_wtf8());
            if let Some(arg) = zelf.default.as_ref() {
                let default = arg.repr(vm)?;
                result.push_str(" default=");
                result.push_wtf8(default.as_wtf8());
            }
            result.push_str(&format!(" at {id:#x}>"));
            Ok(result)
        }
    }

    #[pyattr]
    #[pyclass(name = "Token")]
    #[derive(Debug, PyPayload)]
    struct ContextToken {
        ctx: PyRef<PyContext>,          // tok_ctx in CPython
        var: PyRef<ContextVar>,         // tok_var in CPython
        old_value: Option<PyObjectRef>, // tok_oldval in CPython
        used: AtomicBool,
    }

    #[pyclass(with(Constructor, Representable))]
    impl ContextToken {
        #[pygetset]
        fn var(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyRef<ContextVar> {
            zelf.var.clone()
        }

        #[pygetset]
        fn old_value(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyObjectRef {
            match &zelf.old_value {
                Some(value) => value.clone(),
                None => ContextTokenMissing::static_type().to_owned().into(),
            }
        }

        #[pyclassmethod]
        fn __class_getitem__(
            cls: PyTypeRef,
            object: PyObjectRef,
            vm: &VirtualMachine,
        ) -> PyResult<PyGenericAlias> {
            PyGenericAlias::from_args(cls, object, vm)
        }

        #[pymethod]
        fn __enter__(zelf: PyRef<Self>) -> PyRef<Self> {
            zelf
        }

        #[pymethod]
        fn __exit__(
            zelf: &Py<Self>,
            _type: PyObjectRef,
            _val: PyObjectRef,
            _tb: PyObjectRef,
            vm: &VirtualMachine,
        ) -> PyResult<()> {
            ContextVar::reset(&zelf.var, zelf.to_owned(), vm)
        }
    }

    impl Constructor for ContextToken {
        type Args = FuncArgs;

        fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
            Err(vm.new_runtime_error("Tokens can only be created by ContextVars"))
        }

        fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
            unimplemented!("use slot_new")
        }
    }

    impl Representable for ContextToken {
        #[inline]
        fn repr_wtf8(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
            let used = if zelf.used.load(Ordering::Acquire) {
                " used"
            } else {
                ""
            };
            let var = Representable::repr_wtf8(&zelf.var, vm)?;
            let ptr = zelf.as_object().get_id() as *const u8;
            let mut result = Wtf8Buf::from(format!("<Token{used} var="));
            result.push_wtf8(&var);
            result.push_str(&format!(" at {ptr:p}>"));
            Ok(result)
        }
    }

    #[pyclass(no_attr, name = "Token.MISSING")]
    #[derive(Debug, PyPayload)]
    pub(super) struct ContextTokenMissing {}

    #[pyclass(with(Representable))]
    impl ContextTokenMissing {}

    impl Representable for ContextTokenMissing {
        fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
            Ok("<Token.MISSING>".to_owned())
        }
    }

    #[pyfunction]
    fn copy_context(vm: &VirtualMachine) -> PyContext {
        PyContext::copy(&PyContext::current(vm), vm)
    }

    // Set Token.MISSING attribute
    pub(crate) fn module_exec(
        vm: &VirtualMachine,
        module: &Py<crate::vm::builtins::PyModule>,
    ) -> PyResult<()> {
        __module_exec(vm, module);

        let token_type = module.get_attr("Token", vm)?;
        token_type.set_attr("MISSING", ContextTokenMissing::static_type().to_owned(), vm)?;

        Ok(())
    }
}