Skip to content

Implement str iterator support #1171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/snippets/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,33 @@ def try_mutate_str():
assert "\u00BE" == "¾"
assert "\u9487" == "钇"
assert "\U0001F609" == "😉"

# test str iter
iterable_str = "123456789"
str_iter = iter(iterable_str)

assert next(str_iter) == "1"
assert next(str_iter) == "2"
assert next(str_iter) == "3"
assert next(str_iter) == "4"
assert next(str_iter) == "5"
assert next(str_iter) == "6"
assert next(str_iter) == "7"
assert next(str_iter) == "8"
assert next(str_iter) == "9"
assert next(str_iter, None) == None
assert_raises(StopIteration, lambda: next(str_iter))

str_iter_reversed = reversed(iterable_str)

assert next(str_iter_reversed) == "9"
assert next(str_iter_reversed) == "8"
assert next(str_iter_reversed) == "7"
assert next(str_iter_reversed) == "6"
assert next(str_iter_reversed) == "5"
assert next(str_iter_reversed) == "4"
assert next(str_iter_reversed) == "3"
assert next(str_iter_reversed) == "2"
assert next(str_iter_reversed) == "1"
assert next(str_iter_reversed, None) == None
assert_raises(StopIteration, lambda: next(str_iter_reversed))
96 changes: 96 additions & 0 deletions vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate unicode_categories;
extern crate unicode_xid;

use std::cell::Cell;
use std::char;
use std::fmt;
use std::ops::Range;
Expand Down Expand Up @@ -28,6 +29,7 @@ use crate::vm::VirtualMachine;
use super::objbytes::PyBytes;
use super::objdict::PyDict;
use super::objint::{self, PyInt};
use super::objiter;
use super::objnone::PyNone;
use super::objsequence::PySliceableSequence;
use super::objslice::PySlice;
Expand Down Expand Up @@ -90,6 +92,79 @@ impl TryIntoRef<PyString> for &str {
}
}

#[pyclass]
#[derive(Debug)]
pub struct PyStringIterator {
pub string: PyStringRef,
position: Cell<usize>,
}

impl PyValue for PyStringIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.striterator_type()
}
}

#[pyimpl]
impl PyStringIterator {
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let pos = self.position.get();

if pos < self.string.value.chars().count() {
self.position.set(self.position.get() + 1);

#[allow(clippy::range_plus_one)]
let value = self.string.value.do_slice(pos..pos + 1);

value.into_pyobject(vm)
} else {
Err(objiter::new_stop_iteration(vm))
}
}

#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}
}

#[pyclass]
#[derive(Debug)]
pub struct PyStringReverseIterator {
pub position: Cell<usize>,
pub string: PyStringRef,
}

impl PyValue for PyStringReverseIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.strreverseiterator_type()
}
}

#[pyimpl]
impl PyStringReverseIterator {
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
if self.position.get() > 0 {
let position: usize = self.position.get() - 1;

#[allow(clippy::range_plus_one)]
let value = self.string.value.do_slice(position..position + 1);

self.position.set(position);
value.into_pyobject(vm)
} else {
Err(objiter::new_stop_iteration(vm))
}
}

#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}
}

#[pyimpl]
impl PyString {
// TODO: should with following format
Expand Down Expand Up @@ -1025,6 +1100,24 @@ impl PyString {
let encoded = PyBytes::from_string(&self.value, &encoding, vm)?;
Ok(encoded.into_pyobject(vm)?)
}

#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyStringIterator {
PyStringIterator {
position: Cell::new(0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you're missing the __reversed__ method somewhere here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I checked it using CPython (3.7.3):

>>> hasattr(str, "__reversed__")
False

Should I implement __reversed__ despite this fact or leave it unchanged?

Copy link
Member

@coolreader18 coolreader18 Jul 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is PyStringReverseIterator used then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within reverse:

string = "123456789"
reversed_iter = reversed(string)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but nowhere in your code do you construct a PyStringReverseIterator, so there's no way to actually get one in Python code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I'll fix it in a moment.

string: zelf,
}
}

#[pymethod(name = "__reversed__")]
fn reversed(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyStringReverseIterator {
let begin = zelf.value.chars().count();

PyStringReverseIterator {
position: Cell::new(begin),
string: zelf,
}
}
}

impl PyValue for PyString {
Expand Down Expand Up @@ -1053,6 +1146,9 @@ impl IntoPyObject for &String {

pub fn init(ctx: &PyContext) {
PyString::extend_class(ctx, &ctx.str_type);

PyStringIterator::extend_class(ctx, &ctx.striterator_type);
PyStringReverseIterator::extend_class(ctx, &ctx.strreverseiterator_type);
}

pub fn get_value(obj: &PyObjectRef) -> String {
Expand Down
14 changes: 14 additions & 0 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pub struct PyContext {
pub list_type: PyClassRef,
pub listiterator_type: PyClassRef,
pub listreverseiterator_type: PyClassRef,
pub striterator_type: PyClassRef,
pub strreverseiterator_type: PyClassRef,
pub dictkeyiterator_type: PyClassRef,
pub dictvalueiterator_type: PyClassRef,
pub dictitemiterator_type: PyClassRef,
Expand Down Expand Up @@ -274,6 +276,8 @@ impl PyContext {
let listiterator_type = create_type("list_iterator", &type_type, &object_type);
let listreverseiterator_type =
create_type("list_reverseiterator", &type_type, &object_type);
let striterator_type = create_type("str_iterator", &type_type, &object_type);
let strreverseiterator_type = create_type("str_reverseiterator", &type_type, &object_type);
let dictkeys_type = create_type("dict_keys", &type_type, &object_type);
let dictvalues_type = create_type("dict_values", &type_type, &object_type);
let dictitems_type = create_type("dict_items", &type_type, &object_type);
Expand Down Expand Up @@ -341,6 +345,8 @@ impl PyContext {
list_type,
listiterator_type,
listreverseiterator_type,
striterator_type,
strreverseiterator_type,
dictkeys_type,
dictvalues_type,
dictitems_type,
Expand Down Expand Up @@ -476,6 +482,14 @@ impl PyContext {
self.listreverseiterator_type.clone()
}

pub fn striterator_type(&self) -> PyClassRef {
self.striterator_type.clone()
}

pub fn strreverseiterator_type(&self) -> PyClassRef {
self.strreverseiterator_type.clone()
}

pub fn module_type(&self) -> PyClassRef {
self.module_type.clone()
}
Expand Down