forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjiter.rs
More file actions
265 lines (239 loc) · 7.75 KB
/
objiter.rs
File metadata and controls
265 lines (239 loc) · 7.75 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
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
/*
* Various types to support iteration.
*/
use crossbeam_utils::atomic::AtomicCell;
use num_traits::{Signed, ToPrimitive};
use super::objint::PyInt;
use super::objsequence;
use super::objtype::{self, PyClassRef};
use crate::exceptions::PyBaseExceptionRef;
use crate::pyobject::{
PyCallable, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;
/*
* This helper function is called at multiple places. First, it is called
* in the vm when a for loop is entered. Next, it is used when the builtin
* function 'iter' is called.
*/
pub fn get_iter(vm: &VirtualMachine, iter_target: &PyObjectRef) -> PyResult {
if let Some(method_or_err) = vm.get_method(iter_target.clone(), "__iter__") {
let method = method_or_err?;
vm.invoke(&method, vec![])
} else {
vm.get_method_or_type_error(iter_target.clone(), "__getitem__", || {
format!("Cannot iterate over {}", iter_target.class().name)
})?;
Ok(PySequenceIterator::new_forward(iter_target.clone())
.into_ref(vm)
.into_object())
}
}
pub fn call_next(vm: &VirtualMachine, iter_obj: &PyObjectRef) -> PyResult {
vm.call_method(iter_obj, "__next__", vec![])
}
/*
* Helper function to retrieve the next object (or none) from an iterator.
*/
pub fn get_next_object(
vm: &VirtualMachine,
iter_obj: &PyObjectRef,
) -> PyResult<Option<PyObjectRef>> {
let next_obj: PyResult = call_next(vm, iter_obj);
match next_obj {
Ok(value) => Ok(Some(value)),
Err(next_error) => {
// Check if we have stopiteration, or something else:
if objtype::isinstance(&next_error, &vm.ctx.exceptions.stop_iteration) {
Ok(None)
} else {
Err(next_error)
}
}
}
}
/* Retrieve all elements from an iterator */
pub fn get_all<T: TryFromObject>(vm: &VirtualMachine, iter_obj: &PyObjectRef) -> PyResult<Vec<T>> {
let cap = length_hint(vm, iter_obj.clone())?.unwrap_or(0);
// TODO: fix extend to do this check (?), see test_extend in Lib/test/list_tests.py,
// https://github.com/python/cpython/blob/master/Objects/listobject.c#L934-L940
if cap >= isize::max_value() as usize {
return Ok(Vec::new());
}
let mut elements = Vec::with_capacity(cap);
while let Some(element) = get_next_object(vm, iter_obj)? {
elements.push(T::try_from_object(vm, element)?);
}
elements.shrink_to_fit();
Ok(elements)
}
pub fn new_stop_iteration(vm: &VirtualMachine) -> PyBaseExceptionRef {
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
vm.new_exception_empty(stop_iteration_type)
}
pub fn stop_iter_with_value(val: PyObjectRef, vm: &VirtualMachine) -> PyBaseExceptionRef {
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
vm.new_exception(stop_iteration_type, vec![val])
}
pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResult {
let args = exc.args();
let val = args
.as_slice()
.first()
.cloned()
.unwrap_or_else(|| vm.get_none());
Ok(val)
}
pub fn length_hint(vm: &VirtualMachine, iter: PyObjectRef) -> PyResult<Option<usize>> {
if let Some(len) = objsequence::opt_len(&iter, vm) {
match len {
Ok(len) => return Ok(Some(len)),
Err(e) => {
if !objtype::isinstance(&e, &vm.ctx.exceptions.type_error) {
return Err(e);
}
}
}
}
let hint = match vm.get_method(iter, "__length_hint__") {
Some(hint) => hint?,
None => return Ok(None),
};
let result = match vm.invoke(&hint, vec![]) {
Ok(res) => res,
Err(e) => {
if objtype::isinstance(&e, &vm.ctx.exceptions.type_error) {
return Ok(None);
} else {
return Err(e);
}
}
};
let result = result
.payload_if_subclass::<PyInt>(vm)
.ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
result.class().name
))
})?
.as_bigint();
if result.is_negative() {
return Err(vm.new_value_error("__length_hint__() should return >= 0".to_owned()));
}
let hint = result.to_usize().ok_or_else(|| {
vm.new_value_error("Python int too large to convert to Rust usize".to_owned())
})?;
Ok(Some(hint))
}
#[pyclass]
#[derive(Debug)]
pub struct PySequenceIterator {
pub position: AtomicCell<isize>,
pub obj: PyObjectRef,
pub reversed: bool,
}
impl ThreadSafe for PySequenceIterator {}
impl PyValue for PySequenceIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.iter_type()
}
}
#[pyimpl]
impl PySequenceIterator {
pub fn new_forward(obj: PyObjectRef) -> Self {
Self {
position: AtomicCell::new(0),
obj,
reversed: false,
}
}
pub fn new_reversed(obj: PyObjectRef, len: isize) -> Self {
Self {
position: AtomicCell::new(len - 1),
obj,
reversed: true,
}
}
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let step: isize = if self.reversed { -1 } else { 1 };
let pos = self.position.fetch_add(step);
if pos >= 0 {
match vm.call_method(&self.obj, "__getitem__", vec![vm.new_int(pos)]) {
Err(ref e) if objtype::isinstance(&e, &vm.ctx.exceptions.index_error) => {
Err(new_stop_iteration(vm))
}
// also catches stop_iteration => stop_iteration
ret => ret,
}
} else {
Err(new_stop_iteration(vm))
}
}
#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>) -> PyRef<Self> {
zelf
}
#[pymethod(name = "__length_hint__")]
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<isize> {
let pos = self.position.load();
let hint = if self.reversed {
pos + 1
} else {
let len = objsequence::opt_len(&self.obj, vm).unwrap_or_else(|| {
Err(vm.new_type_error("sequence has no __len__ method".to_owned()))
})?;
len as isize - pos
};
Ok(hint)
}
}
pub fn seq_iter_method(obj: PyObjectRef) -> PySequenceIterator {
PySequenceIterator::new_forward(obj)
}
#[pyclass(name = "callable_iterator")]
#[derive(Debug)]
pub struct PyCallableIterator {
callable: PyCallable,
sentinel: PyObjectRef,
done: AtomicCell<bool>,
}
impl ThreadSafe for PyCallableIterator {}
impl PyValue for PyCallableIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.types.callable_iterator.clone()
}
}
#[pyimpl]
impl PyCallableIterator {
pub fn new(callable: PyCallable, sentinel: PyObjectRef) -> Self {
Self {
callable,
sentinel,
done: AtomicCell::new(false),
}
}
#[pymethod(magic)]
fn next(&self, vm: &VirtualMachine) -> PyResult {
if self.done.load() {
return Err(new_stop_iteration(vm));
}
let ret = self.callable.invoke(vec![], vm)?;
if vm.bool_eq(ret.clone(), self.sentinel.clone())? {
self.done.store(true);
Err(new_stop_iteration(vm))
} else {
Ok(ret)
}
}
#[pymethod(magic)]
fn iter(zelf: PyRef<Self>) -> PyRef<Self> {
zelf
}
}
pub fn init(context: &PyContext) {
PySequenceIterator::extend_class(context, &context.types.iter_type);
PyCallableIterator::extend_class(context, &context.types.callable_iterator);
}