gh-91576: Speed up iteration of strings (#91574)

This commit is contained in:
Kumar Aditya 2022-04-18 19:48:27 +05:30 committed by GitHub
parent a29f858124
commit 8c54c3dacc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 6 deletions

View file

@ -9,6 +9,7 @@ import _string
import codecs
import itertools
import operator
import pickle
import struct
import sys
import textwrap
@ -185,6 +186,36 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual(next(it), "\u3333")
self.assertRaises(StopIteration, next, it)
def test_iterators_invocation(self):
cases = [type(iter('abc')), type(iter('🚀'))]
for cls in cases:
with self.subTest(cls=cls):
self.assertRaises(TypeError, cls)
def test_iteration(self):
cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
for case in cases:
with self.subTest(string=case):
self.assertEqual(case, "".join(iter(case)))
def test_exhausted_iterator(self):
cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
for case in cases:
with self.subTest(case=case):
iterator = iter(case)
tuple(iterator)
self.assertRaises(StopIteration, next, iterator)
def test_pickle_iterator(self):
cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
for case in cases:
with self.subTest(case=case):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
it = iter(case)
with self.subTest(proto=proto):
pickled = "".join(pickle.loads(pickle.dumps(it, proto)))
self.assertEqual(case, pickled)
def test_count(self):
string_tests.CommonTest.test_count(self)
# check mixed argument types