26
26
from types import FunctionType
27
27
from copyreg import dispatch_table
28
28
from copyreg import _extension_registry , _inverted_registry , _extension_cache
29
- from itertools import islice
29
+ from itertools import batched
30
30
from functools import partial
31
31
import sys
32
32
from sys import maxsize
@@ -1033,31 +1033,25 @@ def _batch_appends(self, items, obj):
1033
1033
write (APPEND )
1034
1034
return
1035
1035
1036
- it = iter (items )
1037
1036
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 :
1042
1039
write (MARK )
1043
- for i , x in enumerate (tmp , start ):
1040
+ for i , x in enumerate (batch , start ):
1044
1041
try :
1045
1042
save (x )
1046
1043
except BaseException as exc :
1047
1044
exc .add_note (f'when serializing { _T (obj )} item { i } ' )
1048
1045
raise
1049
1046
write (APPENDS )
1050
- elif n :
1047
+ else :
1051
1048
try :
1052
- save (tmp [0 ])
1049
+ save (batch [0 ])
1053
1050
except BaseException as exc :
1054
1051
exc .add_note (f'when serializing { _T (obj )} item { start } ' )
1055
1052
raise
1056
1053
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 )
1061
1055
1062
1056
def save_dict (self , obj ):
1063
1057
if self .bin :
@@ -1086,32 +1080,26 @@ def _batch_setitems(self, items, obj):
1086
1080
write (SETITEM )
1087
1081
return
1088
1082
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 :
1094
1085
write (MARK )
1095
- for k , v in tmp :
1086
+ for k , v in batch :
1096
1087
save (k )
1097
1088
try :
1098
1089
save (v )
1099
1090
except BaseException as exc :
1100
1091
exc .add_note (f'when serializing { _T (obj )} item { k !r} ' )
1101
1092
raise
1102
1093
write (SETITEMS )
1103
- elif n :
1104
- k , v = tmp [0 ]
1094
+ else :
1095
+ k , v = batch [0 ]
1105
1096
save (k )
1106
1097
try :
1107
1098
save (v )
1108
1099
except BaseException as exc :
1109
1100
exc .add_note (f'when serializing { _T (obj )} item { k !r} ' )
1110
1101
raise
1111
1102
write (SETITEM )
1112
- # else tmp is empty, and we're done
1113
- if n < self ._BATCHSIZE :
1114
- return
1115
1103
1116
1104
def save_set (self , obj ):
1117
1105
save = self .save
@@ -1124,21 +1112,15 @@ def save_set(self, obj):
1124
1112
write (EMPTY_SET )
1125
1113
self .memoize (obj )
1126
1114
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 )
1142
1124
dispatch [set ] = save_set
1143
1125
1144
1126
def save_frozenset (self , obj ):
0 commit comments