|
| 1 | +/* |
| 2 | + * Various types to support iteration. |
| 3 | + */ |
| 4 | + |
| 5 | +use super::super::pyobject::{ |
| 6 | + AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, |
| 7 | + PyResult, TypeProtocol, |
| 8 | +}; |
| 9 | +use super::super::vm::VirtualMachine; |
| 10 | +use super::objstr; |
| 11 | +use super::objtype; // Required for arg_check! to use isinstance |
| 12 | + |
| 13 | +pub fn get_iter(vm: &mut VirtualMachine, iter_target: &PyObjectRef) -> PyResult { |
| 14 | + // Check what we are going to iterate over: |
| 15 | + let iterated_obj = if objtype::isinstance(iter_target, vm.ctx.iter_type()) { |
| 16 | + // If object is already an iterator, return that one. |
| 17 | + return Ok(iter_target.clone()) |
| 18 | + } else if objtype::isinstance(iter_target, vm.ctx.list_type()) { |
| 19 | + iter_target.clone() |
| 20 | + } else { |
| 21 | + let type_str = objstr::get_value(&vm.to_str(iter_target.typ()).unwrap()); |
| 22 | + let type_error = vm.new_type_error(format!("Cannot iterate over {}", type_str)); |
| 23 | + return Err(type_error); |
| 24 | + }; |
| 25 | + |
| 26 | + let iter_obj = PyObject::new( |
| 27 | + PyObjectKind::Iterator { |
| 28 | + position: 0, |
| 29 | + iterated_obj: iterated_obj, |
| 30 | + }, |
| 31 | + vm.ctx.iter_type(), |
| 32 | + ); |
| 33 | + |
| 34 | + // We are all good here: |
| 35 | + Ok(iter_obj) |
| 36 | +} |
| 37 | + |
| 38 | +// Sequence iterator: |
| 39 | +fn iter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
| 40 | + arg_check!( |
| 41 | + vm, |
| 42 | + args, |
| 43 | + required = [(iter_target, None)] |
| 44 | + ); |
| 45 | + |
| 46 | + get_iter(vm, iter_target) |
| 47 | +} |
| 48 | + |
| 49 | +fn iter_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
| 50 | + arg_check!( |
| 51 | + vm, |
| 52 | + args, |
| 53 | + required = [(iter, Some(vm.ctx.iter_type()))] |
| 54 | + ); |
| 55 | + // Return self: |
| 56 | + Ok(iter.clone()) |
| 57 | +} |
| 58 | + |
| 59 | +fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
| 60 | + arg_check!( |
| 61 | + vm, |
| 62 | + args, |
| 63 | + required = [(iter, Some(vm.ctx.iter_type()))] |
| 64 | + ); |
| 65 | + |
| 66 | + let next_obj: Option<PyObjectRef> = { |
| 67 | + // We require a mutable pyobject here to update the iterator: |
| 68 | + let mut iterator: &mut PyObject = &mut iter.borrow_mut(); |
| 69 | + iterator.nxt() |
| 70 | + }; |
| 71 | + |
| 72 | + // Return next item, or StopIteration |
| 73 | + match next_obj { |
| 74 | + Some(value) => Ok(value), |
| 75 | + None => { |
| 76 | + let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone(); |
| 77 | + let stop_iteration = vm.new_exception(stop_iteration_type, "End of iterator".to_string()); |
| 78 | + Err(stop_iteration) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +pub fn init(context: &PyContext) { |
| 84 | + let ref iter_type = context.iter_type; |
| 85 | + iter_type.set_attr("__new__", context.new_rustfunc(iter_new)); |
| 86 | + iter_type.set_attr("__iter__", context.new_rustfunc(iter_iter)); |
| 87 | + iter_type.set_attr("__next__", context.new_rustfunc(iter_next)); |
| 88 | +} |
| 89 | + |
0 commit comments