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,26 @@ 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
+ batch_len = len (batch )
1039
+ if batch_len != 1 :
1042
1040
write (MARK )
1043
- for i , x in enumerate (tmp , start ):
1041
+ for i , x in enumerate (batch , start ):
1044
1042
try :
1045
1043
save (x )
1046
1044
except BaseException as exc :
1047
1045
exc .add_note (f'when serializing { _T (obj )} item { i } ' )
1048
1046
raise
1049
1047
write (APPENDS )
1050
- elif n :
1048
+ else :
1051
1049
try :
1052
- save (tmp [0 ])
1050
+ save (batch [0 ])
1053
1051
except BaseException as exc :
1054
1052
exc .add_note (f'when serializing { _T (obj )} item { start } ' )
1055
1053
raise
1056
1054
write (APPEND )
1057
- # else tmp is empty, and we're done
1058
- if n < self ._BATCHSIZE :
1059
- return
1060
- start += n
1055
+ start += batch_len
1061
1056
1062
1057
def save_dict (self , obj ):
1063
1058
if self .bin :
@@ -1086,32 +1081,26 @@ def _batch_setitems(self, items, obj):
1086
1081
write (SETITEM )
1087
1082
return
1088
1083
1089
- it = iter (items )
1090
- while True :
1091
- tmp = list (islice (it , self ._BATCHSIZE ))
1092
- n = len (tmp )
1093
- if n > 1 :
1084
+ for batch in batched (items , self ._BATCHSIZE ):
1085
+ if len (batch ) != 1 :
1094
1086
write (MARK )
1095
- for k , v in tmp :
1087
+ for k , v in batch :
1096
1088
save (k )
1097
1089
try :
1098
1090
save (v )
1099
1091
except BaseException as exc :
1100
1092
exc .add_note (f'when serializing { _T (obj )} item { k !r} ' )
1101
1093
raise
1102
1094
write (SETITEMS )
1103
- elif n :
1104
- k , v = tmp [0 ]
1095
+ else :
1096
+ k , v = batch [0 ]
1105
1097
save (k )
1106
1098
try :
1107
1099
save (v )
1108
1100
except BaseException as exc :
1109
1101
exc .add_note (f'when serializing { _T (obj )} item { k !r} ' )
1110
1102
raise
1111
1103
write (SETITEM )
1112
- # else tmp is empty, and we're done
1113
- if n < self ._BATCHSIZE :
1114
- return
1115
1104
1116
1105
def save_set (self , obj ):
1117
1106
save = self .save
@@ -1124,21 +1113,15 @@ def save_set(self, obj):
1124
1113
write (EMPTY_SET )
1125
1114
self .memoize (obj )
1126
1115
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
1116
+ for batch in batched (obj , self ._BATCHSIZE ):
1117
+ write (MARK )
1118
+ try :
1119
+ for item in batch :
1120
+ save (item )
1121
+ except BaseException as exc :
1122
+ exc .add_note (f'when serializing { _T (obj )} element' )
1123
+ raise
1124
+ write (ADDITEMS )
1142
1125
dispatch [set ] = save_set
1143
1126
1144
1127
def save_frozenset (self , obj ):
0 commit comments