Skip to content

Commit 4fa459b

Browse files
committed
pythongh-126317: Simplify stdlib code by using itertools.batched()
1 parent f0c6fcc commit 4fa459b

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

Lib/pickle.py

+21-39
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from types import FunctionType
2727
from copyreg import dispatch_table
2828
from copyreg import _extension_registry, _inverted_registry, _extension_cache
29-
from itertools import islice
29+
from itertools import batched
3030
from functools import partial
3131
import sys
3232
from sys import maxsize
@@ -1033,31 +1033,25 @@ def _batch_appends(self, items, obj):
10331033
write(APPEND)
10341034
return
10351035

1036-
it = iter(items)
10371036
start = 0
1038-
while True:
1039-
tmp = list(islice(it, self._BATCHSIZE))
1040-
n = len(tmp)
1041-
if n > 1:
1037+
for batch in batched(items, self._BATCHSIZE):
1038+
if len(batch) != 1:
10421039
write(MARK)
1043-
for i, x in enumerate(tmp, start):
1040+
for i, x in enumerate(batch, start):
10441041
try:
10451042
save(x)
10461043
except BaseException as exc:
10471044
exc.add_note(f'when serializing {_T(obj)} item {i}')
10481045
raise
10491046
write(APPENDS)
1050-
elif n:
1047+
else:
10511048
try:
1052-
save(tmp[0])
1049+
save(batch[0])
10531050
except BaseException as exc:
10541051
exc.add_note(f'when serializing {_T(obj)} item {start}')
10551052
raise
10561053
write(APPEND)
1057-
# else tmp is empty, and we're done
1058-
if n < self._BATCHSIZE:
1059-
return
1060-
start += n
1054+
start += len(batch)
10611055

10621056
def save_dict(self, obj):
10631057
if self.bin:
@@ -1086,32 +1080,26 @@ def _batch_setitems(self, items, obj):
10861080
write(SETITEM)
10871081
return
10881082

1089-
it = iter(items)
1090-
while True:
1091-
tmp = list(islice(it, self._BATCHSIZE))
1092-
n = len(tmp)
1093-
if n > 1:
1083+
for batch in batched(items, self._BATCHSIZE):
1084+
if len(batch) != 1:
10941085
write(MARK)
1095-
for k, v in tmp:
1086+
for k, v in batch:
10961087
save(k)
10971088
try:
10981089
save(v)
10991090
except BaseException as exc:
11001091
exc.add_note(f'when serializing {_T(obj)} item {k!r}')
11011092
raise
11021093
write(SETITEMS)
1103-
elif n:
1104-
k, v = tmp[0]
1094+
else:
1095+
k, v = batch[0]
11051096
save(k)
11061097
try:
11071098
save(v)
11081099
except BaseException as exc:
11091100
exc.add_note(f'when serializing {_T(obj)} item {k!r}')
11101101
raise
11111102
write(SETITEM)
1112-
# else tmp is empty, and we're done
1113-
if n < self._BATCHSIZE:
1114-
return
11151103

11161104
def save_set(self, obj):
11171105
save = self.save
@@ -1124,21 +1112,15 @@ def save_set(self, obj):
11241112
write(EMPTY_SET)
11251113
self.memoize(obj)
11261114

1127-
it = iter(obj)
1128-
while True:
1129-
batch = list(islice(it, self._BATCHSIZE))
1130-
n = len(batch)
1131-
if n > 0:
1132-
write(MARK)
1133-
try:
1134-
for item in batch:
1135-
save(item)
1136-
except BaseException as exc:
1137-
exc.add_note(f'when serializing {_T(obj)} element')
1138-
raise
1139-
write(ADDITEMS)
1140-
if n < self._BATCHSIZE:
1141-
return
1115+
for batch in batched(obj, self._BATCHSIZE):
1116+
write(MARK)
1117+
try:
1118+
for item in batch:
1119+
save(item)
1120+
except BaseException as exc:
1121+
exc.add_note(f'when serializing {_T(obj)} element')
1122+
raise
1123+
write(ADDITEMS)
11421124
dispatch[set] = save_set
11431125

11441126
def save_frozenset(self, obj):

0 commit comments

Comments
 (0)