save_dict(): Untangled most of the bin-vs-not-bin logic. Also used

iteritems() instead of materializing a (possibly giant) list of the
items.
This commit is contained in:
Tim Peters 2003-01-28 01:34:43 +00:00
parent 21c18f0bf5
commit 064567e41a

View File

@ -508,29 +508,28 @@ class Pickler:
def save_dict(self, object):
write = self.write
save = self.save
items = object.iteritems()
if self.bin:
write(EMPTY_DICT)
else:
self.memoize(object)
if len(object) > 1:
write(MARK)
for key, value in items:
save(key)
save(value)
write(SETITEMS)
return
else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
write(MARK + DICT)
self.memoize(object)
self.memoize(object)
using_setitems = (self.bin and (len(object) > 1))
if using_setitems:
write(MARK)
items = object.items()
# proto 0 or len(object) < 2
for key, value in items:
save(key)
save(value)
if not using_setitems:
write(SETITEM)
if using_setitems:
write(SETITEMS)
write(SETITEM)
dispatch[DictionaryType] = save_dict
if not PyStringMap is None: