rustpython_vm/builtins/
generator.rs

1/*
2 * The mythical generator.
3 */
4
5use super::{PyCode, PyStrRef, PyType};
6use crate::{
7    class::PyClassImpl,
8    coroutine::Coro,
9    frame::FrameRef,
10    function::OptionalArg,
11    protocol::PyIterReturn,
12    types::{IterNext, Iterable, Representable, SelfIter, Unconstructible},
13    AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
14};
15
16#[pyclass(module = false, name = "generator")]
17#[derive(Debug)]
18pub struct PyGenerator {
19    inner: Coro,
20}
21
22impl PyPayload for PyGenerator {
23    fn class(ctx: &Context) -> &'static Py<PyType> {
24        ctx.types.generator_type
25    }
26}
27
28#[pyclass(with(Py, Unconstructible, IterNext, Iterable))]
29impl PyGenerator {
30    pub fn as_coro(&self) -> &Coro {
31        &self.inner
32    }
33
34    pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
35        PyGenerator {
36            inner: Coro::new(frame, name),
37        }
38    }
39
40    #[pygetset(magic)]
41    fn name(&self) -> PyStrRef {
42        self.inner.name()
43    }
44
45    #[pygetset(magic, setter)]
46    fn set_name(&self, name: PyStrRef) {
47        self.inner.set_name(name)
48    }
49
50    #[pygetset]
51    fn gi_frame(&self, _vm: &VirtualMachine) -> FrameRef {
52        self.inner.frame()
53    }
54    #[pygetset]
55    fn gi_running(&self, _vm: &VirtualMachine) -> bool {
56        self.inner.running()
57    }
58    #[pygetset]
59    fn gi_code(&self, _vm: &VirtualMachine) -> PyRef<PyCode> {
60        self.inner.frame().code.clone()
61    }
62    #[pygetset]
63    fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
64        self.inner.frame().yield_from_target()
65    }
66}
67
68#[pyclass]
69impl Py<PyGenerator> {
70    #[pymethod]
71    fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
72        self.inner.send(self.as_object(), value, vm)
73    }
74
75    #[pymethod]
76    fn throw(
77        &self,
78        exc_type: PyObjectRef,
79        exc_val: OptionalArg,
80        exc_tb: OptionalArg,
81        vm: &VirtualMachine,
82    ) -> PyResult<PyIterReturn> {
83        self.inner.throw(
84            self.as_object(),
85            exc_type,
86            exc_val.unwrap_or_none(vm),
87            exc_tb.unwrap_or_none(vm),
88            vm,
89        )
90    }
91
92    #[pymethod]
93    fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
94        self.inner.close(self.as_object(), vm)
95    }
96}
97
98impl Unconstructible for PyGenerator {}
99
100impl Representable for PyGenerator {
101    #[inline]
102    fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
103        Ok(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm))
104    }
105}
106
107impl SelfIter for PyGenerator {}
108impl IterNext for PyGenerator {
109    fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
110        zelf.send(vm.ctx.none(), vm)
111    }
112}
113
114pub fn init(ctx: &Context) {
115    PyGenerator::extend_class(ctx, ctx.types.generator_type);
116}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.