mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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:
parent
21c18f0bf5
commit
064567e41a
1 changed files with 14 additions and 15 deletions
|
@ -508,29 +508,28 @@ class Pickler:
|
||||||
def save_dict(self, object):
|
def save_dict(self, object):
|
||||||
write = self.write
|
write = self.write
|
||||||
save = self.save
|
save = self.save
|
||||||
|
items = object.iteritems()
|
||||||
|
|
||||||
if self.bin:
|
if self.bin:
|
||||||
write(EMPTY_DICT)
|
write(EMPTY_DICT)
|
||||||
else:
|
|
||||||
write(MARK + DICT)
|
|
||||||
|
|
||||||
self.memoize(object)
|
self.memoize(object)
|
||||||
|
if len(object) > 1:
|
||||||
using_setitems = (self.bin and (len(object) > 1))
|
|
||||||
|
|
||||||
if using_setitems:
|
|
||||||
write(MARK)
|
write(MARK)
|
||||||
|
|
||||||
items = object.items()
|
|
||||||
for key, value in items:
|
for key, value in items:
|
||||||
save(key)
|
save(key)
|
||||||
save(value)
|
save(value)
|
||||||
|
|
||||||
if not using_setitems:
|
|
||||||
write(SETITEM)
|
|
||||||
|
|
||||||
if using_setitems:
|
|
||||||
write(SETITEMS)
|
write(SETITEMS)
|
||||||
|
return
|
||||||
|
|
||||||
|
else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
|
||||||
|
write(MARK + DICT)
|
||||||
|
self.memoize(object)
|
||||||
|
|
||||||
|
# proto 0 or len(object) < 2
|
||||||
|
for key, value in items:
|
||||||
|
save(key)
|
||||||
|
save(value)
|
||||||
|
write(SETITEM)
|
||||||
|
|
||||||
dispatch[DictionaryType] = save_dict
|
dispatch[DictionaryType] = save_dict
|
||||||
if not PyStringMap is None:
|
if not PyStringMap is None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue