8000 gh-126317: Simplify stdlib code by using itertools.batched() · python/cpython@11eb1ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 11eb1ee

Browse files
committed
gh-126317: Simplify stdlib code by using itertools.batched()
1 parent f0c6fcc commit 11eb1ee

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

Lib/pickle.py

Lines changed: 10 additions & 21 deletions
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
@@ -1035,12 +1035,11 @@ def _batch_appends(self, items, obj):
10351035

10361036
it = iter(items)
10371037
start = 0
1038-
while True:
1039-
tmp = list(islice(it, self._BATCHSIZE))
1040-
n = len(tmp)
1038+
for batch in batched(it, self._BATCHSIZE):
1039+
n = len(batch)
10411040
if n > 1:
10421041
write(MARK)
1043-
for i, x in enumerate(tmp, start):
1042+
for i, x in enumerate(batch, start):
10441043
try:
10451044
save(x)
10461045
except BaseException as exc:
@@ -1049,14 +1048,11 @@ def _batch_appends(self, items, obj):
10491048
write(APPENDS)
10501049
elif n:
10511050
try:
1052-
save(tmp[0])
1051+
save( 8000 batch[0])
10531052
except BaseException as exc:
10541053
exc.add_note(f'when serializing {_T(obj)} item {start}')
10551054
raise
10561055
write(APPEND)
1057-
# else tmp is empty, and we're done
1058-
if n < self._BATCHSIZE:
1059-
return
10601056
start += n
10611057

10621058
def save_dict(self, obj):
@@ -1087,12 +1083,11 @@ def _batch_setitems(self, items, obj):
10871083
return
10881084

10891085
it = iter(items)
1090-
while True:
1091-
tmp = list(islice(it, self._BATCHSIZE))
1092-
n = len(tmp)
1086+
for batch in batched(it, self._BATCHSIZE):
1087+
n = len(batch)
10931088
if n > 1:
10941089
write(MARK)
1095-
for k, v in tmp:
1090+
for k, v in batch:
10961091
save(k)
10971092
try:
10981093
save(v)
@@ -1101,17 +1096,14 @@ def _batch_setitems(self, items, obj):
11011096
raise
11021097
write(SETITEMS)
11031098
elif n:
1104-
k, v = tmp[0]
1099+
k, v = batch[0]
11051100
save(k)
11061101
try:
11071102
save(v)
11081103
except BaseException as exc:
11091104
exc.add_note(f'when serializing {_T(obj)} item {k!r}')
11101105
raise
11111106
write(SETITEM)
1112-
# else tmp is empty, and we're done
1113-
if n < self._BATCHSIZE:
1114-
return
11151107

11161108
def save_set(self, obj):
11171109
save = self.save
@@ -1125,8 +1117,7 @@ def save_set(self, obj):
11251117
self.memoize(obj)
11261118

11271119
it = iter(obj)
1128-
while True:
1129-
batch = list(islice(it, self._BATCHSIZE))
1120+
for batch in batched(it, self._BATCHSIZE):
11301121
n = len(batch)
11311122
if n > 0:
11321123
write(MARK)
@@ -1137,8 +1128,6 @@ def save_set(self, obj):
11371128
exc.add_note(f'when serializing {_T(obj)} element')
11381129
raise
11391130
write(ADDITEMS)
1140-
if n < self._BATCHSIZE:
1141-
return
11421131
dispatch[set] = save_set
11431132

11441133
def save_frozenset(self, obj):

0 commit comments

Comments
 (0)
0