Skip to content

Commit 7172426

Browse files
committed
clean up generator
1 parent 29055ab commit 7172426

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

vm/src/builtins/generator.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl PyPayload for PyGenerator {
2525
}
2626
}
2727

28-
#[pyclass(with(Constructor, IterNext))]
28+
#[pyclass(with(Py, Constructor, IterNext))]
2929
impl PyGenerator {
3030
pub fn as_coro(&self) -> &Coro {
3131
&self.inner
@@ -47,21 +47,41 @@ impl PyGenerator {
4747
self.inner.set_name(name)
4848
}
4949

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]
69+
impl Py<PyGenerator> {
5070
#[pymethod]
51-
fn send(zelf: PyRef<Self>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
52-
zelf.inner.send(zelf.as_object(), value, vm)
71+
fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
72+
self.inner.send(self.as_object(), value, vm)
5373
}
5474

5575
#[pymethod]
5676
fn throw(
57-
zelf: PyRef<Self>,
77+
&self,
5878
exc_type: PyObjectRef,
5979
exc_val: OptionalArg,
6080
exc_tb: OptionalArg,
6181
vm: &VirtualMachine,
6282
) -> PyResult<PyIterReturn> {
63-
zelf.inner.throw(
64-
zelf.as_object(),
83+
self.inner.throw(
84+
self.as_object(),
6585
exc_type,
6686
exc_val.unwrap_or_none(vm),
6787
exc_tb.unwrap_or_none(vm),
@@ -70,25 +90,8 @@ impl PyGenerator {
7090
}
7191

7292
#[pymethod]
73-
fn close(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {
74-
zelf.inner.close(zelf.as_object(), vm)
75-
}
76-
77-
#[pygetset]
78-
fn gi_frame(&self, _vm: &VirtualMachine) -> FrameRef {
79-
self.inner.frame()
80-
}
81-
#[pygetset]
82-
fn gi_running(&self, _vm: &VirtualMachine) -> bool {
83-
self.inner.running()
84-
}
85-
#[pygetset]
86-
fn gi_code(&self, _vm: &VirtualMachine) -> PyRef<PyCode> {
87-
self.inner.frame().code.clone()
88-
}
89-
#[pygetset]
90-
fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
91-
self.inner.frame().yield_from_target()
93+
fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
94+
self.inner.close(self.as_object(), vm)
9295
}
9396
}
9497

@@ -104,7 +107,7 @@ impl Representable for PyGenerator {
104107
impl IterNextIterable for PyGenerator {}
105108
impl IterNext for PyGenerator {
106109
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
107-
Self::send(zelf.to_owned(), vm.ctx.none(), vm)
110+
zelf.send(vm.ctx.none(), vm)
108111
}
109112
}
110113

0 commit comments

Comments
 (0)