summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-01-28 01:34:43 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-01-28 01:34:43 (GMT)
commit064567e41a4a0a2e717e9cc5ec960f366bd8491c (patch)
treea5277a70a92c90a37a2b23f67784520cdcbdebda /Lib/pickle.py
parent21c18f0bf5c3edc6ed226f71d36910292575fb92 (diff)
downloadcpython-064567e41a4a0a2e717e9cc5ec960f366bd8491c.zip
cpython-064567e41a4a0a2e717e9cc5ec960f366bd8491c.tar.gz
cpython-064567e41a4a0a2e717e9cc5ec960f366bd8491c.tar.bz2
save_dict(): Untangled most of the bin-vs-not-bin logic. Also used
iteritems() instead of materializing a (possibly giant) list of the items.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py29
1 files changed, 14 insertions, 15 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index f8ecad5..957a87f 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -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:
- write(MARK + DICT)
-
- self.memoize(object)
-
- using_setitems = (self.bin and (len(object) > 1))
+ self.memoize(object)
+ if len(object) > 1:
+ write(MARK)
+ for key, value in items:
+ save(key)
+ save(value)
+ write(SETITEMS)
+ return
- if using_setitems:
- write(MARK)
+ else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
+ write(MARK + DICT)
+ self.memoize(object)
- 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: