Skip to content

Commit e4aea92

Browse files
committed
remove iterator::stop_iter_with_value
1 parent 198a2b3 commit e4aea92

File tree

3 files changed

+54
-50
lines changed

3 files changed

+54
-50
lines changed

stdlib/src/json.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ mod machinery;
55
mod _json {
66
use super::machinery;
77
use crate::vm::{
8-
builtins::PyBaseExceptionRef,
9-
builtins::{PyStrRef, PyTypeRef},
8+
builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef},
109
function::{FuncArgs, OptionalArg},
11-
iterator,
10+
protocol::PyIterReturn,
1211
slots::{Callable, SlotConstructor},
13-
IdProtocol, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
14-
VirtualMachine,
12+
IdProtocol, IntoPyObject, IntoPyResult, PyObjectRef, PyRef, PyResult, PyValue,
13+
TryFromObject, VirtualMachine,
1514
};
1615
use num_bigint::BigInt;
1716
use std::str::FromStr;
@@ -74,42 +73,48 @@ mod _json {
7473
idx: usize,
7574
scan_once: PyObjectRef,
7675
vm: &VirtualMachine,
77-
) -> PyResult {
78-
let c = s
79-
.chars()
80-
.next()
81-
.ok_or_else(|| iterator::stop_iter_with_value(vm.ctx.new_int(idx), vm))?;
76+
) -> PyResult<PyIterReturn> {
77+
let c = match s.chars().next() {
78+
Some(c) => c,
79+
None => return Ok(PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx)))),
80+
};
8281
let next_idx = idx + c.len_utf8();
8382
match c {
8483
'"' => {
8584
return scanstring(pystr, next_idx, OptionalArg::Present(self.strict), vm)
86-
.map(|x| x.into_pyobject(vm))
85+
.map(|x| PyIterReturn::Return(x.into_pyobject(vm)))
8786
}
8887
'{' => {
8988
// TODO: parse the object in rust
9089
let parse_obj = vm.get_attribute(self.ctx.clone(), "parse_object")?;
91-
return vm.invoke(
92-
&parse_obj,
93-
(
94-
vm.ctx
95-
.new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]),
96-
self.strict,
97-
scan_once,
98-
self.object_hook.clone(),
99-
self.object_pairs_hook.clone(),
90+
return PyIterReturn::from_pyresult(
91+
vm.invoke(
92+
&parse_obj,
93+
(
94+
vm.ctx
95+
.new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]),
96+
self.strict,
97+
scan_once,
98+
self.object_hook.clone(),
99+
self.object_pairs_hook.clone(),
100+
),
100101
),
102+
vm,
101103
);
102104
}
103105
'[' => {
104106
// TODO: parse the array in rust
105107
let parse_array = vm.get_attribute(self.ctx.clone(), "parse_array")?;
106-
return vm.invoke(
107-
&parse_array,
108-
vec![
109-
vm.ctx
110-
.new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]),
111-
scan_once,
112-
],
108+
return PyIterReturn::from_pyresult(
109+
vm.invoke(
110+
&parse_array,
111+
vec![
112+
vm.ctx
113+
.new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]),
114+
scan_once,
115+
],
116+
),
117+
vm,
113118
);
114119
}
115120
_ => {}
@@ -118,7 +123,9 @@ mod _json {
118123
macro_rules! parse_const {
119124
($s:literal, $val:expr) => {
120125
if s.starts_with($s) {
121-
return Ok(vm.ctx.new_tuple(vec![$val, vm.ctx.new_int(idx + $s.len())]));
126+
return Ok(PyIterReturn::Return(
127+
vm.ctx.new_tuple(vec![$val, vm.ctx.new_int(idx + $s.len())]),
128+
));
122129
}
123130
};
124131
}
@@ -128,16 +135,18 @@ mod _json {
128135
parse_const!("false", vm.ctx.new_bool(false));
129136

130137
if let Some((res, len)) = self.parse_number(s, vm) {
131-
return Ok(vm.ctx.new_tuple(vec![res?, vm.ctx.new_int(idx + len)]));
138+
return Ok(PyIterReturn::Return(
139+
vm.ctx.new_tuple(vec![res?, vm.ctx.new_int(idx + len)]),
140+
));
132141
}
133142

134143
macro_rules! parse_constant {
135144
($s:literal) => {
136145
if s.starts_with($s) {
137-
return Ok(vm.ctx.new_tuple(vec![
146+
return Ok(PyIterReturn::Return(vm.ctx.new_tuple(vec![
138147
vm.invoke(&self.parse_constant, ($s.to_owned(),))?,
139148
vm.ctx.new_int(idx + $s.len()),
140-
]));
149+
])));
141150
}
142151
};
143152
}
@@ -146,7 +155,7 @@ mod _json {
146155
parse_constant!("Infinity");
147156
parse_constant!("-Infinity");
148157

149-
Err(iterator::stop_iter_with_value(vm.ctx.new_int(idx), vm))
158+
Ok(PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx))))
150159
}
151160

152161
fn parse_number(&self, s: &str, vm: &VirtualMachine) -> Option<(PyResult, usize)> {
@@ -185,16 +194,19 @@ mod _json {
185194
Some((ret, buf.len()))
186195
}
187196

188-
fn call(zelf: &PyRef<Self>, pystr: PyStrRef, idx: isize, vm: &VirtualMachine) -> PyResult {
197+
fn call(
198+
zelf: &PyRef<Self>,
199+
pystr: PyStrRef,
200+
idx: isize,
201+
vm: &VirtualMachine,
202+
) -> PyResult<PyIterReturn> {
189203
if idx < 0 {
190204
return Err(vm.new_value_error("idx cannot be negative".to_owned()));
191205
}
192206
let idx = idx as usize;
193207
let mut chars = pystr.as_str().chars();
194-
if idx > 0 {
195-
chars
196-
.nth(idx - 1)
197-
.ok_or_else(|| iterator::stop_iter_with_value(vm.ctx.new_int(idx), vm))?;
208+
if idx > 0 && chars.nth(idx - 1).is_none() {
209+
return Ok(PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx))));
198210
}
199211
zelf.parse(
200212
chars.as_str(),
@@ -209,7 +221,7 @@ mod _json {
209221
impl Callable for JsonScanner {
210222
fn call(zelf: &PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
211223
let (pystr, idx) = args.bind::<(PyStrRef, isize)>(vm)?;
212-
JsonScanner::call(zelf, pystr, idx, vm)
224+
JsonScanner::call(zelf, pystr, idx, vm).into_pyresult(vm)
213225
}
214226
}
215227

vm/src/iterator.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ where
2626
Ok(results)
2727
}
2828

29-
pub fn stop_iter_with_value(val: PyObjectRef, vm: &VirtualMachine) -> PyBaseExceptionRef {
30-
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
31-
vm.new_exception(stop_iteration_type, vec![val])
32-
}
33-
3429
pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyObjectRef {
3530
let args = exc.args();
3631
vm.unwrap_or_none(args.as_slice().first().cloned())

wasm/lib/src/js_module.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,20 +552,17 @@ impl fmt::Debug for AwaitPromise {
552552
#[pyimpl(with(SlotIterator))]
553553
impl AwaitPromise {
554554
#[pymethod]
555-
fn send(&self, val: Option<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
555+
fn send(&self, val: Option<PyObjectRef>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
556556
match self.obj.take() {
557557
Some(prom) => {
558558
if val.is_some() {
559559
Err(vm
560560
.new_type_error("can't send non-None value to an awaitpromise".to_owned()))
561561
} else {
562-
Ok(prom)
562+
Ok(PyIterReturn::Return(prom))
563563
}
564564
}
565-
None => Err(rustpython_vm::iterator::stop_iter_with_value(
566-
vm.unwrap_or_none(val),
567-
vm,
568-
)),
565+
None => Ok(PyIterReturn::StopIteration(val)),
569566
}
570567
}
571568

@@ -590,7 +587,7 @@ impl AwaitPromise {
590587
impl IteratorIterable for AwaitPromise {}
591588
impl SlotIterator for AwaitPromise {
592589
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
593-
PyIterReturn::from_pyresult(zelf.send(None, vm), vm)
590+
zelf.send(None, vm)
594591
}
595592
}
596593

0 commit comments

Comments
 (0)