summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2022-04-18 14:18:27 (GMT)
committerGitHub <noreply@github.com>2022-04-18 14:18:27 (GMT)
commit8c54c3dacccb12a712acaa48d86a54f9ee9e37b5 (patch)
tree429abc799a7e0e6269fb2bec5c607c69809bc850 /Lib
parenta29f858124bc698f6604716b73306c65b63b5054 (diff)
downloadcpython-8c54c3dacccb12a712acaa48d86a54f9ee9e37b5.zip
cpython-8c54c3dacccb12a712acaa48d86a54f9ee9e37b5.tar.gz
cpython-8c54c3dacccb12a712acaa48d86a54f9ee9e37b5.tar.bz2
gh-91576: Speed up iteration of strings (#91574)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_unicode.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index df7afd5..c98fabf 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -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