Skip to content

Implement methods for PyReverseSequenceIterator#2911

Merged
youknowone merged 3 commits into
RustPython:masterfrom
Snowapril:fix-reversed-iter
Aug 19, 2021
Merged

Implement methods for PyReverseSequenceIterator#2911
youknowone merged 3 commits into
RustPython:masterfrom
Snowapril:fix-reversed-iter

Conversation

@Snowapril

@Snowapril Snowapril commented Aug 19, 2021

Copy link
Copy Markdown
Contributor

This revision includes:

  • Implement length_hint, reduce, setstate and next for PyReverseSequenceIterator
  • Remove PyStrReverseIterator to make PyStr type use general PyReverseSequenceIterator
  • Removes expectedFailure annotations in test_enumerate.py, test_tuple.py

By these changes, TestReversed.test_len, TestReversed.test_pickle and TupleTest.test_reversed_pickle tests became successful now.

Also, below python codes are now available by this PR

Before

>>>>> a = reversed('hello')
>>>>> operator.length_hint(a)
0
>>>>> a.__reduce__()
(<function _reconstructor at 0x55e7c6094470>, (<class 'reversed'>, <class 'object'>, None))
>>>>> a.__setstate__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str_reverseiterator' object has no attribute '__setstate__'

After

>>>>> a = reversed('hello')
>>>>> operator.length_hint(a)
5
>>>>> a.__reduce__()
(<built-in function reversed>, ('hello',), 4)
>>>>> a.__setstate__(2)
>>>>> a.__reduce__()
(<built-in function reversed>, ('hello',), 2)

Before

>>>>> a = reversed(tuple('hello'))
>>>>> operator.length_hint(a)
5
>>>>> a.__setstate__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'reversed' object has no attribute '__setstate__'
>>>>> a.__reduce__()
(<function _reconstructor at 0x55e7c6094470>, (<class 'reversed'>, <class 'object'>, None))

After

>>>>> a = reversed(tuple('hello'))
>>>>> operator.length_hint(a)
5
>>>>> a.__reduce__()
(<built-in function reversed>, (('h', 'e', 'l', 'l', 'o'),), 4)
>>>>> a.__setstate__(2)
>>>>> a.__reduce__()
(<built-in function reversed>, (('h', 'e', 'l', 'l', 'o'),), 2)

Except above example codes, general object that implements required interface can use PyReverseSequenceIterator and its methods.

Comment thread vm/src/builtins/enumerate.rs Outdated
Comment thread vm/src/builtins/enumerate.rs
This commit implements `length_hint`, `reduce`, `setstate`, `next` for
`PyReverseSequenceIterator`.

In previous rustpython, it did not conduct methods well for
general object.

Signed-off-by: snowapril <sinjihng@gmail.com>
This commit removes `PyStrReverseIterator` and its traits for using
general `PyReverseSequenceIterator` for `PyStr` type.

In previous rustpython, `PyStrReverseIterator` type registered in
`TypeZoo`, even it had no methods(e.g. `length_hint`, `reduce`, `setstate`)

Therefore, by removing `PyStrReverseIterator`, `PyStr` enable to use
`PySequenceReverseIterator`.

Signed-off-by: snowapril <sinjihng@gmail.com>
By implement general PyReverseSequenceIterator and make PyStr to use it,
now three tests fixed.

Signed-off-by: snowapril <sinjihng@gmail.com>

@DimitrisJim DimitrisJim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@youknowone pinging you here because remember you had some feedback when I added this type back in #2900. Overall this lgtm.

@youknowone youknowone merged commit 1e7af98 into RustPython:master Aug 19, 2021
@youknowone

Copy link
Copy Markdown
Member

@DimitrisJim yes, I found this PR is solving infinite sub problem, thanks!

@youknowone youknowone added the z-ca-2021 Tag to track contrubution-academy 2021 label Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2021 Tag to track contrubution-academy 2021

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants