Skip to content

Add length check, reduce, setstate on zip from cpython 3.10 - #3235

Merged
youknowone merged 4 commits into
RustPython:mainfrom
Snowapril:zip_strict_option
Oct 6, 2021
Merged

youknowone merged 4 commits into
RustPython:mainfrom
Snowapril:zip_strict_option

Conversation

@Snowapril

Copy link
Copy Markdown
Contributor

This revision implements optional length-checking to zip and its related new methods (__reduce__, __setstate__).

This is new features in cpython 3.10 stable-release.
If strict=True option is given to zip, all the iterators to be zipped must have same length.

>>>>> a = [1,2,3]
>>>>> b = (4,5)
>>>>> for k in zip(a,b,strict=True):
.....   print(k)
..... 
(1, 4)
(2, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is shorter than argument 1

>>>>> for k in zip(b,a,strict=True):
.....   print(k)
..... 
(4, 1)
(5, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is longer than argument 1

also implement __setstate__ and __reduce__ for the strict parameter in zipobject.

>>>>> z = zip([1,2,3], (4,5,6), strict=True)
>>>>> z.__reduce__()
(<class 'zip'>, (<list_iterator object at 0x557d82f1b9f0>, <tuple_iterator object at 0x557d82df3a30>), True)
>>>>> z.__setstate__(False)
>>>>> z.__reduce__()
(<class 'zip'>, (<list_iterator object at 0x557d82f1b9f0>, <tuple_iterator object at 0x557d82df3a30>))

Documents

Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Comment thread vm/src/builtins/zip.rs Resolved
when strict option is enabled, zipped iterators' lengths are must be
same. If not, value error will be occurred

* https://www.python.org/dev/peps/pep-0618/

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

@youknowone youknowone 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.

looks great

@youknowone
youknowone merged commit 6a8d7e6 into RustPython:main Oct 6, 2021
@Snowapril
Snowapril deleted the zip_strict_option branch October 7, 2021 00:48
@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