Repository navigation
Expand file tree
/
Copy pathgenerator.rs
More file actions
134 lines (114 loc) · 3.26 KB
/
Copy pathgenerator.rs
File metadata and controls
134 lines (114 loc) · 3.26 KB
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
/*
* The mythical generator.
*/
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
coroutine::{Coro, warn_deprecated_throw_signature},
frame::FrameRef,
function::OptionalArg,
protocol::PyIterReturn,
types::{IterNext, Iterable, Representable, SelfIter},
};
#[pyclass(module = false, name = "generator")]
#[derive(Debug)]
pub struct PyGenerator {
inner: Coro,
}
impl PyPayload for PyGenerator {
#[inline]
fn class(ctx: &Context) -> &'static Py<PyType> {
ctx.types.generator_type
}
}
#[pyclass(flags(DISALLOW_INSTANTIATION), with(Py, IterNext, Iterable))]
impl PyGenerator {
pub const fn as_coro(&self) -> &Coro {
&self.inner
}
pub fn new(frame: FrameRef, name: PyStrRef, qualname: PyStrRef) -> Self {
Self {
inner: Coro::new(frame, name, qualname),
}
}
#[pygetset]
fn __name__(&self) -> PyStrRef {
self.inner.name()
}
#[pygetset(setter)]
fn set___name__(&self, name: PyStrRef) {
self.inner.set_name(name)
}
#[pygetset]
fn __qualname__(&self) -> PyStrRef {
self.inner.qualname()
}
#[pygetset(setter)]
fn set___qualname__(&self, qualname: PyStrRef) {
self.inner.set_qualname(qualname)
}
#[pygetset]
fn gi_frame(&self, _vm: &VirtualMachine) -> FrameRef {
self.inner.frame()
}
#[pygetset]
fn gi_running(&self, _vm: &VirtualMachine) -> bool {
self.inner.running()
}
#[pygetset]
fn gi_code(&self, _vm: &VirtualMachine) -> PyRef<PyCode> {
self.inner.frame().code.clone()
}
#[pygetset]
fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
self.inner.frame().yield_from_target()
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyclass]
impl Py<PyGenerator> {
#[pymethod]
fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
self.inner.send(self.as_object(), value, vm)
}
#[pymethod]
fn throw(
&self,
exc_type: PyObjectRef,
exc_val: OptionalArg,
exc_tb: OptionalArg,
vm: &VirtualMachine,
) -> PyResult<PyIterReturn> {
warn_deprecated_throw_signature(&exc_val, &exc_tb, vm)?;
self.inner.throw(
self.as_object(),
exc_type,
exc_val.unwrap_or_none(vm),
exc_tb.unwrap_or_none(vm),
vm,
)
}
#[pymethod]
fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
self.inner.close(self.as_object(), vm)
}
}
impl Representable for PyGenerator {
#[inline]
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
Ok(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm))
}
}
impl SelfIter for PyGenerator {}
impl IterNext for PyGenerator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.send(vm.ctx.none(), vm)
}
}
pub fn init(ctx: &Context) {
PyGenerator::extend_class(ctx, ctx.types.generator_type);
}